DesignPattern.Ch10.Template Method.Note

模版方法模式,定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。模版方法使得子类可以不改变算法的结构即可重定义算法的特定步骤。

菜鸟使用模版方法模拟学生做试卷:

Pattern.Template Method.Code
 1 namespace Pattern.TempleMethod.Newbie
 2 {
 3     ///学生试卷运用模版模式的实现
 4     class TestPaper
 5     {
 6         public void TestQuestion1()
 7         {
 8             Console.WriteLine("屠龙刀的玄铁可能是:[],a 球磨抽贴.b 马口铁。c 高速合金。d 碳素前卫");
 9             Console.WriteLine(Answer1());
10         }
11         public virtual string Answer1()
12         {
13             return "";
14         }
15 
16         public void TestQuestion2()
17         {
18             Console.WriteLine("屠龙刀的玄铁可能是2:[],a 球磨抽贴.b 马口铁。c 高速合金。d 碳素前卫");
19             Console.WriteLine(Answer1());
20         }
21         public virtual string Answer2()
22         {
23             return "";
24         }
25 
26         public void TestQuestion3()
27         {
28             Console.WriteLine("屠龙刀的玄铁可能是3:[],a 球磨抽贴.b 马口铁。c 高速合金。d 碳素前卫");
29             Console.WriteLine(Answer1());
30         }
31         public virtual string Answer3()
32         {
33             return "";
34         }
35     }
36 
37     //考生A的答卷
38     internal class TestPaperA : TestPaper
39     {
40         public override string Answer1()
41         {
42             return "A";
43         }
44         public override string Answer2()
45         {
46             return "B";
47         }
48         public override string Answer3()
49         {
50             return "C";
51         }
52     }
53 
54     //考生B的答卷
55     internal class TestPaperB : TestPaper
56     {
57         public override string Answer1()
58         {
59             return "A1";
60         }
61         public override string Answer2()
62         {
63             return "B1";
64         }
65         public override string Answer3()
66         {
67             return "C1";
68         }
69     }
70 }
Pattern.Template Method.Code
 1 namespace Pattern.TempleMethod.Primitive
 2 {
 3     abstract class AbstractClass
 4     {
 5         protected abstract void PrimitiveOperation1();
 6         protected abstract void PrimitiveOperation2();
 7         public void TemplateMethod()
 8         {
 9             this.PrimitiveOperation1();
10             this.PrimitiveOperation2();
11             Console.WriteLine();
12         }
13     }
14     internal class ConcreteClassA:AbstractClass
15     {
16         protected override void PrimitiveOperation1()
17         {
18             Console.WriteLine("具体a类的方法1的实现。");
19         }
20         protected override void PrimitiveOperation2()
21         {
22             Console.WriteLine("具体a类方法2的实现。");
23         }
24     }
25 
26     internal class ConcreteClassB : AbstractClass
27     {
28         protected override void PrimitiveOperation1()
29         {
30             Console.WriteLine("具体B类的方法1的实现。");
31         }
32         protected override void PrimitiveOperation2()
33         {
34             Console.WriteLine("具体B类方法2的实现。");
35         }
36     }
37 }

模版方法模式的优点

”模版方法的优点是把不变行为搬移到超类,去除子类中的重复代码来体现它的优势。” 模版方法提供了一个很好代码复用平台。

我们会遇到一系列步骤构成的过程需要执行,这个过程从高层看是相同的,但是有些步骤实现可能不同。这时,我们可以考虑模版方法模式了。

当可变和不可变的行为在方法的子类实现中混合在一起的时候,不变的行为就会在子类中重复的实现。我们可以通过模版方法把重复的行为搬移到一个单一的地方,这样有助于摆脱重复不变行为的纠结。

原文地址:https://www.cnblogs.com/lovey/p/2985637.html