策略模式之GOF
?
如图所示,在Duck基类里实现了公共的quack()和swim()方法,而MallardDuck和RedheadDuck可以分别覆盖实现自己的display()方法,这样即重用了公共的部分,又支持不同子类的个性化扩展。从目前的情况看,这是一个很好的设计,哈!
??
??? Joe的上司很高兴,带着新产品给董事们演示去了……??
Joe的上司知道后怒了:“你这样做难道是希望所有需要quack()和fly()方法的鸭子都去重复实现这两个方法的功能吗?就这么几个鸭子还好说,但是我们有几十、上百个鸭子的时候你怎么办?如果某个方法要做一点修改,难道你要重复修改上百遍吗?你是不是疯啦?”
Joe已经晕了,“你说了这么多,全是大白话,来点代码行不行,我要C#的!”。说到这里,我们也该开始彻底改造这个设计了,并会在最后附加部分代码来帮助大家理解。
??我们再看看整个设计修改后的类图:
最后大家再看看演示代码,因为代码比较多,就不贴出来了,大家可以下载后参考:
。下面是演示代码的执行结果:

Context(应用场景):
using?System;
?2
using?System.Collections;
?3
?4
public?class?SamplesArrayList??{
?5
?
?6
???public?class?myReverserClass?:?IComparer??{
?7
?8
??????//?Calls?CaseInsensitiveComparer.Compare?with?the?parameters?reversed.
?9
??????int?IComparer.Compare(?Object?x,?Object?y?)??{
10
??????????return(?(new?CaseInsensitiveComparer()).Compare(?y,?x?)?);
11
??????}
12
13
???}
14
15
???public?static?void?Main()??{
16
?
17
??????//?Creates?and?initializes?a?new?ArrayList.
18
??????ArrayList?myAL?=?new?ArrayList();
19
??????myAL.Add(?"The"?);
20
??????myAL.Add(?"quick"?);
21
??????myAL.Add(?"brown"?);
22
??????myAL.Add(?"fox"?);
23
??????myAL.Add(?"jumps"?);
24
??????myAL.Add(?"over"?);
25
??????myAL.Add(?"the"?);
26
??????myAL.Add(?"lazy"?);
27
??????myAL.Add(?"dog"?);
28
?
29
??????//?Displays?the?values?of?the?ArrayList.
30
??????Console.WriteLine(?"The?ArrayList?initially?contains?the?following?values:"?);
31
??????PrintIndexAndValues(?myAL?);
32
?
33
??????//?Sorts?the?values?of?the?ArrayList?using?the?default?comparer.
34
??????myAL.Sort();
35
??????Console.WriteLine(?"After?sorting?with?the?default?comparer:"?);
36
??????PrintIndexAndValues(?myAL?);
37
38
??????//?Sorts?the?values?of?the?ArrayList?using?the?reverse?case-insensitive?comparer.
39
??????IComparer?myComparer?=?new?myReverserClass();
40
??????myAL.Sort(?myComparer?);
41
??????Console.WriteLine(?"After?sorting?with?the?reverse?case-insensitive?comparer:"?);
42
??????PrintIndexAndValues(?myAL?);
43
44
???}
45
?
46
???public?static?void?PrintIndexAndValues(?IEnumerable?myList?)??{
47
??????int?i?=?0;
48
??????foreach?(?Object?obj?in?myList?)
49
?????????Console.WriteLine(?"\t[{0}]:\t{1}",?i++,?obj?);
50
??????Console.WriteLine();
51
???}
52
53
}
54
55
56
/*?
57
This?code?produces?the?following?output.
58
The?ArrayList?initially?contains?the?following?values:
59
????????[0]:????The
60
????????[1]:????quick
61
????????[2]:????brown
62
????????[3]:????fox
63
????????[4]:????jumps
64
????????[5]:????over
65
????????[6]:????the
66
????????[7]:????lazy
67
????????[8]:????dog
68
69
After?sorting?with?the?default?comparer:
70
????????[0]:????brown
71
????????[1]:????dog
72
????????[2]:????fox
73
????????[3]:????jumps
74
????????[4]:????lazy
75
????????[5]:????over
76
????????[6]:????quick
77
????????[7]:????the
78
????????[8]:????The
79
80
After?sorting?with?the?reverse?case-insensitive?comparer:
81
????????[0]:????the
82
????????[1]:????The
83
????????[2]:????quick
84
????????[3]:????over
85
????????[4]:????lazy
86
????????[5]:????jumps
87
????????[6]:????fox
88
????????[7]:????dog
89
????????[8]:????brown?
90
*/
关于组合和继承,我们只要这样来理解即可:组合是一种“HAS-A”关系,而继承是一种“IS-A”关系。很明显“HAS-A”要比“IS-A”更灵活一些。也就是说在创建系统的时候,我们应该优先使用对象组合,因为它不仅可以给你提供更多灵活性和扩展性,而且还使你可以在运行时改变行为(组合不同的对象),这简直是酷毙了!但是也不是说继承就是不能用,只是说应该把继承应用在相对更稳定,几乎没有变化的地方,例如前面的Duck类里的Swim()方法,因为可以肯定所有鸭子一定都会游泳,所以就没有必要给这个行为提供基于Strategy模式的实现方式,因为那样做除了是程序更复杂以外,没有什么意义。
转载自 : http://www.cnblogs.com/justinw/archive/2007/02/06/641414.html