设计模式三刷(创建型)

2023-02-07  本文已影响0人  山猪打不过家猪

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()

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. 建造者模式

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
上一篇下一篇

猜你喜欢

热点阅读