有些情况下我们需要根据不同的选择逻辑提供不同的构造工厂,而对于多个工厂而言需要一个统一的抽象工厂:

 

<?php

class System{}
class Soft{}

class MacSystem extends System{}
class MacSoft extends Soft{}

class WinSystem extends System{}
class WinSoft extends Soft{}


/**
 * AbstractFactory class[抽象工厂模式]
 * @author ITYangs<ityangs@163.com>
 */
interface AbstractFactory {
    public function CreateSystem();
    public function CreateSoft();
}

class MacFactory implements AbstractFactory{
    public function CreateSystem(){ return new MacSystem(); }
    public function CreateSoft(){ return new MacSoft(); }
}

class WinFactory implements AbstractFactory{
    public function CreateSystem(){ return new WinSystem(); }
    public function CreateSoft(){ return new WinSoft(); }
}






//@test:创建工厂->用该工厂生产对应的对象

//创建MacFactory工厂
$MacFactory_obj = new MacFactory();
//用MacFactory工厂分别创建不同对象
var_dump($MacFactory_obj->CreateSystem());//输出:object(MacSystem)#2 (0) { }
var_dump($MacFactory_obj->CreateSoft());// 输出:object(MacSoft)#2 (0) { }


//创建WinFactory
$WinFactory_obj = new WinFactory();
//用WinFactory工厂分别创建不同对象
var_dump($WinFactory_obj->CreateSystem());//输出:object(WinSystem)#3 (0) { }
var_dump($WinFactory_obj->CreateSoft());//输出:object(WinSoft)#3 (0) { }

 

相关文章:

  • 2021-11-08
  • 2021-06-16
  • 2021-12-13
  • 2021-07-01
  • 2022-01-20
  • 2021-11-06
  • 2021-10-29
  • 2021-10-15
猜你喜欢
  • 2021-11-01
  • 2022-12-23
  • 2021-06-21
  • 2021-10-17
  • 2021-06-06
  • 2022-12-23
相关资源
相似解决方案