<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Chan Chen Coding...

    Two: Strategy pattern

    Strategy pattern

     

    In computer programming, the strategy pattern (also known as the policy pattern) is a particular software design pattern, whereby algorithms can be selected at runtime. Formally speaking, the strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.[1]

    For instance, a class that performs validation on incoming data may use a strategy pattern to select a validation algorithm based on the type of data, the source of the data, user choice, and/or other discriminating factors. These factors are not known for each case until run-time, and may require radically different validation to be performed. The validation strategies, encapsulated separately from the validating object, may be used by other validating objects in different areas of the system (or even different systems) without code duplication.

    The essential requirement in the programming language is the ability to store a reference to some code in a data structure and retrieve it. This can be achieved by mechanisms such as the native function pointer, the first-class function, classes or class instances in object-oriented programming languages, or accessing the language implementation's internal storage of code via reflection.


    Example

    The following example is in Java.

    // The classes that implement a concrete strategy should implement this. // The Context class uses this to call the concrete strategy.

    interface Strategy {

         int execute(int a, int b);

     }  

    // Implements the algorithm using the strategy interface

    class ConcreteStrategyAdd implements Strategy {

           public int execute(int a, int b) {

           System.out.println("Called ConcreteStrategyAdd's execute()");

           return a + b; 

    // Do an addition with a and b

     }

     }  

     

    class ConcreteStrategySubtract implements Strategy {

           public int execute(int a, int b) {

             System.out.println("Called ConcreteStrategySubtract's execute()");

             return a - b;

     // Do a subtraction with a and b

         }

     }

    class ConcreteStrategyMultiply implements Strategy {

           public int execute(int a, int b) {

             System.out.println("Called ConcreteStrategyMultiply's execute()");

             return a * b;

         // Do a multiplication with a and b

    }

    }  

    // Configured with a ConcreteStrategy object and maintains a reference to a Strategy object

    class Context {

           private Strategy strategy;

           // Constructor

        public Context(Strategy strategy) {

             this.strategy = strategy;

         }

     

         public int executeStrategy(int a, int b) {

             return strategy.execute(a, b);

         }

     }

     // Test application class StrategyExample {

           public static void main(String[] args) {

               Context context;

               // Three contexts following different strategies

             context = new Context(new ConcreteStrategyAdd());

             int resultA = context.executeStrategy(3,4);

             context = new Context(new ConcreteStrategySubtract());

             int resultB = context.executeStrategy(3,4);

             context = new Context(new ConcreteStrategyMultiply());

             int resultC = context.executeStrategy(3,4);

         }

     }

    Strategy versus Bridge

    The UML class diagram for the strategy pattern is the same[further explanation needed] as the diagram for the Bridge pattern. However, these two design patterns aren't the same in their intent. While the strategy pattern is meant for behavior, the Bridge pattern is meant for structure.

    The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.

    Strategy and open/closed principle

    http://upload.wikimedia.org/wikipedia/commons/thumb/4/4b/StrategyPattern_IBrakeBehavior.svg/220px-StrategyPattern_IBrakeBehavior.svg.png

    Accelerate and brake behaviors must be declared in each new car model

    According to the strategy pattern, the behaviors of a class should not be inherited, instead they should be encapsulated using interfaces. As an example, consider a car class. Two possible functionalities for car are brake and accelerate.

    Since accelerate and brake behaviors change frequently between models, a common approach is to implement these behaviors in subclasses. This approach has significant drawbacks: accelerate and brake behaviors must be declared in each new Car model. The work of managing these behaviors increases greatly as the number of models increases, and requires code to be duplicated across models. Additionally, it is not easy to determine the exact nature of the behavior for each model without investigating the code in each.

    The strategy pattern uses aggregation instead of inheritance. In the strategy pattern behaviors are defined as separate interfaces and specific classes that implement these interfaces. Specific classes encapsulate these interfaces. This allows better decoupling between the behavior and the class that uses the behavior. The behavior can be changed without breaking the classes that use it, and the classes can switch between behaviors by changing the specific implementation used without requiring any significant code changes. Behaviors can also be changed at run-time as well as at design-time. For instance, a car object’s brake behavior can be changed from BrakeWithABS() to Brake() by changing the brakeBehavior member to:

    brakeBehavior = new Brake();

    This gives greater flexibility in design and is in harmony with the Open/closed principle (OCP) that states that classes should be open for extension but closed for modification.

     



    -----------------------------------------------------
    Silence, the way to avoid many problems;
    Smile, the way to solve many problems;

    posted on 2012-06-26 05:31 Chan Chen 閱讀(283) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Design Pattern

    主站蜘蛛池模板: 国产卡一卡二卡三免费入口 | 夜色阁亚洲一区二区三区| 亚洲视频2020| 暖暖免费日本在线中文| 久久精品国产亚洲AV果冻传媒| 久青草视频在线观看免费| 永久看日本大片免费35分钟| 亚洲精品私拍国产福利在线| 久久水蜜桃亚洲AV无码精品| 中文字幕无码免费久久| 卡一卡二卡三在线入口免费| 国产成人精品日本亚洲| 日本不卡免费新一区二区三区| 久久精品免费全国观看国产| 久久综合亚洲色HEZYO国产| XXX2高清在线观看免费视频| 成人免费在线看片| 国产亚洲精品高清在线| 国产无遮挡又黄又爽免费网站| 国产免费av片在线看| 朝桐光亚洲专区在线中文字幕 | 亚洲 日韩 色 图网站| 女人18一级毛片免费观看| 黄网站在线播放视频免费观看| 狠狠综合久久综合88亚洲| 日韩精品无码免费一区二区三区| 亚洲欧洲国产成人精品| 国产精品视_精品国产免费| 亚洲国产精品综合一区在线| 免费人成激情视频在线观看冫| 337p日本欧洲亚洲大胆精品555588| 男女超爽视频免费播放| 亚洲综合熟女久久久30p| AV激情亚洲男人的天堂国语| 亚洲自偷自偷图片| 97国产免费全部免费观看| 午夜不卡AV免费| 亚洲欧洲精品一区二区三区| 久久er国产精品免费观看2| 亚洲国产精品线观看不卡 | 亚洲乱码国产一区三区|