说明:我也是初学者,希望大家能提出宝贵意见。另外转载请注明作者左洸和出处博客园,毕竟花费了很长时间才完成。
不管是穷人还是富人,都要谈恋爱结婚,而且每个人的婚恋经历,步骤大体上都是一样的,比如说:见面、吃饭、游玩、婚礼、婚房 等等,这些步骤是社会已经给我们安排好了的,他就像一个大纲、一个模版,作为社会中的一个成员,不管是穷是富,都只能按照步骤去做,谁也不能改变这些步骤,但是每个步骤的具体内容暂时还是抽象的,怎么见面,怎么吃饭,怎么游玩,婚礼怎么办,婚房是什么样的,一千个人可能有一千个情况,比如你穷,和女朋友吃饭一碗面条也能打发;如果你富有,山珍海味也是吃饭。这就是为什么大家都恋爱结婚,却有人欢喜有人忧啊。
按照一个已经列好的大纲,根据自己具体的情况,实现其中的抽象步骤,这就是模版方法模式。
<a href="http://myqiao.cnblogs.com/category/33478.html?Show=All">点击浏览更多的“说故事、学模式”系列</a>
下面让我们来看看,用C#语言怎么描述穷人和富人的不同的婚恋历程,这是一个典型的模版方法模式。
TemplateMethod.cs
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 using System;
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 using System.Collections.Generic;
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 using System.Text;
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 namespace Template
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 {
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 //***************************************************************
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 //客户端
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 class Client
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 static void Main(string[] args)
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 try
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Society rich = new Rich();
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Society poor = new Poor();
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Society et = new ET();
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Console.WriteLine("富人的婚恋过程。。。。。。。。\n");
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Marry(rich);
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Console.WriteLine();
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Console.WriteLine("穷人的婚恋过程。。。。。。。。\n");
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Marry(poor);
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Console.WriteLine("外星人的婚恋过程。。。。。。。。\n");
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Marry(et);
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 }
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 catch (Exception e)
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Console.WriteLine(e.Message);
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Console.Read();
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 }
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 static public void Marry(Society society)
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 society.Marry();
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 }
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 //******************************************************************
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 //社会公认的婚恋过程,他是抽象的,只有步骤没有具体内容
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 public abstract class Society
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 abstract public void Meet(); //第一次见面
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 abstract public void Dinner(); //吃饭
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 abstract public void Journey(); //游玩约会
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 abstract public void Wedding(); //婚礼
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 abstract public void House(); //婚房
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 //每一个人都要经历的婚恋历程,需要做哪些事情社会已经替你安排好了
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 //人在江湖身不由己,不管你是穷人还是富人你都无法改变这个过程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 //但是,具体实施的时候我们还是可以掌握着自己的命运
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 //这里是恋爱结婚的模版方法
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 public void Marry()
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Console.Write("见面:");
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Meet();
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Console.Write("吃饭:");
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Dinner();
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Console.Write("游玩:");
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Journey();
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Console.Write("婚礼:");
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Wedding();
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Console.Write("婚房:");
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 House();
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 //我希望自己是一个富人,下面是我设想的富人的婚恋过程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 public class Rich : Society
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 public override void Meet()
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Console.WriteLine("用半年的时间调查对方的详细情况,见面这天签订婚姻合同,一个月后举行婚礼\n");
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 public override void Dinner()
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Console.WriteLine("18.8万元的天价年夜饭一份,先凑合一下吧!\n");
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 public override void Journey()
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Console.WriteLine("花2000万美元坐神舟8号到太空去看星星\n");
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 public override void Wedding()
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Console.WriteLine("克林顿作为特邀嘉宾主持,对全世界150个国家进行现场直播\n");
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 public override void House()
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Console.WriteLine("给小布什打个招呼,借用白宫一个月\n");
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 //虽然我很穷,但是我也要恋爱结婚
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 public class Poor : Society
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Console.WriteLine("见面之前只有对方的电话号码,在天桥底下见面,十五分钟啥也没打听出来\n");
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Console.WriteLine("两份肯德鸡套餐,花了五六十元,心疼啊!!!\n");
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Console.WriteLine("从公园后墙跳进去,里面的长椅可以免费坐,惊险刺激又节约\n");
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Console.WriteLine("只要认识的人一律发请帖,收了这么多年的罚款单,得连本代利赚回来!!\n");
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 Console.WriteLine("单位宿舍20来平米,暂时住住吧!媳妇,看看睡马路的那哥们,不错了!!!\n");
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 //外星人怎么恋爱结婚呢?让你来当一回编剧吧。。。。
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 public class ET : Society
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 string msg = "剧本还没有写好,请自行解决!";
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 throw new Exception(msg);
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 }
模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程 执行结果如下图:
本文转自左洸博客园博客,原文链接:http://www.cnblogs.com/myqiao/archive/2006/03/26/359478.html,如需转载请自行联系原作者