The policy pattern defines a family of algorithms, encapsulates them separately so that they can be replaced with each other, so that changes in the algorithm do not affect the customers using the algorithm. A policy pattern is a way of defining a series of algorithms, all of which conceptually do the same job but are implemented differently, that can call all algorithms in the same way, reducing the coupling between various algorithm classes and using algorithm classes.
A,
We must really understand how patterns work in order to choose the right patterns for different situations. The purpose of using patterns is to keep the code hierarchy clear and to minimize the need to modify and recompile the completed code, thus reducing the risk of code being broken by unintentional or intentional changes. There is also my personal use habits, is to try to make the main program code concise and clear, thus reducing the coupling degree.
Second, the problem of
Let’s think about it. Now we need a program to calculate the trading volume. There are many algorithms to calculate the trading volume, including equivalent transaction, discount transaction and so on. How do you design for functionality and low coupling, maintainability, security, and so on?
Third, understand
We carefully think about the requirements of this program, which can be simplified as, input amount -> choose a calculation algorithm -> get the trading volume, after analysis, the idea is already very obvious, we can know that although there are a variety of calculation algorithms, but these algorithms are to get a value, We just implement it differently (understand the definition of the policy pattern), so we just need to design an abstract class, and then subclass it with different algorithms and use it with a utility class. (Some people would say that simple factory mode can be used, of course, but every time I add a new algorithm, I need to change the switch judgment in the factory class, so every time I need to recompile the factory class, there is still a risk.)
Four, implementation,
We need an abstract superclass, some algorithm subclasses, and a utility class
Abstract superclass code:
public abstract class Strategy {
public abstract double AlgorithmInterface(double money);
}
Copy the code
Algorithm subclasses
// Equivalent transaction
public class NormalCash extends Strategy {
@Override
public double AlgorithmInterface(double money) {
returnmoney; }}// Discount deals
public class RebateCash extends Strategy {
private double moneyRebate = 1d;
public RebateCash(String moneyRebate) {
this.moneyRebate = Double.parseDouble(moneyRebate);
}
@Override
public double AlgorithmInterface(double money) {
returnmoney * moneyRebate; }}// Cash back transaction
public class ReturnCash extends Strategy {
private double moneyCondition = 0.0 d;
private double moneyReturn = 0.0 d;
public ReturnCash(String moneyCondition, String moneyReturn) {
this.moneyCondition = Double.parseDouble(moneyCondition);
this.moneyReturn = Double.parseDouble(moneyReturn);
}
@Override
public double AlgorithmInterface(double money) {
double result = money;
if(money >= moneyCondition) {
result = money - Math.floor(money / moneyCondition) * moneyReturn;
}
returnresult; }}Copy the code
Utility class:
public class ContextCash {
private Strategy strategy;
public ContextCash(Strategy strategy) {
this.strategy = strategy;
}
public double GetResult(double money) {
returnstrategy.AlgorithmInterface(money); }}Copy the code
Finally in the main program test use:
public class Main {
public static void main(String[] args) {
double total = 0.0 d;
ContextCash cash = null;
cash = new ContextCash(new NormalCash());
System.out.println(cash.GetResult(1.12));
cash = new ContextCash(new RebateCash("0.8"));
System.out.println(cash.GetResult(1.12));
cash = new ContextCash(new ReturnCash("300"."100"));
System.out.println(cash.GetResult(310)); }}Copy the code