简单工厂设计模式
内容:不直接向客户端暴露对象创建的实现细节,而是通过一个工厂类来负责创建产品类的实例
角色:
- 抽象工厂角色(Creator)
- 具体工厂角色(Concrete Creator)
- 具体产品角色(Concrete Product)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| 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("支付宝支付%d元" % money) class WechatPay(Payment): def pay(self, money): print("微信支付%d元" % money) class BankPay(Payment): def pay(self, money): print("银行卡支付%d元" % money) class PaymentFactory: def create_payment(self, method): if method == 'alipay': return Alipay(use_huabei=True) elif method == 'wechat': return WechatPay() elif method == 'bankpay': return BankPay() else: raise TypeError("No such payment named %s" % method) pf = PaymentFactory() p = pf.create_payment('alipay') p.pay(100)
|
优点:
缺点:
- 违反了单一职责原则,将创建逻辑几种到一个工厂类里
- 当添加新产品时,需要修改工厂类代码,违反了开闭原则
工厂方法模式
内容:定义一个用于创建对象的接口(工厂接口),让子类决定实例化那一个产品类
角色:
- 抽象工厂角色(Creator)
- 具体工厂角色(Concrete Creator)
- 抽象产品角色(Product)
- 具体产品角色(Concrete Product)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| 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("支付宝支付%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(use_huabei=True) class WechatPayFactory(PaymentFactory): def create_payment(self): return WechatPay()
class BankPayFactory(PaymentFactory): def create_payment(self): return pf = AliPayFactory() p = pf.create_payment() p.pay(100)
|
优点:
- 每个具体产品都对应一个具体工厂类,不需要修改工厂类代码
- 隐藏了对象创建的实现细节
缺点:
- 每增加一个具体产品类,就必须增加一个相对应的具体工厂类
抽象工厂模式
内容:定义一个工厂类接口,让工厂子类来创建一系列相关或相互依赖的对象
例如:生产一部手机,需要手机壳、CPU、操作系统三类对象进行组装,其中每类对象都有不同的种类。对每个具体工厂,分别生产一部手机所需要的三个对象
相比工厂方法模式,抽象工厂模式中的每个具体工厂都生产一套产品
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| from abc import ABCMeta, abstractmethod
class PhoneShell(metaclass=ABCMeta): @abstractmethod def show_shell(self): pass class PhoneCPU(metaclass=ABCMeta): @abstractmethod def show_cpu(self): pass class PhoneOS(metaclass=ABCMeta): @abstractmethod def show_os(self): pass
class SmallShell(PhoneShell): def show_shell(self): print("普通手机小手机壳") class BigShell(PhoneShell): def show_shell(self): print("普通手机大手机壳") class AppleShell(PhoneShell): def show_shell(self): print("苹果手机壳") class SnapDragonCPU(PhoneCPU): def show_cpu(self): print("骁龙CPU") class HuaweiCPU(PhoneCPU): def show_cpu(self): print("华为CPU") class AppleCPU(PhoneCPU): def show_cpu(self): print("苹果CPU") class AndroidOS(PhoneOS): def show_os(self): print("AndroidOS系统") class AppleOS(PhoneOS): def show_os(self): print("IOS系统")
class PhoneFactory(metaclass=ABCMeta): @abstractmethod def make_shell(self): pass @abstractmethod def make_cpu(self): pass @abstractmethod def make_os(self): pass
class HuaweiFactory(PhoneFactory): def make_shell(self): return SmallShell() def make_cpu(self): return HuaweiCPU() def make_os(self): return AndroidOS() class AppleFactory(PhoneFactory): def make_shell(self): return AppleShell() def male_cpu(self): return AppleCPU() def make_os(self): return AppleOS()
class Phone: def __init__(self, shell, cpu, os): self.shell = shell self.cpu = cpu self.os = os def show_info(self): print("手机信息: ") self.shell.show_shell() self.cpu.show_cpu() self.os.show_os() def make_phone(factory): shell = factory.make_shell() cpu = factory.make_cpu() os = factory.make_os() return Phone(shell, cpu, os)
p = make_phone(HuaweiFactory()) p.show_info()
|
优点:
- 将客户端与类的具体实现相分离
- 每个工厂创建了一个完整的产品系列,使得易于交换产品系列
- 有利于产品的一致性(即产品之间的约束关系)
缺点: