设计模式实战,工厂+策略+单例 解决 if else 过多问题

场景

在支付选择中,可以选择京东白条、支付宝、微信、银联支付等。



    public void payment(String payType) {
     

        if (payType.equals("JD")) {
     
            
            //京东支付
            
        } else if (payType.equals("Alibaba")) {
     
            //支付宝支付
            
        } else if (payType.equals("Tecent")) {
     
            //微信支付
            
        } else if (payType.equals("Union")) {
     
            //银联支付            
            
        } else {
     
            //其它支付........

        }

    }

解决方案

工厂设计模式+策略模式+单例模式,UML图如下:

设计模式实战,工厂+策略+单例 解决 if else 过多问题_第1张图片

策略

public abstract class PaymentStrategy {
     


    public abstract String getName();

    protected abstract double queryBalance(String uid);


    public MsgResult pay(String uid, double amount) {
     
        if (queryBalance(uid) < amount) {
     
            return new MsgResult(500, "支付失败", "余额不足");
        }
        return new MsgResult(200, "支付成功", "支付余额: " + (queryBalance(uid) - amount));
    }


}

public class UnionPayStrategy extends PaymentStrategy {
     
    @Override
    public String getName() {
     
        return "银联支付";
    }

    @Override
    protected double queryBalance(String uid) {
     
        return 100;
    }
}

public class TecentPayStrategy extends PaymentStrategy {
     
    @Override
    public String getName() {
     
        return "微信支付";
    }

    @Override
    protected double queryBalance(String uid) {
     
        return 1000;
    }
}

public class JdPayStrategy extends PaymentStrategy {
     
    @Override
    public String getName() {
     
        return "京东白条";
    }

    @Override
    protected double queryBalance(String uid) {
     
        return 1000;
    }
}

public class AlibabaPayStrategy extends PaymentStrategy {
     

    @Override
    public String getName() {
     
        return "支付宝支付";
    }

    @Override
    protected double queryBalance(String uid) {
     
        return 1000;
    }
}



枚举

public enum PaymentEnum {
     


    ALIBABA("ALIBABA", new AlibabaPayStrategy()),
    JD("JD", new JdPayStrategy()),
    TECENT("TECENT", new TecentPayStrategy()),
    UNION("UNION", new UnionPayStrategy()),
    DEFAULT("DEFAULT", new AlibabaPayStrategy());

    private final String payName;

    private final PaymentStrategy paymentStrategy;


    PaymentEnum(String payName, PaymentStrategy paymentStrategy) {
     
        this.payName = payName;
        this.paymentStrategy = paymentStrategy;
    }

    public String getPayName() {
     
        return payName;
    }

    public PaymentStrategy getPaymentStrategy() {
     
        return paymentStrategy;
    }
}

工厂+单例

public class PaymentFactory {
     

    /**
     * 注册表
     */
    private static Map<String, PaymentStrategy> register = new HashMap<>();

    static {
     
        for (PaymentEnum p : PaymentEnum.values()) {
     
            register.put(p.getPayName(), p.getPaymentStrategy());
        }
    }

    /**
     * 单例+饿汉(注册表)
     */
    public static PaymentStrategy getInstance(String payType) {
     
        if (register.containsKey(payType)) {
     
            return register.get(payType);
        }
        return PaymentEnum.DEFAULT.getPaymentStrategy();
    }


}

业务类


@AllArgsConstructor
@Data
@NoArgsConstructor
@ToString
public class MsgResult {
     

    private int code;

    private Object data;

    private String msg;


}




@AllArgsConstructor
@Data
@NoArgsConstructor
@ToString
public class Order {
     

    private String uid;

    private String orderId;

    private Double amount;


    public MsgResult pay(String payType) {
     

        PaymentStrategy paymentStrategy = PaymentFactory.getInstance(payType);
        System.out.println("欢迎使用" + paymentStrategy.getName());
        System.out.println("本次交易金额为:"  + amount + ",....开始扣款");
        return paymentStrategy.pay(uid,amount);

    }

}

测试类

public class PayTest {
     

    public static void main(String[] args) {
     

        Order order = new Order("6", "6", 500.0);
        MsgResult msg = order.pay(PaymentEnum.ALIBABA.getPayName());
        System.out.println(msg.toString());

    }

}

设计模式实战,工厂+策略+单例 解决 if else 过多问题_第2张图片

你可能感兴趣的:(设计模式,设计模式实战)