设计模式——策略模式

名称Strategy
结构 Strategy.gif
意图定义一系列的算法,把它们一个个封装起来, 并且使它们可相互替换。本模式使得算法可独立于使用它的客户而变化。
适用性
  • 许多相关的类仅仅是行为有异。“策略”提供了一种用多个行为中的一个行为来配置一个类的方法。
  • 需要使用一个算法的不同变体。例如,你可能会定义一些反映不同的空间/时间权衡的算法。当这些变体实现为一个算法的类层次时[ H O 8 7 ] ,可以使用策略模式。
  • 算法使用客户不应该知道的数据。可使用策略模式以避免暴露复杂的、与算法相关的数据结构。
  • 一个类定义了多种行为, 并且这些行为在这个类的操作中以多个条件语句的形式出现。将相关的条件分支移入它们各自的S t r a t e g y 类中以代替这些条件语句。
Code Example
 1 None.gif //  Strategy
 2 None.gif
 3 None.gif //  Intent: "Define a family of algorithms, encapsultate each one, and make 
 4 None.gif //  them interchangeable. Strategy lets the algorithm vary independently 
 5 None.gif //  from clients that use it." 
 6 None.gif
 7 None.gif //  For further information, read "Design Patterns", p315, Gamma et al.,
 8 None.gif //  Addison-Wesley, ISBN:0-201-63361-2
 9 None.gif
10 ExpandedBlockStart.gifContractedBlock.gif /**/ /* Notes:
11InBlock.gif * Ideal for creating exchangeable algorithms. 
12ExpandedBlockEnd.gif */

13 None.gif 
14 None.gif namespace  Strategy_DesignPattern
15 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
16InBlock.gif    using System;
17InBlock.gif
18InBlock.gif    
19InBlock.gif    abstract class Strategy 
20ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
21InBlock.gif        abstract public void DoAlgorithm();
22ExpandedSubBlockEnd.gif    }

23InBlock.gif
24InBlock.gif    class FirstStrategy : Strategy 
25ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
26InBlock.gif        override public void DoAlgorithm()
27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
28InBlock.gif            Console.WriteLine("In first strategy");            
29ExpandedSubBlockEnd.gif        }

30ExpandedSubBlockEnd.gif    }

31InBlock.gif
32InBlock.gif    class SecondStrategy : Strategy 
33ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
34InBlock.gif        override public void DoAlgorithm()
35ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
36InBlock.gif            Console.WriteLine("In second strategy");            
37ExpandedSubBlockEnd.gif        }

38ExpandedSubBlockEnd.gif    }

39InBlock.gif
40InBlock.gif    class Context 
41ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
42InBlock.gif        Strategy s;
43InBlock.gif        public Context(Strategy strat)
44ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
45InBlock.gif            s = strat;            
46ExpandedSubBlockEnd.gif        }

47InBlock.gif
48InBlock.gif        public void DoWork()
49ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
50InBlock.gif            // some of the context's own code goes here
51ExpandedSubBlockEnd.gif        }

52InBlock.gif
53InBlock.gif        public void DoStrategyWork()
54ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
55InBlock.gif            // now we can hand off to the strategy to do some 
56InBlock.gif            // more work
57InBlock.gif            s.DoAlgorithm();
58ExpandedSubBlockEnd.gif        }

59ExpandedSubBlockEnd.gif    }

60InBlock.gif
61ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
62InBlock.gif    ///    Summary description for Client.
63ExpandedSubBlockEnd.gif    /// </summary>

64InBlock.gif    public class Client
65ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
66InBlock.gif        public static int Main(string[] args)
67ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{    
68InBlock.gif            FirstStrategy firstStrategy = new FirstStrategy();
69InBlock.gif            Context c = new Context(firstStrategy);
70InBlock.gif            c.DoWork();
71InBlock.gif            c.DoStrategyWork();
72InBlock.gif
73InBlock.gif            return 0;
74ExpandedSubBlockEnd.gif        }

75ExpandedSubBlockEnd.gif    }

76ExpandedBlockEnd.gif}

77 None.gif
78 None.gif

转载于:https://www.cnblogs.com/DarkAngel/archive/2005/08/18/217265.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值