首页 > 分享 > 模板模式 TemplatePattern

模板模式 TemplatePattern

最新推荐文章于 2018-09-27 18:31:06 发布

iteye_20785 于 2010-11-27 16:48:00 发布

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(); } } }

调用者

相关知识

模板模式 TemplatePattern
PR模板
观赏鱼详情页模板
吉林商城模板app开发
宠物用品节日促销模板
宠物海报模板图片
行为治疗模式
知库|CrossFit训练计划制定的理论模板
Java设计模式——行为型模式
肠道详情页模板

网址: 模板模式 TemplatePattern https://m.mcbbbk.com/newsview464013.html

所属分类:萌宠日常
上一篇: 电子手表用户界面设计研究
下一篇: 学龄前儿童餐具形态设计研究