# 抽象工厂

抽象工厂主要针对产品族的概念,举个例子。戴尔电脑、联想电脑就是产品族,而CPU、RAM就是他们的等级结构。每个工厂都可以生产出一个电脑出来。当然,抽象工厂的弊端就是,一旦生产了电脑,工厂再想创建其它的产品,必须重新建造一个工厂

# 代码实例

# 产品,将等级结构进行抽象
class Cpu:
    def __init__(self, cup_model):
        self.cup_model = cup_model

    def cpu_model(self):
        return self.cup_model


class Ram:
    def __init__(self, ram_model):
        self.ram_model = ram_model

    def ram_model(self):
        return self.ram_model


# 工厂接口,将产品族抽象

class ComputerInterFace:
    def __init__(self, compute_name, cpu, ram):
        self.compute_name = compute_name
        self.cpu = cpu
        self.ram = ram

    def create(self):
        raise NotImplemented


# 具体工厂
class DellComputer(ComputerInterFace):
    def create(self):
        self.cpu = Cpu('i9')
        self.ram = Ram('金士顿')
        self.compute_name = 'dell'
        return '戴尔电脑'


class LenovoComputer(ComputerInterFace):
    def create(self):
        self.cpu = Cpu('i7')
        self.ram = Ram('光威')
        self.compute_name = 'lenovo'
        return '联想电脑'