设计模式——模板模式

名称Template Method
结构 Template.gif
意图定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。Te m p l a t e M e t h o d 使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。
适用性
  • 一次性实现一个算法的不变的部分,并将可变的行为留给子类来实现。
  • 各子类中公共的行为应被提取出来并集中到一个公共父类中以避免代码重复。这是O p d y k e 和J o h n s o n 所描述过的“重分解以一般化”的一个很好的例子[ O J 9 3 ]。首先识别现有代码中的不同之处,并且将不同之处分离为新的操作。最后,用一个调用这些新的操作的模板方法来替换这些不同的代码。
  • 控制子类扩展。模板方法只在特定点调用“h o o k ”操作(参见效果一节),这样就只允许在这些点进行扩展。
Code Example
 1 None.gif //  Template Method
 2 None.gif
 3 None.gif //  Intent: "Define the skeleton of an algorithm in an operation, deferring 
 4 None.gif //  some steps to subclasses. Template Method lets subclasses redefine 
 5 None.gif //  certain steps of an algorithm without changing the algorithm's structure." 
 6 None.gif
 7 None.gif //  For further information, read "Design Patterns", p325, Gamma et al.,
 8 None.gif //  Addison-Wesley, ISBN:0-201-63361-2
 9 None.gif
10 ExpandedBlockStart.gifContractedBlock.gif /**/ /* Notes:
11InBlock.gif * If you have an algorithm with multiple steps, and it could be helpful
12InBlock.gif * to make some of those steps replaceable, but no the entire algorithm, then 
13InBlock.gif * use the Template method. 
14InBlock.gif * 
15InBlock.gif * If the programming language in use supports generics / templates (C# does
16InBlock.gif * not), then they could be used here. It would be educational to take a 
17InBlock.gif * good look at the way algorithms in ISO C++'s STL work. 
18ExpandedBlockEnd.gif */

19 None.gif 
20 None.gif namespace  TemplateMethod_DesignPattern
21 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
22InBlock.gif    using System;
23InBlock.gif
24InBlock.gif    class Algorithm 
25ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
26InBlock.gif        public void DoAlgorithm() 
27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
28InBlock.gif            Console.WriteLine("In DoAlgorithm");
29InBlock.gif            
30InBlock.gif            // do some part of the algorithm here
31InBlock.gif            
32InBlock.gif            // step1 goes here
33InBlock.gif            Console.WriteLine("In Algorithm - DoAlgoStep1");            
34InBlock.gif            // . . . 
35InBlock.gif
36InBlock.gif            // step 2 goes here
37InBlock.gif            Console.WriteLine("In Algorithm - DoAlgoStep2");            
38InBlock.gif            // . . . 
39InBlock.gif
40InBlock.gif            // Now call configurable/replacable part
41InBlock.gif            DoAlgoStep3();
42InBlock.gif
43InBlock.gif            // step 4 goes here
44InBlock.gif            Console.WriteLine("In Algorithm - DoAlgoStep4");            
45InBlock.gif            // . . . 
46InBlock.gif
47InBlock.gif            // Now call next configurable part
48InBlock.gif            DoAlgoStep5();
49ExpandedSubBlockEnd.gif        }

50InBlock.gif
51InBlock.gif        virtual public void DoAlgoStep3()
52ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
53InBlock.gif            Console.WriteLine("In Algorithm - DoAlgoStep3");        
54ExpandedSubBlockEnd.gif        }

55InBlock.gif
56InBlock.gif        virtual public void DoAlgoStep5()
57ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
58InBlock.gif            Console.WriteLine("In Algorithm - DoAlgoStep5");            
59ExpandedSubBlockEnd.gif        }

60ExpandedSubBlockEnd.gif    }

61InBlock.gif
62InBlock.gif    class CustomAlgorithm : Algorithm
63ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
64InBlock.gif        public override void DoAlgoStep3()
65ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
66InBlock.gif            Console.WriteLine("In CustomAlgorithm - DoAlgoStep3");
67ExpandedSubBlockEnd.gif        }

68InBlock.gif
69InBlock.gif        public override void DoAlgoStep5()
70ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
71InBlock.gif            Console.WriteLine("In CustomAlgorithm - DoAlgoStep5");
72ExpandedSubBlockEnd.gif        }

73ExpandedSubBlockEnd.gif    }

74InBlock.gif
75ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
76InBlock.gif    ///    Summary description for Client.
77ExpandedSubBlockEnd.gif    /// </summary>

78InBlock.gif    public class Client
79ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
80InBlock.gif        public static int Main(string[] args)
81ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
82InBlock.gif            CustomAlgorithm c = new CustomAlgorithm();
83InBlock.gif
84InBlock.gif            c.DoAlgorithm();
85InBlock.gif
86InBlock.gif            return 0;
87ExpandedSubBlockEnd.gif        }

88ExpandedSubBlockEnd.gif    }

89ExpandedBlockEnd.gif}

90 None.gif

转载于:https://www.cnblogs.com/DarkAngel/archive/2005/08/02/205584.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值