Learning Design Patterns - Strategy Pattern

Strategy Mode

First, the definition

   Defines a set of algorithms, each algorithm are packaged together, and so that they can be converted to each other before. (Define a family of algorithms, encapsulate each one, and make them interchangeable.)

 

 Second, understanding

   Separate extraction behavior change alone, the definition of the respective interface, and encapsulates the corresponding behavior, so that they can replace each other.

  

  1. Context context role

    Since the package connecting role, shielding direct access to the high-level strategy Dian algorithm, the package may change

  2. Strategy Strategy Interface

    Define a policy interface

  3. ConreteStrategy specific strategies

     Implementation strategy interface to encapsulate specific algorithm

 

 Third, for example

  

  3.1) interface to define policies

/**
 * 策略
 * @author TimFruit
 * @date 2019/5/21 7:40
 */

public interface IAlgorithm {

    int doOperate(int num1, int num2);
    
}

   

  3.2) Interface Implementation Strategy

public class Add implements IAlgorithm {
    @Override public int doOperate(int num1, int num2) {
        return num1+num2;
    }
}

 

public class Multiply implements IAlgorithm {
    @Override public int doOperate(int num1, int num2) {
        return num1 * num2;
    }
}

 

   3.3) achieved context

/**
 * 上下文,承载算法
 * 
 * @author TimFruit
 * @date 2019/5/21 8:03
 */

public class Calculator {
    IAlgorithm algorithm;

    public Calculator(IAlgorithm algorithm) {
        this.algorithm = algorithm;
    }
    
    public int exec(int num1, int num2){
        return algorithm.doOperate(num1, num2);
    }
}

 

  3.4) test scenario

public class Client {
    public static void main(String[] args) {
        IAlgorithm algorithm=new Add();
        
        Calculator calculator =new Calculator(algorithm);
        
        int num1=1;
        int num2=3;
        int result= calculator.exec(num1, num2);
        System.out.println("结果为: "+ result);
    }
}

 

 

 Four Dian strategy enumeration

  

/ ** 
 * Strategy enumerate 
 * 
 * very good, but generally for the role does not change frequently, because of the limited enumerated type (expand very poor T_T) 
 * 
 * @author TimFruit 
 * 8 @date 2019/5/21 : 12 is 
 * / 

public  enum   the Calculator { 
    
    the ADD { 
        @Override int Exec ( int num1, int num2) {
             return num1 + num2; 
        } 
    }, 
    the SUB { 
        @Override int Exec ( int num1, int num2) {
             return num1- num2; 
        } 
    } ,
    MULTIPLY {
        @Override int exec(int num1, int num2) {
            return num1 * num2;
        }
    };
    
    
    
    abstract int exec(int num1, int num2);
    
}

 

  test environment:

public class Client {

    public static void main(String[] args) {
        int num1=1;
        int num2=3;
        
        int result=Calculator.ADD.exec(num1, num2);
        System.out.println("结果为: "+ result);
    }
    
}

 

 

 Learning materials:

  <Zen design pattern>

 

 

 

Guess you like

Origin www.cnblogs.com/timfruit/p/11121410.html