Home  >  Article  >  Java  >  Learning the Strategy Pattern in Java

Learning the Strategy Pattern in Java

黄舟
黄舟Original
2017-10-13 10:16:461200browse

This article mainly introduces the relevant information of the strategy pattern in java design pattern learning in detail. It has a certain reference value. Interested friends can refer to

Strategy pattern: The strategy pattern is A method of defining a series of algorithms. The work done by the algorithms is the same, but the implementation is different. It can call all algorithms in the same way, reducing the coupling between various algorithm classes and the use of algorithm classes.

Java implements a strategy model:

Requirements: Shopping mall cashier system, the cashier method is normal charging, 20% off, and 100 back for purchases over 300, these three charging methods.

1: Create a super class. That is, the abstract method of charging.


public abstract class CashSuper {
  public abstract double acceptCash(double money);
}

2: Create a class that implements this superclass.


public class CashNormal extends CashSuper {
 
  @Override
  public double acceptCash(double money) {
    return money;
  }
 
}

3: According to the different charging methods of shopping malls, create specific implementation algorithm classes for three charging methods


/**打折子类
 * 获取打折折扣,使用应付金额乘以折扣
 * @author 我不是张英俊
 *
 */
public class CashRebate extends CashSuper {

  private double moneyRebate=1;
  
  public CashRebate(String moneyRebate){
    this.moneyRebate=Double.parseDouble(moneyRebate);
  }
  @Override
  public double acceptCash(double money) {
    
    
    return money*moneyRebate;
  }

}


/**返利收费子类,例如,满三百减一百
 * @author 我不是张英俊
 *
 */
public class CashReturn extends CashSuper {

  /**
   * 返利收费,初始化时必须要输入返利条件和返利值,比如慢300时返100,
   * 则moneyCondition为300,moneyRetrun 为100
   */
  private double moneyCondition=0;
  private double moneyReturn=0;
  
  public CashReturn(String moneyCondition,String moneyReturn){
    this.moneyCondition=Double.parseDouble(moneyCondition);
    this.moneyReturn=Double.parseDouble(moneyReturn);
  }
  
  
  @Override
  public double acceptCash(double money) {
    
    double result=money;
    if(money>=moneyCondition){
      result=money-Math.floor(money/moneyCondition)*moneyReturn;
    }
    
    return result;
  }

}


/**
 *正常收费的,原价返回
 * @author 我不是张英俊
 *
 */
public class CashNormal extends CashSuper {

  @Override
  public double acceptCash(double money) {
    return money;
  }

}

4: Write a Context to implement different payment methods through the same method. Because different objects need to be created, it needs to be implemented in conjunction with the simple factory pattern.


public class CashContext {

  private CashSuper cs=null;
  
  public CashContext(String type){
    switch (type) {
    case "正常收费":
      CashNormal cs0=new CashNormal();
      cs=cs0;
      break;
    case "满300返100":
      CashReturn cr1=new CashReturn("300", "100");
      cs=cr1;
      break;
    case "打八折":
      CashRebate cr2=new CashRebate("0.8");
      cs=cr2;
      break;
      
    }
  }
  
  public double GetResult(double money){
    return cs.acceptCash(money);
  }
  
}

5: Test class


public class Test {

  public static void main(String[] args) {
    
    double a=new CashContext("打八折").GetResult(300);
    System.out.println(a);
    double b=new CashContext("正常收费").GetResult(300);
    System.out.println(b);
    double c=new CashContext("满300返100").GetResult(300);
    System.out.println(c);
  }

}

6: Console.

240.0
300.0
200.0

Summary: Strategy pattern simplifies unit testing because each algorithm is its own class and can be tested individually through its own interface .

The strategy pattern is used to encapsulate algorithms, but in actual use, it can be used to encapsulate any type of rules, as long as you hear that different business rules need to be applied at different times during the analysis process , you can consider the possibility of using the strategy pattern to handle this change.

My own reflection: In general, the strategy pattern encapsulates the algorithm. For example, Kung Fu Panda, after inheriting the super class of animal, needs to have both the function of implementing the language and the function of kung fu. Using strategies Modes separate various algorithms and can be combined well when used.

The above is the detailed content of Learning the Strategy Pattern in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn