clllll  

结构型

类与类之间 关系的一种模式

适配器模式

  • 将一个类的接口转换成另一种接口。兼容。

俩种方式

  1. 多继承
# 银行支付和网上支付不一样
class BankPayment:
   def cost(self, money):
       print("银行支付")
# 继承网上支付。
class NewBankPayment(Payment, BankPayment):
   def pay(self, money):
       self.cost(money)
  1. 组合
class PaymentAdapter(Payment):
   def __init__(self, payment):
       self.payment = payment
   
   def pay(self, money):
       self.payment.cost(money)

桥模式

将一个事物的俩个维度分离,使其都可以独立地变化
形状和颜色

# 颜色和形状紧密耦合
class Shape:
    pass 

class Line(Shape):
    pass 

class Rectangle(Shape):
    pass 

class Circle(Shape):
    pass 

# 类爆炸 3 * 3 。
class RedLine(Line):
    # * 3 
    pass 

class RedRectangle(Rectangle):
    # * 3 
    pass 

改进后。
互相拥有

from abc import ABCMeta, abstractmethod
class Shape(metaclass=ABCMeta):
    # 形状 拥有 color 
    def __init__(self, color):
        self.color = color 

    @abstractmethod
    def draw(self):
        pass 

class Color(metaclass=ABCMeta):
    @abstractmethod
    def paint(self, shape):
        # color 调用 shape.
        pass 

class Line(Shape):
    def draw(self):
        self.color.paint(self)

class Rectangle(Shape):
    def draw(self):
        self.color.paint(self)

class Circle(Shape):
    def draw(self):
        self.color.paint(self) 


class Red(Color):
    def paint(self, shape):
        print(" 红色的", shape)

class Green(Color):
    def paint(self, shape):
        print(" 绿色的", shape)


shape1 = Line(Red())
shape2 = Rectangle(Green())
shape1.draw()
shape2.draw()

组合模式

  • 将对象组合成树形结构 以表示 部分-整体 的层次结构。单个对象和组合对象的使用具有一致性。
  • 抽象组件、叶子组件、复合组件、客户端
  • 部分和整体如何一致?使用统一的接口
from abc import ABCMeta,abstractmethod

class Graphic(metaclass=ABCMeta):
    # 统一的接口
    @abstractmethod
    def draw(self):
        pass 

class Point(Graphic):
    def __init__(self, x, y):
        self.x = x 
        self.y = y 

    def draw(self):
        print("Ponint:", self.x, self.y)
    
class Line(Graphic):
    def __init__(self, p1, p2):
        self.p1 = p1 
        self.p2 = p2 

    def draw(self):
        print("Line:", self.p1, self.p2)

class Picture(Graphic):
    # 复杂图形
    def __init__(self, iterable):
        self.children = []
        for g in iterable:
            self.children.append(g)
    
    def draw(self):
        for g in self.children:
            g.draw() 

p1  = Point(1,2)
p2 = Point(3,4)
l1 = Line(p1, p2)
pic  = Picture([p1, p2, l1])
pic.draw()

外观模式

  • 为子系统中的一组接口提供一个一致的界面。外观模式定义了一个高层接口,这个接口使得这子系统更加容易使用
  • 外观 facade
  • 子系统类 subsystem classes
# coding=utf-8

class Compute:
   '外观类'
    def __init__(self):
        self.disk = Disk() 
        self.cpu = CPU()
        self.memory = Memory()
    
    def run(self):
        self.disk.run()
        self.cpu.run()
        self.memory.run() 
    
    def stop(self):
        self.disk.stop()
        self.cpu.stop()
        self.memory.stop() 

class Disk:
    # 子系统类 
    def run(self):
        print("磁盘运行")
    
    def stop(self):
        print("磁盘停止")

class CPU:
    def run(self):
        print("CPU运行")
    
    def stop(self):
        print("CPU停止")

class Memory:
    def run(self):
        print("Memory运行")
    
    def stop(self):
        print("Memory停止")

c = Compute()
c.run()
c.stop()

代理模式

  • 为其他对象提供一种代理以控制对这个对象的访问。
  • 远程代理、虚代理、保护代理
from abc import ABCMeta,abstractmethod

class Subject(metaclass=ABCMeta):
    # 为代理对象和真实对象提供统一的接口
    @abstractmethod
    def get_content(self):
        pass 

    @abstractmethod
    def set_content(self):
        pass 

class RealSubject(Subject):
    def __init__(self, filename):
        self.filename = filename
        f = open(self.filename, 'r', encoding='utf-8')
        self.content = f.read()
        f.close()

    def get_content(self):
        return self.content
    
    def set_content(self, content):
        f = open(self.filename, 'w', encoding='utf-8')
        f.write(content)
        f.close() 

class VirtualSubject(Subject):
    # 虚代理 
    def __init__(self, filename):
        self.filename = filename
        self.subj = None 
    
    def get_content(self):
        # 真正需要的时候才获取
        if not self.subj:
            self.subj = RealSubject(self.filename)
        return self.subj.get_content() 
    
    def set_content(self, content):
        if not self.subj:
            self.subj = RealSubject(self.filename)
        return self.subj.set_content(content)

class ProtectedSubject(Subject):
    # 保护代理 
    def __init__(self, filename):
        self.subj = RealSubject(filename)
    
    def get_content(self):
        return self.subj.get_content() 
    
    def set_content(self, content):
        # 权限设置等
        raise PermissionError("没权限")
    

subj = VirtualSubject('tree.py')
psubj = ProtectedSubject("tree.py")
psubj.set_content("dsafdsaf")

posted on 2022-10-30 13:59  llcl  阅读(30)  评论(0编辑  收藏  举报