Strategy Pattern /Template Method Pattern

来源:互联网 发布:哇嘎网络连接失败 编辑:程序博客网 时间:2024/05/22 05:33
现在就来说说策略模式[Stragegy Pattern]/和模板方法模式[Template Method Pattern]:
 
几个原则:
  • Program to an interface, not an implementation [针对接口编程,而不要针对实现编程]

  • Favor object [aggregation] over class inheritance [优先选择组合,而不是继承]
  • Find what varies and encapsulate it封装变化]
     
    1、策略模式
    Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it
    [定义一组算法,对它们进行封装,从而使得他们可以相互替换。策略模式使得算法可以在不影响到使用它的客户端的情况下单独发生变化。]
     
     

    The Strategy pattern is based on a few principles:[策略模式基于以下几个原则:]

    • Objects have responsibilities.[对象具有特定的职责]

    • Different, specific implementations of these responsibilities are manifested through the use of polymorphism.[通过使用来多态表示这些职责的不同、特定实现]

    • There is a need to manage several different implementations of what is, conceptually, the same algorithm.[需要对同一算法的几个不同实现进行管理]

    UML图:

     

    Code:

    package strategy;

    /**
     * @role __Strategy
     */

    public interface Strategy {

     //算法接口
     void AlgorithmInterface();
    }

     

    package strategy;

    /**
     * Implements an algorithm using the Strategy interface.
     */

    public class ConcreteStrategyA implements stategy.Strategy {

     //具体算法实现
     public void AlgorithmInterface() {
      System.out.println("I'm ConcreteStrategyA.AlgorithmInterface();");
     }
    }

     

    package strategy;

    /**
     * Implements an algorithm using the Strategy interface.
     */

    public class ConcreteStrategyB implements stategy.Strategy {

    // 具体算法实现
     public void AlgorithmInterface() {
      System.out.println("I'm ConcreteStrategyB.AlgorithmInterface();");
     }
    }

     

    package strategy;

    /**
     * Implements an algorithm using the Strategy interface.
     */

    public class ConcreteStrategyC implements stategy.Strategy {

    // 具体算法实现
     public void AlgorithmInterface() {
      System.out.println("I'm ConcreteStrategyC.AlgorithmInterface();");
     }
    }

     

    package strategy;

    /**
     * @role __StrategyContext
     */

    public class Context {

     /**
      * @label Strategy
      * @link aggregation
      */

     private Strategy strategy;

     public Context(Strategy strategy) {
      this.strategy = strategy;
     }

     public void ContextInterface() {
      strategy.AlgorithmInterface();
     }

    }

     

    package strategy;

    public class Client {

     /*#stategy.Context Dependency_Link*/
     /* #stategy.Context Dependency_Link */
     /* #stategy.Context Dependency_Link */
     public static void Main(String[] args) {

      // 测试下几个不同的策略算法
      Context a = new Context(new ConcreteStrategyA());
      a.ContextInterface();

      Context b = new Context(new ConcreteStrategyB());
      b.ContextInterface();

      Context c = new Context(new ConcreteStrategyC());
      c.ContextInterface();
     }

    }

     

     2、模板方法模式
    Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Redefine the steps in an algorithm without changing the algorithm's structure
     
    [在一个操作中定义一个算法的框架,把具体的实现延迟到子类的某些步骤去实现,在不改变算法结构的情况下重新定义算法步骤]
     
    UML:
     
    Code:
     
    package templatemethod;
    /**
     * @role __TemplateContext
     */
    public abstract class AbstractClass {
     
     /**
      * Primitive operation
      */
     public abstract void primitiveOperation();
     /**
      * Primitive operation
      */
     public abstract int primitiveOperation1();
     /**
      * Defines the skeleton of an algorithm. Calls primitive
      operations as well as operations defined in AbstractClass
      or those in other objects.
      */
     public void templateMethod() {
      //something happens here...
      primitiveOperation();
      //...
      //later we need another step
      primitiveOperation1();
      //something more
     }
    }
     
    package templatemethod;
    /**
     * Implements the primitive operations to carry out
     *  subclass-specific steps of the algorithm.
     */
    public class ConcreteClass extends AbstractClass {
     public void primitiveOperation() {
      /* put implementation of particular step of template method here*/
      System.out.println("ConcreteClass.PrimitiveOperation1() is called;");
     }
     public int primitiveOperation1() {
      /* put implementation of particular step of template method here*/
      System.out.println("ConcreteClass.PrimitiveOperation2() is called;");
      return 0;
     }
    }
     
    package templatemethod;
    public class Client {
     /*#templatemethod.AbstractClass Dependency_Link*/
     public static void Main(String[] args) {
      
      // Create instance and call template method
      AbstractClass instance = null;
      instance = new ConcreteClass();
      instance.templateMethod();
     }
    }