码迷,mamicode.com
首页 > 其他好文 > 详细

CenterFactory

时间:2015-07-15 20:36:39      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

<?php
/* 实例3 */
/* 抽象工厂 */
//青铜会员的打折商品
class BronzeRebateCommodity {
//描述
public $desc = ‘青铜会员的打折商品‘;
}
//白银会员的打折商品
class SilverRebateCommodity {
public $desc = ‘白银会员的打折商品‘;
}
//青铜会员的推荐商品
class BronzeCommendatoryCommodity {
public $desc = ‘青铜会员的推荐商品‘;
}
//白银会员的推荐商品
class SilverCommendatoryCommodity {
public $desc = ‘白银会员的推荐商品‘;
}
//各个工厂的接口
interface ConcreteFactory {
//生产对象的方法
public function create($what);
}
//青铜工厂
class BronzeFactory implements ConcreteFactory {
//生产对象的方法
public function create($what){
$productName = ‘Bronze‘.$what.‘Commodity‘;
return new $productName;
}
}
//白银工厂
class SilverFactory implements ConcreteFactory {
//生产对象的方法
public function create($what){
$productName = ‘Silver‘.$what.‘Commodity‘;
return new $productName;
}
}
//调度中心
class CenterFactory {
//获取工厂的方法
public function getFactory($what) {
$factoryName = $what.‘Factory‘;
return new $factoryName;
}
//获取工厂的静态方法
public static function getFactory2($what) {
$factoryName = $what.‘Factory‘;
return new $factoryName;
}
}
 
//实例化调度中心
$center  = new CenterFactory();
//获得一个白银工厂
$factory = $center->getFactory(‘Silver‘);
//让白银工厂制造一个推荐商品
$product = $factory->create(‘Commendatory‘);
//得到白银会员的推荐商品
echo $product->desc.‘<br>‘;
//获得一个青铜工厂
$factory2 = CenterFactory::getFactory2(‘Bronze‘);
//让青铜工厂制造一个打折商品
$product2 = $factory2->create(‘Rebate‘);
//得到青铜会员的推荐商品
echo $product2->desc;

CenterFactory

标签:

原文地址:http://www.cnblogs.com/yhdsir/p/4649233.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!