<?php //青铜会员的打折商品 class BronzeRebateCommodity { //描述 public $descri = ‘青铜会员的打折商品‘; } //白银会员的打折商品 class SilverRebateCommodity { //描述 public $descri = ‘白银会员的打折商品‘; } //青铜会员的推荐商品 class BronzeCommendatoryCommodity { //描述 public $descri = ‘青铜会员的推荐商品‘; } //白银会员的推荐商品 class SilverCommendatoryCommodity { //描述 public $descri = ‘白银会员的推荐商品‘; } //各个工厂的接口 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; } } //建造一个调度中心 $center = new CenterFactory(); //获得一个白银会员的工厂 $factory = $center->getFactory(‘silver‘); //让工厂制造一个推荐产品 $product = $factory->create(‘Commendatory‘); //可以看到是白银会员的推荐商品 echo $product->descri; ?>
原文:http://www.cnblogs.com/jiufen/p/4990497.html