模版方法模式 Template Method Pattern — 穷人和富人的不同婚恋历程

简介:

说明:我也是初学者,希望大家能提出宝贵意见。另外转载请注明作者左洸和出处博客园,毕竟花费了很长时间才完成。

不管是穷人还是富人,都要谈恋爱结婚,而且每个人的婚恋经历,步骤大体上都是一样的,比如说:见面、吃饭、游玩、婚礼、婚房   等等,这些步骤是社会已经给我们安排好了的,他就像一个大纲、一个模版,作为社会中的一个成员,不管是穷是富,都只能按照步骤去做,谁也不能改变这些步骤,但是每个步骤的具体内容暂时还是抽象的,怎么见面,怎么吃饭,怎么游玩,婚礼怎么办,婚房是什么样的,一千个人可能有一千个情况,比如你穷,和女朋友吃饭一碗面条也能打发;如果你富有,山珍海味也是吃饭。这就是为什么大家都恋爱结婚,却有人欢喜有人忧啊。

按照一个已经列好的大纲,根据自己具体的情况,实现其中的抽象步骤,这就是模版方法模式。

点击浏览更多的“说故事、学模式”系列

下面让我们来看看,用C#语言怎么描述穷人和富人的不同的婚恋历程,这是一个典型的模版方法模式。

TemplateMethod.cs

 

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
namespace  Template
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
//***************************************************************
InBlock.gif
InBlock.gif    
//客户端
InBlock.gif
    class Client
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Society rich 
= new Rich();
InBlock.gif                Society poor 
= new Poor();
InBlock.gif                Society et 
= new ET();
InBlock.gif
InBlock.gif                Console.WriteLine(
"富人的婚恋过程。。。。。。。。\n");
InBlock.gif                Marry(rich);
InBlock.gif
InBlock.gif                Console.WriteLine();
InBlock.gif
InBlock.gif                Console.WriteLine(
"穷人的婚恋过程。。。。。。。。\n");
InBlock.gif                Marry(poor);
InBlock.gif
InBlock.gif                Console.WriteLine();
InBlock.gif
InBlock.gif                Console.WriteLine(
"外星人的婚恋过程。。。。。。。。\n");
InBlock.gif                Marry(et);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(e.Message);
InBlock.gif                Console.Read();
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
static public void Marry(Society society)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            society.Marry();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//******************************************************************
InBlock.gif
InBlock.gif    
//社会公认的婚恋过程,他是抽象的,只有步骤没有具体内容
InBlock.gif
    public abstract class Society
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
abstract public void Meet();    //第一次见面
InBlock.gif
        abstract public void Dinner();  //吃饭
InBlock.gif
        abstract public void Journey(); //游玩约会
InBlock.gif
        abstract public void Wedding(); //婚礼
InBlock.gif
        abstract public void House();   //婚房
InBlock.gif
InBlock.gif        
//每一个人都要经历的婚恋历程,需要做哪些事情社会已经替你安排好了
InBlock.gif        
//人在江湖身不由己,不管你是穷人还是富人你都无法改变这个过程
InBlock.gif        
//但是,具体实施的时候我们还是可以掌握着自己的命运
InBlock.gif        
//这里是恋爱结婚的模版方法
InBlock.gif
        public void Marry()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.Write(
"见面:");
InBlock.gif            Meet();
InBlock.gif
InBlock.gif            Console.Write(
"吃饭:");
InBlock.gif            Dinner();
InBlock.gif
InBlock.gif            Console.Write(
"游玩:");
InBlock.gif            Journey();
InBlock.gif
InBlock.gif            Console.Write(
"婚礼:");
InBlock.gif            Wedding();
InBlock.gif
InBlock.gif            Console.Write(
"婚房:");
InBlock.gif            House();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//******************************************************************
InBlock.gif
InBlock.gif    
//我希望自己是一个富人,下面是我设想的富人的婚恋过程
InBlock.gif
    public class Rich : Society
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public override void Meet()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"用半年的时间调查对方的详细情况,见面这天签订婚姻合同,一个月后举行婚礼\n");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void Dinner()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"18.8万元的天价年夜饭一份,先凑合一下吧!\n");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void Journey()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"花2000万美元坐神舟8号到太空去看星星\n");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void Wedding()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"克林顿作为特邀嘉宾主持,对全世界150个国家进行现场直播\n");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void House()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"给小布什打个招呼,借用白宫一个月\n");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//******************************************************************
InBlock.gif
InBlock.gif    
//虽然我很穷,但是我也要恋爱结婚
InBlock.gif
    public class Poor : Society
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public override void Meet()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"见面之前只有对方的电话号码,在天桥底下见面,十五分钟啥也没打听出来\n");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void Dinner()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"两份肯德鸡套餐,花了五六十元,心疼啊!!!\n");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void Journey()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"从公园后墙跳进去,里面的长椅可以免费坐,惊险刺激又节约\n");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void Wedding()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"只要认识的人一律发请帖,收了这么多年的罚款单,得连本代利赚回来!!\n");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void House()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"单位宿舍20来平米,暂时住住吧!媳妇,看看睡马路的那哥们,不错了!!!\n");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//******************************************************************
InBlock.gif
InBlock.gif    
//外星人怎么恋爱结婚呢?让你来当一回编剧吧。。。。
InBlock.gif
    public class ET : Society
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string msg = "剧本还没有写好,请自行解决!";
InBlock.gif
InBlock.gif        
public override void Meet()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new Exception(msg);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void Dinner()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new Exception(msg);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void Journey()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new Exception(msg);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void Wedding()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new Exception(msg);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void House()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new Exception(msg);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

执行结果如下图:

 


本文转自左洸博客园博客,原文链接:http://www.cnblogs.com/myqiao/archive/2006/03/26/359478.html,如需转载请自行联系原作者


目录
相关文章
|
4月前
|
设计模式 缓存
二十三种设计模式全面解析-代理模式(Proxy Pattern)详解:探索隐藏于背后的力量
二十三种设计模式全面解析-代理模式(Proxy Pattern)详解:探索隐藏于背后的力量
|
4月前
|
设计模式
二十三种设计模式全面解析-原型模式(Prototype Pattern)详解:创造对象的奇妙之道
二十三种设计模式全面解析-原型模式(Prototype Pattern)详解:创造对象的奇妙之道
|
11月前
|
程序员
祖传shi山代码重构实战(01)-Extract Class提炼类
某个类做了应该由两个类做的事。 建立一个新类,将相关的字段和函数从旧类移到新类。
117 0
|
JavaScript 前端开发 计算机视觉
由一篇ES6-Class科普文章引发的“惨案”
让我们就开始惨案的解密过程吧。 首先,我们不按常规去思考上面的疑惑,我们需要在破案之前,需要一些准备工具。 首先让人很刺眼的一个是:Object.defineProperty。既然遇到了,我们就来会会他。
|
Java Spring 容器
面试官:@Configuration 和 @Component 注解的区别?大部分人都会答错!
面试官:@Configuration 和 @Component 注解的区别?大部分人都会答错!
103 0
面试官:@Configuration 和 @Component 注解的区别?大部分人都会答错!
|
Serverless C语言 Python
学编程这么久,还傻傻分不清什么是方法(method),什么是函数(function)?
在标准库inspect 中,它提供了两个自省的函数,即 ismethod() 和 isfunction(),可以用来判断什么是方法,什么是函数。
269 0
学编程这么久,还傻傻分不清什么是方法(method),什么是函数(function)?
冇事来学系--Vue2.0中命名路由和路由的query和params
命名路由 在设置路由规则的时候,可以给路由指定一个name属性。 作用:可以简化路由的跳转
116 0
|
存储 JavaScript 前端开发
冇事来学系--Vue2.0中ref引用
jQuery相比于原生JS,牛逼在简化了操作DOM的过程 vue的优势:MVVM框架,在vue中,不需要操作DOM,程序员只需要把数据维护好即可(数据驱动视图) 假设:在vue中需要操作DOM,需要获取到页面上某个DOM元素的引用,该如何?---> 使用ref引用
149 0
好客租房65-render-props模式-1思路分析
好客租房65-render-props模式-1思路分析
65 0
|
前端开发
好客租房27-state的基本使用
好客租房27-state的基本使用
69 0