假设一个场景:每天骑自行车下班回家。我有两条路可以走,1:顺着经十路走,2:顺着经一路走。
好,我们有两个回家的方法
Code
1 public string GoHome()
2 {
3 string strResult = "";
4 strResult += TakeBike();//推自行车
5 strResult += "\r\n" + AlongJINGSHILU();//走经十路
6 strResult += "\r\n" + OpenDoor();//开门回家
7 return strResult;
8 }
9 public string GoHome()
10 {
11 string strResult = "";
12 strResult += TakeBike();//推自行车
13 strResult += "\r\n" + AlongJINGYILU();//走经一路
14 strResult += "\r\n" + OpenDoor();//开门回家
15 return strResult;
16 }
1 public string GoHome()
2 {
3 string strResult = "";
4 strResult += TakeBike();//推自行车
5 strResult += "\r\n" + AlongJINGSHILU();//走经十路
6 strResult += "\r\n" + OpenDoor();//开门回家
7 return strResult;
8 }
9 public string GoHome()
10 {
11 string strResult = "";
12 strResult += TakeBike();//推自行车
13 strResult += "\r\n" + AlongJINGYILU();//走经一路
14 strResult += "\r\n" + OpenDoor();//开门回家
15 return strResult;
16 }
看看上面两个方法,有啥不同呢?。哦除了走哪条路,其他的都是一样的
这样一样的方法我们抽象为模板方法,作为基类,走哪条路就让子类去做吧
Code
1namespace TemplateMethod
2{
3 public abstract class BaseClass
4 {
5 public BaseClass()
6 {
7
8 }
9 public string GoHome()//模板方法
10 {
11 string strResult = "";
12 strResult+= TakeBike();
13 if (hook())
14 {
15 strResult += "\r\n" + AlongRoad();
16 }
17 strResult += "\r\n" + OpenDoor();
18 return strResult;
19 }
20 public string TakeBike()
21 {
22 return "取自行车";
23 }
24 public virtual bool hook()
25 {
26 return true;
27 }
28 public abstract string AlongRoad();
29
30
31 public string OpenDoor()
32 {
33 return "开门回家";
34 }
35
36 }
37}
38
1namespace TemplateMethod
2{
3 public abstract class BaseClass
4 {
5 public BaseClass()
6 {
7
8 }
9 public string GoHome()//模板方法
10 {
11 string strResult = "";
12 strResult+= TakeBike();
13 if (hook())
14 {
15 strResult += "\r\n" + AlongRoad();
16 }
17 strResult += "\r\n" + OpenDoor();
18 return strResult;
19 }
20 public string TakeBike()
21 {
22 return "取自行车";
23 }
24 public virtual bool hook()
25 {
26 return true;
27 }
28 public abstract string AlongRoad();
29
30
31 public string OpenDoor()
32 {
33 return "开门回家";
34 }
35
36 }
37}
38
Code
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace TemplateMethod
7{
8 public class GoHomeWithJINGSHI:BaseClass
9 {
10 public GoHomeWithJINGSHI() { }
11 public override string AlongRoad()
12 {
13 return "我走经十路";
14 }
15 public override string ToString()
16 {
17 return base.ToString();
18 }
19 }
20}
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace TemplateMethod
7{
8 public class GoHomeWithJINGSHI:BaseClass
9 {
10 public GoHomeWithJINGSHI() { }
11 public override string AlongRoad()
12 {
13 return "我走经十路";
14 }
15 public override string ToString()
16 {
17 return base.ToString();
18 }
19 }
20}
Code
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace TemplateMethod
7{
8 public class GoHomeWithJINGYI:BaseClass
9 {
10 public GoHomeWithJINGYI() { }
11 public override string AlongRoad()
12 {
13 return "我走经一路";
14 }
15 public override bool hook()
16 {
17 return false;
18 }
19 }
20}
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace TemplateMethod
7{
8 public class GoHomeWithJINGYI:BaseClass
9 {
10 public GoHomeWithJINGYI() { }
11 public override string AlongRoad()
12 {
13 return "我走经一路";
14 }
15 public override bool hook()
16 {
17 return false;
18 }
19 }
20}
仔细看一下,代码中多了一个hook()方法,这个方法是做啥的呢?有什么好处呢。哦,原来作用是子类可以自己控制是否要执行AlongRoad()方法。假如我今天住朋友家,我只要推车子,开门就行了。(朋友家在车棚)。这个用在这个例子不太恰当,先这样吧