Template Method
the template defines the bone of the algorithm, something like how the system works, and encapsulates the common things together as a template, Actually the system works step by step. for some situation the order of the steps is stable and never changed. but the steps themselves are different according to the different situation. different client. So how we make those changing steps together.
OK, that 's why we need this parttern, the answer is let the Concrete Type to implement the step(We call the step Primitive Operation) by inheriting the abstracted class where the template method is implemented.
Actrually the mechanisms of the OOP play the most important role in this PD.
Below is a small example full of coments in bold which describe the code and this PD detailedly, so i don't wanna write any more word but just show the code here.
1 public abstract class AbstractClass
2 {
3 /// <summary>
4 /// This is the template, where it defines how the algorithms works,
5 /// it encapsulates the common code here for the code resue
6 /// </summary>
7 public void TemplateMethod()
8 {
9 PrimitiveMethod1();
10 PrimitiveMethod2();
11 }
12
13 /// <summary>
14 /// it is the primitives that compose the template, but they work differently
15 /// under the different situations, different clienst. So they should be implementd
16 /// by the differnt concrete type according to the situation.
17 /// They are part of the template
18 /// </summary>
19 protected abstract void PrimitiveMethod1();
20 //here should be protected, only for the inherited types
21 protected abstract void PrimitiveMethod2();
22 }
23
24 /// <summary>
25 /// One concrete class represents a special requirement
26 /// for the primitive method
27 /// </summary>
28 public class ConcreteClass1:AbstractClass
29 {
30 protected override void PrimitiveMethod1()
31 {
32 //own logical
33 }
34
35 protected override void PrimitiveMethod2()
36 {
37 //own logical
38 }
39 }
40 /// <summary>
41 /// another special requirement
42 /// </summary>
43 public class ConcreteClass2 : AbstractClass
44 {
45 protected override void PrimitiveMethod1()
46 {
47 //own logical
48 }
49
50 protected override void PrimitiveMethod2()
51 {
52 //own logical
53 }
54 }
55
56 /// <summary>
57 /// All the client cares about is the TemplateMethod,
58 /// the client just needs to initialize the Concrete type as the own reqirement
59 ///
60 /// </summary>
61 public class MyClient
62 {
63 static void Main(string[] args)
64 {
65 AbstractClass ac = null;
66
67 ac = new ConcreteClass1();
68
69 ac.TemplateMethod();
70
71
72 ac = new ConcreteClass2();
73
74 ac.TemplateMethod();
75 }
76 }
The Template Method is a little similar to the Strategy discussed yesterday evening, But they are different.
- The Strategy lets the concrete type re-implement all the algorithms. all are changed, the concrete type is a very special Strategy, they are replaceable.
- Template Method encapsulates the algorithms and holds thems, just lets the concrete type implement part of them ,especially the detaials for the different situation.
- They can work together
Done here