关于设计模式:创建型设计模式抽象工厂-Abstract-Factory

91次阅读

共计 2724 个字符,预计需要花费 7 分钟才能阅读完成。

简介

拿工厂办法模式作比照,在工厂办法模式中,一个工厂只能创立一种产品,如椅子工厂只能创立椅子。而形象工厂能够创立一系列产品,如家具工厂能够创立椅子,桌子,床等等。

形象工厂类负责定义能够创立的形象产品类,具体工厂编写不同格调(即各自的业务逻辑)的创立产品的过程。

角色

  • 形象工厂

    定义工厂能够生产的形象产品,多个产品则对应多个创立办法

  • 形象产品

    与工厂办法模式中没什么区别,简略定义类属性即可

    可能有多个,形象椅子,形象桌子,形象床等

  • 具体工厂

    别离实现形象工厂中定义的创立产品的办法

    可能有多个,依照不同格调或叫不同业务逻辑,创立一组产品,如古典工厂创立古典风格椅子、桌子、床,古代工厂创立古代格调桌椅床。

  • 具体产品

    实现形象产品即可

    可能有多个,古代格调桌子、古典风格椅子等等

类图

图中展现,GUIFactory 形象工厂定义能够创立 Button 和 Checkbox 形象产品。

具体工厂 WinFactory 能够创立出 Win 格调的 WinButton 和 WinCheckbox,MacFatory 能够创立出 Mac 格调的 MacButton 和 MacCheckbox。

代码

interface AbstractFactory
{public function createProductA(): AbstractProductA;

    public function createProductB(): AbstractProductB;}

class ConcreteFactory1 implements AbstractFactory
{public function createProductA(): AbstractProductA
    {return new ConcreteProductA1();
    }

    public function createProductB(): AbstractProductB
    {return new ConcreteProductB1();
    }
}

class ConcreteFactory2 implements AbstractFactory
{public function createProductA(): AbstractProductA
    {return new ConcreteProductA2();
    }

    public function createProductB(): AbstractProductB
    {return new ConcreteProductB2();
    }
}

interface AbstractProductA
{public function usefulFunctionA(): string;
}

class ConcreteProductA1 implements AbstractProductA
{public function usefulFunctionA(): string
    {return "The result of the product A1.";}
}

class ConcreteProductA2 implements AbstractProductA
{public function usefulFunctionA(): string
    {return "The result of the product A2.";}
}

interface AbstractProductB
{public function usefulFunctionB(): string;

    public function anotherUsefulFunctionB(AbstractProductA $collaborator): string;
}

class ConcreteProductB1 implements AbstractProductB
{public function usefulFunctionB(): string
    {return "The result of the product B1.";}

    public function anotherUsefulFunctionB(AbstractProductA $collaborator): string
    {$result = $collaborator->usefulFunctionA();

        return "The result of the B1 collaborating with the ({$result})";
    }
}

class ConcreteProductB2 implements AbstractProductB
{public function usefulFunctionB(): string
    {return "The result of the product B2.";}

    public function anotherUsefulFunctionB(AbstractProductA $collaborator): string
    {$result = $collaborator->usefulFunctionA();

        return "The result of the B2 collaborating with the ({$result})";
    }
}

function clientCode(AbstractFactory $factory)
{$productA = $factory->createProductA();
    $productB = $factory->createProductB();

    echo $productB->usefulFunctionB() . "\n";
    echo $productB->anotherUsefulFunctionB($productA) . "\n";
}

echo "Client: Testing client code with the first factory type:\n";
clientCode(new ConcreteFactory1());

echo "Client: Testing the same client code with the second factory type:\n";
clientCode(new ConcreteFactory2());

output:

Client: Testing client code with the first factory type:
The result of the product B1.
The result of the B1 collaborating with the (The result of the product A1.)
Client: Testing the same client code with the second factory type:
The result of the product B2.
The result of the B2 collaborating with the (The result of the product A2.)

正文完
 0