directory

I. Strategy Pattern

Second, the demo:

Iii. Project Address:


I. Strategy Pattern

The strategy pattern defines a set of algorithms and encapsulates each algorithm so that they are interchangeable with each other without affecting the customers who use the algorithms. You need to design an interface that provides a unified approach to a set of implementation classes that implement the interface, or you can design an abstract class (optional, belonging to the helper class) that provides helper functions

The decision of policy mode lies in the user, the system itself provides the realization of different algorithms, add or delete algorithms, do encapsulation of various algorithms. Therefore, the strategy pattern is used in algorithmic decision system, and the external user only needs to decide which algorithm to use.

Second, the demo:

// Unified interface

/** * Created by yjl on 2020/9/30. https://blog.csdn.net/qq_27471405/article/details/108886135 * uniform interface * / public interface ICalculator {public int calculate(String exp); }Copy the code

// Abstract classes, as helper classes, can provide the methods you think you need

/** * Created by yjl on 2020/9/30. https://blog.csdn.net/qq_27471405/article/details/108886135 * algorithm method for auxiliary class * / public abstract class AbstractCalculator { @param exp * @param opt * @return */ public int[] split(String exp,String opt){String array[] = exp.split(opt); int arrayInt[] = new int[2]; arrayInt[0] = Integer.parseInt(array[0]); arrayInt[1] = Integer.parseInt(array[1]); return arrayInt; } public int[] split2(String exp,String opt){system.out.println (" there can be other methods "); Return new int [],5,11 {2}; }}Copy the code

// Three implementation classes for the interface: PlusService:

/** * Created by yjl on 2020/9/30. https://blog.csdn.net/qq_27471405/article/details/108886135 * * addition service/public class PlusService extends AbstractCalculator implements ICalculator{ @Override public int calculate(String exp) { int arrayInt[] = split(exp.toString(),"[+]"); return arrayInt[0]+arrayInt[1]; }}Copy the code

  

MinusService:

/** * Created by yjl on 2020/9/30. Subtract service https://blog.csdn.net/qq_27471405/article/details/108886135 * * / public class MinusService extends AbstractCalculator implements ICalculator { @Override public int calculate(String exp) { int arrayInt[] = split(exp.toString(),"-"); return arrayInt[0]-arrayInt[1]; }}Copy the code

MultiplyService:

/** * Created by yjl on 2020/9/30. https://blog.csdn.net/qq_27471405/article/details/108886135 * * / multiplication service public class MultiplyService extends AbstractCalculator implements ICalculator { @Override public int calculate(String exp) { int arrayInt[] = split(exp.toString(),"[*]"); return arrayInt[0]*arrayInt[1]; }}Copy the code

Of course, if you want to add more, you can continue to add, in accordance with the open closed principle, open to expansion

/ / test class

public class TestStrategy { public static void main(String[] args) { String exp = "10-3"; ICalculator cal = new MinusService(); int result = cal.calculate(exp); System. The out. Println (exp + ":" + result); }}Copy the code

Iii. Project Address:

Github.com/jalenFish/d…