标签:
<?php interface Builder{ public function head(); public function body(); public function foot(); public function hand(); } class Director { private static $_instance = null; private $builder = null; private function __construct(Builder $builder){ $this->builder = $builder; } public static function getInstance(Builder $builder){ if(self::$_instance == null){ self::$_instance = new Director($builder); } return self::$_instance; } private function createHead(Builder $builder = null){ if($builder){ return $builder->head(); }else{ return $this->builder->head(); } } private function createBody(Builder $builder = null){ if($builder){ return $builder->body(); }else{ return $this->builder->body(); } } private function createHand(Builder $builder = null){ if($builder){ return $builder->hand(); }else{ return $this->builder->hand(); } } private function createFoot(Builder $builder = null){ if($builder){ return $builder->foot(); }else{ return $this->builder->foot(); } } public function startCreate(){ $this->createFoot(); $this->createBody(); $this->createHand(); $this->createHead(); echo "\n人造出来嘞"; } } class zhangsan implements Builder{ public function foot(){ echo "张三的脚被造出来了\n"; } public function hand(){ echo "张三的手被造出来嘞\n"; } public function head(){ echo "张三的头被造出来了\n"; } public function body(){ echo "张三的身体被造出来嘞\n"; } } class lisi implements Builder{ public function foot(){ echo "李四的脚被造出来了\n"; } public function hand(){ echo "李四的手被造出来嘞\n"; } public function head(){ echo "李四的头被造出来了\n"; } public function body(){ echo "李四的身体被造出来嘞\n"; } } $builder = Director::getInstance(new zhangsan()); $builder->startCreate(); $builder = Director::getInstance(new lisi()); $builder->startCreate();
标签:
原文地址:http://www.cnblogs.com/Super-Man/p/4384310.html