introduce
Intent: To define a series of algorithms, encapsulate them one by one, and make them interchangeable.
The main solution: the complexity and difficult maintenance brought by using if...else when there are many similar algorithms.
When to use: A system has many, many classes that are distinguished only by their direct behavior.
How to solve: Encapsulate these algorithms into classes one by one and replace them arbitrarily.
Key code: Implement the same interface.
case
Scenario: travel route (choose by car, plane, boat...), payment method (choose Alipay, WeChat, Jingdong Baitiao...), login method (choose QQ login, account login, email login...), their algorithms are similar, all to achieve the same purpose. Such as: payment method, the final result is that I paid a sum of money and went out. Let's take online shopping as an example to see the benefits of the strategy model.
Code:
1> When we pay for online shopping, we definitely need to choose a payment method. The following is a list of several commonly used payment methods.
//支付宝支付
public class AliPay implements Payment {
@Override
public PayState pay(String uid, Double amount) {
System.out.println("欢迎使用支付宝支付");
System.out.println("查询账户余额,开始扣款");
return new PayState(200,"支付成功",amount);
}
}
//京东支付
public class JDPay implements Payment {
@Override
public PayState pay(String uid, Double amount) {
System.out.println("欢迎使用京东白条支付");
System.out.println("查询账户余额,开始扣款");
return new PayState(200,"支付成功",amount);
}
}
//银联支付
public class UnionPay implements Payment {
@Override
public PayState pay(String uid, Double amount) {
System.out.println("欢迎使用银联支付");
System.out.println("查询账户余额,开始扣款");
return new PayState(200,"支付成功",amount);
}
}
//微信支付
public class WeChatPay implements Payment {
@Override
public PayState pay(String uid, Double amount) {
System.out.println("欢迎使用微信支付");
System.out.println("查询账户余额,直接从微信红包扣款");
return new PayState(200,"支付成功",amount);
}
}
2> The above payments all implement the same payment interface.
/**
* 支付渠道、方式
*/
public interface Payment {
public PayState pay(String uid, Double amount);
}
3> List the payment methods in the form of enumeration, so that when the user pays, they only need to choose which payment method, and do not need to care about how to implement it.
/**
* 把枚举当成一个常量去维护
*/
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 getPayment() {
return payment;
}
}
4> After selecting the product, place the order.
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.getPayment().pay(uid,amount);
}
}
5> After the payment is successful, there will be a payment status (success or failure).
/**
* 支付状态
*/
public class PayState {
private int code;
private String msg;
private Object data;
public PayState(int code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public String toString(){
return "支付状态:【"+code+"】,"+msg+",交易详情:"+data;
}
}
6> The user starts to pay.
public class PayStrategyTest {
public static void main(String[] args) {
Order order=new Order("1","20180427001",351.21);
//开始支付 微信支付、支付宝支付、银联支付、京东支付
//每个渠道支付的算法是不一样的
//基本算法固定,用户只有选择的权利
System.out.println(order.pay(PayType.WECHAT_PAY));
//执行结果:
//欢迎使用微信支付
//查询账户余额,直接从微信红包扣款
//支付状态:【200】,支付成功,交易详情:351.21
}
}
Summary: The strategy pattern actually defines a series of algorithms, encapsulates each algorithm, and makes them interchangeable, allowing the algorithm to change independently of the users who use it. Just like the payment above, each payment method has a different algorithm, but they can be replaced with each other, and this replacement is entirely up to the user.