模板模式 TemplatePattern

using System; using System.Collections.Generic; using System.Text; abstract class Robot { private string _Name; private string _NeedSomething; private string _Command; public Robot(string name, string needsomething) { _Name = name; _NeedSomething = needsomething; } private void NamingName() { Console.WriteLine("The Robot's name is " + _Name); } private void PerpareSomething() { Console.WriteLine("Perparing " + _NeedSomething + " now."); } protected abstract string AppointCommand(); private void ExecuteCommand() { Console.WriteLine(_Command + " is Executing."); } private void EndCommand() { Console.WriteLine(_Command + " is end."); } public void ExecuteRobot() { NamingName(); PerpareSomething(); _Command = AppointCommand(); ExecuteCommand(); EndCommand(); } }

模板

using System; class AutoCollectInformationRobot : Robot { private string _Command; public AutoCollectInformationRobot(string name, string something, string command) : base(name, something) { _Command = command; } protected override string AppointCommand() { return _Command; } }

由模板生成的对象A

using System; abstract class BombRobot { private string _Name; private string _NeedSomething; private string _Command; public BombRobot(string name, string needsomething) { _Name = name; _NeedSomething = needsomething; } private void NamingName() { Console.WriteLine("The Robot's name is " + _Name); } private void PerpareSomething() { Console.WriteLine("Perparing " + _NeedSomething + " now."); } protected abstract string AppointCommand(); private void ExecuteCommand() { Console.WriteLine(_Command + " is Executing."); } private void EndCommand() { Console.WriteLine(_Command + " is end."); } protected virtual bool BombOrNot() { return true; } public void ExecuteBomb() { NamingName(); PerpareSomething(); _Command = AppointCommand(); if (BombOrNot()) ExecuteCommand(); EndCommand(); } }

由模板生成的对象B

using System; class MailBombRobot : BombRobot { private string _Command; public MailBombRobot(string name, string needsomething, string command) : base(name, needsomething) { _Command = command; } protected override string AppointCommand() { return _Command; } protected override bool BombOrNot() { return false; } }

由模板生成的对象C

using System; using System.Collections.Generic; using System.Text; namespace TemplatePattern { class Program { static void Main(string[] args) { //Robot webRobot = new AutoCollectInformationRobot("WebInfomationCollecter", "Destination WEB Station", "Collect Information"); //webRobot.ExecuteRobot(); //Console.ReadKey(); BombRobot mbb = new MailBombRobot("MailBombRobot", "Be Bombed EMail Account", "Send A Mail Bomb"); mbb.ExecuteBomb(); Console.ReadKey(); } } }

调用者

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值