`

策略模式

阅读更多
                       
Strategy pattern

Definition:
   The strategy pattern defines a family of algorithms,encapsulate each one,and makes them interchangeable.Strategy lets the algorithm vary independently from clients that use it.Whereby an algorithm's behaviour can be selected at runtime.

Intent
   Encapsulate what varys,and conform to the OCP principle.


   /**
    * The classes that implements a concrete strategy should implement this.
    * The context class uses this to call the concrete strategy.*/
    public interface Stragegy{
        int execute(int a,int b);
    }

    /** Implements the algorithm using the strategy interface*/
    class Add implements Strategy {
        public int execute(int a,int b){
            System.out.println("Called Add's execute()");
            return a+b;
        }
    }

     class Subtract implements Strategy{

          public int execute(int a,int b){
              system.out.println("Called from Subtract's execute()");
              return a-b;
          }
     }
     /**Configured with a ConcreteStrategy object and maintains a reference to a 
        strategy object
      */
     class Context{
          private Strategy strategy;
              
          public Context(Strategy strategy){
               this.strategy = strategy;
          }

          public int executeStrategy(int a,int b){
              return this.strategy.execute(a,b);
          }
     }

     /** Test the pattern*/
     class StrategyExample{
          public static void main(String[] args){
               Context context = new Context(new Add());
               int resultA = context.executeStrategy(3,4);
               context = new Context(new Subtract());
               int resultS = context.executeStrategy(3,4);
              
          }
     }


Usage
   validation strategy,sort strategy,and so on.

Applicability
    Use Strategy pattern when
1 Many related classes differ only in their behavior.Strategies provide a way to configure a class with one of many behaviors.

2 You need different variants of an algorithm.For example.,you might define algorithms reflecting different space/time trade-offs.Strategies can be used when these variants are implements as a class hierachy of algorithms.

3 an algorithm uses data that clients shouldn't know about.Use the Strategy pattern to avoid exposing complex,algorithm-specific data structures.

4 a class defines many behaviors,and these appears as multiple conditional statements in its operations.Instead of many conditionals,move related conditional branches into their own Strategy class.

Consequence
1 It provides an alternative for subclassing.
2 It eliminates conditional statement.

Drawbacks
1 Clients must be aware of different Strategies.The pattern has a potential drawback in that the client must understand how Strategies differ before it can select the appropriate one.Clients must be exposed to implementation issues.Therefore you should use the Strategy pattern only when the variation in behavior is relevant to clients.

2 Increased number of objects.
  • 大小: 14.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics