1.工厂模式
from abc import ABC,abstractmethod
class Payment(ABC):
@abstractmethod
def _pay(self):
pass
class AliPay(Payment):
def _pay(self):
print("Paying by Ali...")
class PaypalPay(Payment):
def _pay(self):
print("Pay by Paypal...")
#版本一:普通工厂
class OnlinePay_LOW:
def creat_payment(self,method):
if method =='alipay':
return AliPay()
elif method =='paypal':
return PaypalPay()
else:
print("NO SUCH PAY METHOD!")
#版本二:依赖注入
class OnlinePay:
def __init__(self,pay_method:Payment) -> None:
self._pay_method = pay_method
def pay_online(self):
self._pay_method._pay()
return
alipay =AliPay()
pay1 = OnlinePay(alipay)
pay1.pay_online()
- 版本一:普通工厂实际用处不多,虽然它实现了解耦,但是里面有很多的if判断,所以不是很好,而且他也没有利用到多态。
- 版本二:好处①不能直接使用各种支付方式的pay,更好的保护了代码②有一个OnlinePay的类,专门用于控制线上支付,使用方法也是pay_online,有利于代码控制
2. 工厂模式改进和重构
- 上面版本二有个问题就是只有一个支付方法类,如果需要新的支付方式,就不支持,所以我们可以抽象出来一个支付方法类。这也体现了代码设计的原则:一般不要直接设计代码框架,而是从业务上出来设计
from abc import ABC,abstractmethod
class Payment(ABC):
@abstractmethod
def _pay(self):
pass
class AliPay(Payment):
def _pay(self):
print("Paying by Ali...")
class PaypalPay(Payment):
def _pay(self):
print("Pay by Paypal...")
class PayMethod(ABC):
@abstractmethod
def __init__(self,pay_method:Payment) -> None:
self._pay_method = pay_method
@abstractmethod
def pay(self):
pass
class OnlinePay(PayMethod):
def __init__(self,pay_method:Payment) -> None:
self._pay_method = pay_method
def pay(self):
print("Paying by Online...")
self._pay_method._pay()
return
class BankPay(PayMethod):
def __init__(self,pay_method:Payment) -> None:
self._pay_method = pay_method
def pay(self):
print("Paying by Bank...")
self._pay_method._pay()
return
alipay =AliPay()
pay_online = OnlinePay(alipay)
pay_Bank = BankPay(alipay)
pay_online.pay()
pay_Bank.pay()
- 通过添加一个支付方法类,进行扩展,这样在以后有新的支付方法比如用比特币支付,只需要添加比特币支付类即可,其他地方都不用改动!
3.工厂类
- 通过抽象一个工厂类,然后通过不同的工厂产生不同的类
from abc import ABC,abstractmethod
class Payment(ABC):
@abstractmethod
def _pay(self):
pass
class AliPay(Payment):
def _pay(self):
print("Paying by Ali...")
class PaypalPay(Payment):
def _pay(self):
print("Pay by Paypal...")
class PaymentFoctory(ABC):
@abstractmethod
def create_payment(self):
pass
class AlipayFacotry(PaymentFoctory):
def create_payment(self):
return AliPay()
class PaypalPayFacotry(PaymentFoctory):
def create_payment(self):
return PaypalPay()
ali_pay_factory = AlipayFacotry()
ali_pay = ali_pay_factory.create_payment()
ali_pay._pay()
4.抽象工厂
pass
5. 建造者模式
- 模式重点:有一个director来指挥创建类实现的顺序和步骤;如果需要更换步骤和逻辑,可以使用director 2,并且可以和工厂模式结合
from dataclasses import dataclass
from abc import ABC,abstractmethod
#产品1
@dataclass
class Player:
face:str = None
hair:str = None
body:str = None
#产品2
@dataclass
class Player2:
face:str = None
hair:str = None
body:str = None
#抽象建造者
class Builder(ABC):
@abstractmethod
def build_face(self):
pass
@abstractmethod
def build_hair(self):
pass
@abstractmethod
def build_body(self):
pass
#具体建造者1
class SexyGirl(Builder):
def __init__(self) -> None:
self.player = Player()
def build_face(self):
self.player.face = "baby face"
return self
def build_hair(self):
self.player.hair = "brown hair"
return self
def build_body(self):
self.player.body = "hot body"
return self
#具体建造着2
class Monster(Builder):
def __init__(self) -> None:
self.player = Player()
def build_face(self):
self.player.body = "Monster face"
return self
def build_hair(self):
self.player.hair = "Monster hair"
return self ## 返回自己可以类似于java的链式调用
def build_body(self):
self.player.body = "Monster body"
return self
#指挥者
class BuildDirector: ##控制顺序
def build_charecher1(self,builder:Builder):
builder.build_face().build_body().build_hair()
return builder.player
def build_charecher2(self,builder:Builder):
builder.build_body().build_hair().build_face()
return builder.player
sexy_girl = SexyGirl()
director = BuildDirector()
p = director.build_charecher2(sexy_girl)
print(vars(p)) #打印出p的所有方法
print(isinstance(p,Player)) #判断实例是否属于某个实例1
print(type(p) is Builder) #判断实例是否属于某个实例2
-
重点:
链式调用
判断成员
获取所有属性的值
网友评论