The strategy mode is a behavior mode, whose characteristic is that the execution process is fixed, but there are some differences in the intermediate steps; it is determined when it is running. The official meaning is to define a series of algorithms, encapsulate them one by one, and make them interchangeable. Let's use the exhaustive method to give several simple understanding examples:
1. Zhuge Liang's brilliant plan, each of which is a strategy
2. For the travel mode, choose to ride a bicycle or take a car. Each travel mode is a strategy.
3. Encapsulation of fixed algorithm, etc.
4. Choose payment method for online shopping.
Scenario: when processing data according to the user's needs, you need to choose algorithms. Some fixed algorithms can be expanded.
Here is a code to select the payment method for demonstration:
package pattern.strategy.pay.payport; import pattern.strategy.pay.PayState; /** * Payment channel */ public interface Payment { public PayState pay(String uid,double amount); }
package pattern.strategy.pay.payport; public enum PayType { ALI_PAY(new AliPay()), WECHAT_PAY(new WechatPay()), UNION_PAY(new UnionPay()), JD_PAY(new JDPay()); private Payment payment;
PayType(Payment payment){ this.payment=payment; } public Payment get(){ return this.payment; } }
package pattern.strategy.pay.payport; import pattern.strategy.pay.PayState; public class AliPay implements Payment { @Override public PayState pay(String uid, double amount) { System.out.println("Welcome to Alipay"); System.out.println("Query account balance,Start deduction"); return new PayState(200,"Payment successful",amount); } }
package pattern.strategy.pay.payport; import pattern.strategy.pay.PayState; public class JDPay implements Payment { @Override public PayState pay(String uid, double amount) { System.out.println("Welcome to JD payment"); System.out.println("Query account balance,Start deduction"); return new PayState(200,"Payment successful",amount); } }
package pattern.strategy.pay.payport; import pattern.strategy.pay.PayState; public class UnionPay implements Payment { @Override public PayState pay(String uid, double amount) { System.out.println("Welcome to UnionPay payment"); System.out.println("Query account balance,Start deduction"); return new PayState(200,"Payment successful",amount); } }
package pattern.strategy.pay.payport; import pattern.strategy.pay.PayState; public class WechatPay implements Payment { @Override public PayState pay(String uid, double amount) { System.out.println("Welcome to wechat payment"); System.out.println("Query account balance,Start deduction"); return new PayState(200,"Payment successful",amount); } }
package pattern.strategy.pay; import pattern.strategy.pay.payport.PayType; public class Order { private String uid; private String orderId; private double amount; public Order(String uid,String orderId,double amount){ this.uid=uid; this.orderId=orderId; this.amount=amount; } public PayState pay(PayType payType){ return payType.get().pay(this.uid,this.amount); } }
package pattern.strategy.pay; public class PayState { private int code; private Object data; private String msg; public PayState(int code,String msg,Object data){ this.code=code; this.msg=msg; this.data=data; } public String toString() { return ("Payment status:["+code+"],"+msg+",transaction details :"+data); } }
package pattern.strategy; import pattern.strategy.pay.Order; import pattern.strategy.pay.payport.PayType; public class StrategyTest { public static void main(String[] args) { Order order=new Order("1","22233333",320); System.out.println(order.pay(PayType.ALI_PAY)); } }
Test results:
Welcome to Alipay Query account balance and start deduction Payment status: [200], payment succeeded, transaction details: 320.0