• 创建型模式-工厂方法模式


    from abc import ABCMeta,abstractmethod
    
    class Payment(metaclass=ABCMeta):
        # abstract class
        @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("支付宝余额支付%d元。"%money)
    
    class WechatPay(Payment):
        def pay(self, money):
            print("微信支付%d元。"%money)
    
    class BankPay(Payment):
        def pay(self, money):
            print("银行卡支付%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 HuabeiFactory(PaymentFactory):
        def create_payment(self):
            return Alipay(use_huabei=True)
    
    class BankPayFactory(PaymentFactory):
        def create_payment(self):
            return BankPay()
        
    #client
    pf = HuabeiFactory()
    p = pf.create_payment()
    p.pay(100)

  • 相关阅读:
    js中常见事件
    第六周
    石家庄地铁售票系统
    第五周
    第四周
    html总结2
    返回一个整数数组中最大子数组的和(续)
    第九周总结
    团队冲刺第五天-KeepRunningAPP
    团队冲刺第四天-KeepRunningAPP
  • 原文地址:https://www.cnblogs.com/tianqizhi/p/13445578.html
Copyright © 2020-2023  润新知