AbstractFactory(抽象工厂模式)
2024-09-30 06:56:54
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
最新文章
- uiautomator-----UiWatcher监听器
- Erlang--proplists结构解析
- 用最简单的方式在C#中使用多线程加速耗时的图像处理算法的执行(多核机器)。
- C语言:联合变量
- 【转】Android Support v4、v7、v13的区别和应用场景
- jquery 3D 标签云
- 嵌入式C语言优化小技巧
- appium新版本不支持findElementByName,切换到findElementByAndroidUIAutomator
- 【转】Ubuntu环境下SSH的安装及使用
- hibernate Restrictions用法
- Oracle heap 表的主键 dump 分析
- handler 源代码分析
- Angular CurrencyPipe货币管道关于人民币符号¥的问题
- C语言第四次博客作业
- Python爬虫爬取网页图片
- spark-rpc是如何实现将netty的Channel隐藏在inbox中的
- 网络协议,socket模块
- MVC Razor
- node+mysql简单注册
- serde
热门文章
- js 学习网站
- 对javascript变量提升跟函数提升的理解
- Java编程基础-选择和循环语句
- mysql同步出现1062错误
- freebsd自动获取ip地址
- 洛谷 P2910 [USACO08OPEN]寻宝之路Clear And Present Danger
- [习题]输入自己的生日(年/月/日)#2 -- 日历(Calendar)控件的时光跳跃,一次跳回五年、十年前?--TodaysDate属性、VisibleDate属性
- Ubuntu12.04安装Chrome浏览器,并添加到左侧的启动栏
- struts2的动态方法配置
- A*和IDA*介绍