Template Method 在一个方法中实现一个算法,但是推迟定义算法中的某些步骤,从而使其他的类可以重新定义这些步骤
一个经典实例 - 排序
自定义类
1using System;
2
3namespace Gof.Test.TemplateMethod
4{
5 public class User
6 {
7 public User(string name,int age)
8 {
9 _name = name;
10 _age = age;
11 }
12 public string Name
13 {
14 get
15 {
16 return _name;
17 }
18 set
19 {
20 _name = value;
21 }
22 }
23 private string _name;
24 public int Age
25 {
26 get
27 {
28 return _age;
29 }
30 set
31 {
32 _age = value;
33 }
34 }
35 private int _age;
36
37 }
38}
1using System;
2
3namespace Gof.Test.TemplateMethod
4{
5 public class User
6 {
7 public User(string name,int age)
8 {
9 _name = name;
10 _age = age;
11 }
12 public string Name
13 {
14 get
15 {
16 return _name;
17 }
18 set
19 {
20 _name = value;
21 }
22 }
23 private string _name;
24 public int Age
25 {
26 get
27 {
28 return _age;
29 }
30 set
31 {
32 _age = value;
33 }
34 }
35 private int _age;
36
37 }
38}
自定义排序类
1 private class MyCustomCompare:System.Collections.IComparer
2 {
3 IComparer 成员#region IComparer 成员
4
5 public int Compare(object x, object y)
6 {
7 User userx = (User)x;
8 User usery = (User)y;
9 int result = 0;
10 if(userx.Age > usery.Age)
11 {
12 result = -1;
13 }
14 if(userx.Age == usery.Age)
15 {
16 result = 0;
17 }
18 if(userx.Age < usery.Age)
19 {
20 result = 1;
21 }
22 return result;
23 }
24
25 #endregion
26 }
1 private class MyCustomCompare:System.Collections.IComparer
2 {
3 IComparer 成员#region IComparer 成员
4
5 public int Compare(object x, object y)
6 {
7 User userx = (User)x;
8 User usery = (User)y;
9 int result = 0;
10 if(userx.Age > usery.Age)
11 {
12 result = -1;
13 }
14 if(userx.Age == usery.Age)
15 {
16 result = 0;
17 }
18 if(userx.Age < usery.Age)
19 {
20 result = 1;
21 }
22 return result;
23 }
24
25 #endregion
26 }
客户代码
1 User user = new User("nan",20);
2 User user2 = new User("shou",19);
3 User user3 = new User("yong",18);
4 User[] users = new User[]{user,user2,user3};
5 Array.Sort(users,new MyCustomCompare());
6 foreach(User u in users)
7 {
8 Console.WriteLine(u.Name);
9 }
10 Console.ReadLine();
重构为模板1 User user = new User("nan",20);
2 User user2 = new User("shou",19);
3 User user3 = new User("yong",18);
4 User[] users = new User[]{user,user2,user3};
5 Array.Sort(users,new MyCustomCompare());
6 foreach(User u in users)
7 {
8 Console.WriteLine(u.Name);
9 }
10 Console.ReadLine();
Machine类层次
1using System;
2using Gof.Test.Factory.Plan;
3
4namespace Gof.Test.Factory.Machines
5{
6 public class Machine
7 {
8 public Machine()
9 {
10 }
11 public bool HasDone
12 {
13 get
14 {
15 return _hasDone;
16 }
17 set
18 {
19 _hasDone = value;
20 }
21 }private bool _hasDone;
22
23 public virtual MachinePlanner CreatePlanner()
24 {
25 return new BasicPlanner(this);
26 }
27 public MachinePlanner GetPlanner()
28 {
29 if(_planner == null)
30 {
31 _planner = CreatePlanner();//这里是多态的
32 }
33 return _planner;
34 }private MachinePlanner _planner; //抽象出来,形成模板
35 }
36}
1using System;
2using Gof.Test.Factory.Plan;
3
4namespace Gof.Test.Factory.Machines
5{
6 public class Machine
7 {
8 public Machine()
9 {
10 }
11 public bool HasDone
12 {
13 get
14 {
15 return _hasDone;
16 }
17 set
18 {
19 _hasDone = value;
20 }
21 }private bool _hasDone;
22
23 public virtual MachinePlanner CreatePlanner()
24 {
25 return new BasicPlanner(this);
26 }
27 public MachinePlanner GetPlanner()
28 {
29 if(_planner == null)
30 {
31 _planner = CreatePlanner();//这里是多态的
32 }
33 return _planner;
34 }private MachinePlanner _planner; //抽象出来,形成模板
35 }
36}
Fuser
1using System;
2using Gof.Test.Factory.Plan;
3
4namespace Gof.Test.Factory.Machines
5{
6 public class Fuser:Machine
7 {
8 public Fuser()
9 {
10 }
11 public override MachinePlanner CreatePlanner()
12 {
13 return new FuserPlanner(this);
14 }
15// private MachinePlanner GetPlanner()
16// {
17// if(_planner == null)
18// {
19// _planner = new FuserPlanner(this);
20// }
21// return _planner;
22// }private MachinePlanner _planner;//重构掉了
23 }
24}
25
1using System;
2using Gof.Test.Factory.Plan;
3
4namespace Gof.Test.Factory.Machines
5{
6 public class Fuser:Machine
7 {
8 public Fuser()
9 {
10 }
11 public override MachinePlanner CreatePlanner()
12 {
13 return new FuserPlanner(this);
14 }
15// private MachinePlanner GetPlanner()
16// {
17// if(_planner == null)
18// {
19// _planner = new FuserPlanner(this);
20// }
21// return _planner;
22// }private MachinePlanner _planner;//重构掉了
23 }
24}
25
Mixer
1using System;
2using Gof.Test.Factory.Plan;
3
4
5namespace Gof.Test.Factory.Machines
6{
7 public class Mixer : Machine
8 {
9 public Mixer()
10 {
11 }
12 public override MachinePlanner CreatePlanner()
13 {
14 return new MixerPlanner(this);
15 }
16// private MachinePlanner GetPlanner()
17// {
18// if(_planner == null)
19// {
20// _planner = new MixerPlanner(this);
21// }
22// return _planner;
23// }private MachinePlanner _planner;
24 }
25}
1using System;
2using Gof.Test.Factory.Plan;
3
4
5namespace Gof.Test.Factory.Machines
6{
7 public class Mixer : Machine
8 {
9 public Mixer()
10 {
11 }
12 public override MachinePlanner CreatePlanner()
13 {
14 return new MixerPlanner(this);
15 }
16// private MachinePlanner GetPlanner()
17// {
18// if(_planner == null)
19// {
20// _planner = new MixerPlanner(this);
21// }
22// return _planner;
23// }private MachinePlanner _planner;
24 }
25}
MachinePlanner类层次
1using System;
2using Gof.Test.Factory.Machines;
3
4namespace Gof.Test.Factory.Plan
5{
6 public abstract class MachinePlanner
7 {
8 public MachinePlanner(Machine machine)
9 {
10 _machine = machine;
11 }
12 public Machine Machine
13 {
14 get
15 {
16 return _machine;
17 }
18 }
19 private Machine _machine;
20 public abstract bool ContineWork
21 {
22 get;
23 }
24 }
25}
1using System;
2using Gof.Test.Factory.Machines;
3
4namespace Gof.Test.Factory.Plan
5{
6 public abstract class MachinePlanner
7 {
8 public MachinePlanner(Machine machine)
9 {
10 _machine = machine;
11 }
12 public Machine Machine
13 {
14 get
15 {
16 return _machine;
17 }
18 }
19 private Machine _machine;
20 public abstract bool ContineWork
21 {
22 get;
23 }
24 }
25}
BasicPlanner
1using System;
2using Gof.Test.Factory.Machines;
3
4namespace Gof.Test.Factory.Plan
5{
6 public class BasicPlanner:MachinePlanner
7 {
8 public BasicPlanner(Machine machine) : base(machine)
9 {
10 }
11 public override bool ContineWork
12 {
13 get
14 {
15 return Machine.HasDone;
16 }
17 }
18 }
19}
1using System;
2using Gof.Test.Factory.Machines;
3
4namespace Gof.Test.Factory.Plan
5{
6 public class BasicPlanner:MachinePlanner
7 {
8 public BasicPlanner(Machine machine) : base(machine)
9 {
10 }
11 public override bool ContineWork
12 {
13 get
14 {
15 return Machine.HasDone;
16 }
17 }
18 }
19}
FuserPlanner
1using System;
2using Gof.Test.Factory.Machines;
3
4namespace Gof.Test.Factory.Plan
5{
6 /**//// <summary>
7 /// FuserPlanner 的摘要说明。
8 /// </summary>
9 public class FuserPlanner:MachinePlanner
10 {
11 public FuserPlanner(Machine machine):base(machine)
12 {
13 }
14 public override bool ContineWork
15 {
16 get
17 {
18 return Machine.HasDone;
19 }
20 }
21 }
22}
1using System;
2using Gof.Test.Factory.Machines;
3
4namespace Gof.Test.Factory.Plan
5{
6 /**//// <summary>
7 /// FuserPlanner 的摘要说明。
8 /// </summary>
9 public class FuserPlanner:MachinePlanner
10 {
11 public FuserPlanner(Machine machine):base(machine)
12 {
13 }
14 public override bool ContineWork
15 {
16 get
17 {
18 return Machine.HasDone;
19 }
20 }
21 }
22}
MixerPlanner
1using System;
2using Gof.Test.Factory.Machines;
3
4namespace Gof.Test.Factory.Plan
5{
6 public class MixerPlanner : MachinePlanner
7 {
8 public MixerPlanner(Machine machine) : base(machine)
9 {
10 }
11 public override bool ContineWork
12 {
13 get
14 {
15 return Machine.HasDone;
16 }
17 }
18
19 }
20}
The Template Method Pattern defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
1using System;
2using Gof.Test.Factory.Machines;
3
4namespace Gof.Test.Factory.Plan
5{
6 public class MixerPlanner : MachinePlanner
7 {
8 public MixerPlanner(Machine machine) : base(machine)
9 {
10 }
11 public override bool ContineWork
12 {
13 get
14 {
15 return Machine.HasDone;
16 }
17 }
18
19 }
20}