• 工厂模式-设计模式-第4篇


    1. 工厂模式

    from abc import ABCMeta, abstractmethod
    
    
    class Payment(metaclass=ABCMeta):
    
        @abstractmethod
        def pay(self, money):
            pass
    
    
    class Alipay(Payment):
        def __init__(self, use_huabei=False):
            self.use_huabei = use_huabei
    
        def pay(self, money):
            if self.use_huabei:
                print("使用花呗支付 %d " % money)
            else:
                print("Ali payed %d" % money)
    
    
    class WechatPay(Payment):
    
        def pay(self, money):
            print("Wechat pay %d" % money)
    
    
    class PaymentFactory(metaclass=ABCMeta):
    
        @abstractmethod
        def create_payment(self):
            pass
    
    
    class AliPayFactory(PaymentFactory):
        def create_payment(self):
            return Alipay()
    
    
    class WechatPayFactory(PaymentFactory):
        def create_payment(self):
            return WechatPay()
    
    
    class HuabeiPayFactory(PaymentFactory):
        def create_payment(self):
            return Alipay(use_huabei=True)
    
    
    if __name__ == '__main__':
        pf = HuabeiPayFactory()
        p = pf.create_payment()
        p.pay(100)
    
    

    假设需要增加一种支付方式 银联支付
    则需要 增加两个类 BankPay BankPayFactory

    from abc import ABCMeta, abstractmethod
    
    
    class Payment(metaclass=ABCMeta):
    
        @abstractmethod
        def pay(self, money):
            pass
    
    
    class Alipay(Payment):
        def __init__(self, use_huabei=False):
            self.use_huabei = use_huabei
    
        def pay(self, money):
            if self.use_huabei:
                print("使用花呗支付 %d " % money)
            else:
                print("Ali payed %d" % money)
    
    
    class WechatPay(Payment):
    
        def pay(self, money):
            print("Wechat pay %d" % money)
    
    
    class BankPay(Payment):
    
        def pay(self, money):
            print("银联支付 pay %d" % money)
    
    
    class PaymentFactory(metaclass=ABCMeta):
    
        @abstractmethod
        def create_payment(self):
            pass
    
    
    class AliPayFactory(PaymentFactory):
        def create_payment(self):
            return Alipay()
    
    
    class WechatPayFactory(PaymentFactory):
        def create_payment(self):
            return WechatPay()
    
    
    class HuabeiPayFactory(PaymentFactory):
        def create_payment(self):
            return Alipay(use_huabei=True)
    
    
    class BankPayFactory(PaymentFactory):
        def create_payment(self):
            return BankPay()
    
    
    if __name__ == '__main__':
        pf = BankPayFactory()
        p = pf.create_payment()
        p.pay(100)
    

    对于与每个 具体的产品类 (WechatPay AliPay BankPay) 必须增加 相应的 工厂类 (WechatPayFactory AliPayFactory BankPayFactory)

  • 相关阅读:
    在spring MVC的controller中获取ServletConfig
    支付宝移动接入报系统繁忙,參数错误等错误
    Android:控件WebView显示网页
    android WebView总结
    关于用WebView或手机浏览器打开连接问题
    Android解决Fragment多层嵌套时onActivityResult无法正确回调的问题
    Fragment嵌套Fragment时遇到的那些坑
    Android 多个Fragment嵌套导致的三大BUG
    Android Fragment使用(四) Toolbar使用及Fragment中的Toolbar处理
    Android Fragment使用(三) Activity, Fragment, WebView的状态保存和恢复
  • 原文地址:https://www.cnblogs.com/cavalier-chen/p/14715706.html
Copyright © 2020-2023  润新知