AbstractFactory(抽象工厂模式)

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

<?php

class Config {
public static $factory = 1;
} interface Product {
public function getName();
} abstract class AbstractFactory { public static function getFactory() {
switch (Config::$factory) {
case 1:
return new FirstFactory();
case 2:
return new SecondFactory();
}
throw new Exception('Bad config');
} abstract public function getProduct();
} class FirstFactory extends AbstractFactory {
public function getProduct() {
return new FirstProduct();
}
}
class FirstProduct implements Product {
public function getName() {
return 'The product from the first factory';
}
} class SecondFactory extends AbstractFactory {
public function getProduct() {
return new SecondProduct();
}
}
class SecondProduct implements Product {
public function getName() {
return 'The product from second factory';
}
} $firstProduct = AbstractFactory::getFactory()->getProduct();
Config::$factory = 2;
$secondProduct = AbstractFactory::getFactory()->getProduct(); print_r($firstProduct->getName());
// The first product from the first factory
print_r($secondProduct->getName());
// Second product from second factory

  

最新文章

  1. uiautomator-----UiWatcher监听器
  2. Erlang--proplists结构解析
  3. 用最简单的方式在C#中使用多线程加速耗时的图像处理算法的执行(多核机器)。
  4. C语言:联合变量
  5. 【转】Android Support v4、v7、v13的区别和应用场景
  6. jquery 3D 标签云
  7. 嵌入式C语言优化小技巧
  8. appium新版本不支持findElementByName,切换到findElementByAndroidUIAutomator
  9. 【转】Ubuntu环境下SSH的安装及使用
  10. hibernate Restrictions用法
  11. Oracle heap 表的主键 dump 分析
  12. handler 源代码分析
  13. Angular CurrencyPipe货币管道关于人民币符号¥的问题
  14. C语言第四次博客作业
  15. Python爬虫爬取网页图片
  16. spark-rpc是如何实现将netty的Channel隐藏在inbox中的
  17. 网络协议,socket模块
  18. MVC Razor
  19. node+mysql简单注册
  20. serde

热门文章

  1. js 学习网站
  2. 对javascript变量提升跟函数提升的理解
  3. Java编程基础-选择和循环语句
  4. mysql同步出现1062错误
  5. freebsd自动获取ip地址
  6. 洛谷 P2910 [USACO08OPEN]寻宝之路Clear And Present Danger
  7. [习题]输入自己的生日(年/月/日)#2 -- 日历(Calendar)控件的时光跳跃,一次跳回五年、十年前?--TodaysDate属性、VisibleDate属性
  8. Ubuntu12.04安装Chrome浏览器,并添加到左侧的启动栏
  9. struts2的动态方法配置
  10. A*和IDA*介绍