设计模式之模板设计模式
来源:互联网 发布:mysql md5函数加密 编辑:程序博客网 时间:2024/05/08 18:20
模板方法模式,定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重新定义该算法的某些特定步骤。
模板方法模式是通过把不变行为搬到超类,去除子类中的重复代码来体现它的优势。
当不变和可变的行为在方法的子类实现中混合在一起的时候,不变的行为就会在子类中重复出现。我们通过模板方法模式把这些行为搬到单一的地方,这样就帮助子类摆脱重复的不变行为的纠缠。(转至大话设计模式)
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 模板方法模式{ class Exam { public void Test01() { Console.WriteLine("第一题:"); Console.WriteLine("答案:"+answer1()); } public virtual string answer1() { return "error"; } public void Test02() { Console.WriteLine("第二题:"); Console.WriteLine("答案:"+answer2()); } public virtual string answer2() { return "error"; } } class StuA:Exam { public override string answer2() { return "A"; } public override string answer1() { return "A"; } } class StuB:Exam { public override string answer2() { return "B"; } public override string answer1() { return "B"; } } class Program { static void Main(string[] args) { Exam stuA = new StuA(); stuA.Test01(); Exam stuB = new StuB(); stuB.Test02(); Console.ReadKey(); } }}
0 0
- 设计模式之模板模式
- 设计模式之模板模式
- 设计模式之模板模式
- 设计模式之模板模式
- 设计模式之模板模式
- 设计模式之模板模式
- 设计模式之模板模式
- 设计模式之 - 模板模式
- 设计模式之模板模式
- 设计模式之模板模式
- 设计模式之模板模式
- 设计模式之模板模式
- 设计模式之模板模式
- 设计模式之模板模式
- 设计模式之模板模式
- 设计模式之模板模式
- 设计模式之模板模式
- 设计模式之模板模式
- 网站链接svn
- 算法基础 - 树的最近公共祖先
- Linux下使用nexus搭建maven私服
- 【C语言】实现一个通讯录(二:动态的实现)
- UVA 10976 Fractions Again?!
- 设计模式之模板设计模式
- MyEclipse Trial Expired 问题及解决方案
- OO的五大原则: Solid
- 内容提供者ContentProvider和内容解析者ContentResolver
- 5月份FPGA总结.
- PPT中如何对齐多个对象
- 49.表示数值的字符串
- RHEL7内核模块的操作和使Linux系统支持ntfs读写
- static auto extern register变量存储类型