PHP设计模式之工厂模式(一)

2015-07-20 17:37:46 · 作者: · 浏览: 13

?


  mount = $mount;
		}
		public function getAmount(){
			return $this -> mount;
		}
		public function add($numbers = 200){
			return new self($this-> mount+$numbers);
		}
		public function debit($numbers = 200){
			if($this-> mount > $numbers){
				return new self($this-> mount - $numbers);
			}
			return false;
		}
	}
	/**
	 *	道具抽象类,规定了一些基本的道具所具有的共有的性质,例如拥有者、价格、
	 *  名称、游戏名称、可以出售、出租和购买,这些属性和方法都是属于道具共有
	 */
	abstract class Property{
		protected $name;
		protected $game;
		protected $price;
		protected $owner;
		function __construct($owner,$game,$name,$price){
			if(empty($owner)){echo '请输入道具的拥有者姓名
';return ;}
			echo '创建道具:所属游戏:'.$game.',名称:'.$name.',价格:'.$price.'
';
			$this->owner= $owner;
			$this->game = $game;
			$this->name = $name;
			$this->price =new Dollar($price);
		}
		/*道具的出租收取的租金,由具体的道具自己定义
			租金公式:$rent_num=$tax_rate * $price;
		*/
		abstract protected function calcRent();
		public function purcharse($player){
			global $owners;
			if($this-> owner ==$player-> name){
				echo '你已经购买了该道具
';
			}elseif($player -> salary->getAmount() < $this-> price-> getAmount() ){
				echo '金币不够
';
			}else{
				$flag=$player->debit($this->price->getAmount());
				if($flag){
					$price=$this->price->getAmount();
					$owners[$this->owner]->earnRent($price);
					unset($owners[$this->owner]);
					$this ->owner = $player->name;
					$owners[$this->owner]=$player;
					echo $player -> name.'购买道具:'.$this->game.$this->name.'成功
';
				}else{
					echo $player -> name.'购买道具:'.$this->
game.$this->name.'失败 '; } } } public function __get($prop){ if(isset($this->$prop)){ return $this->$prop; } } public function setRent($player){ global $owners; if($player->name==$this->owner){echo '不能租用自己的道具 ';return ;} $price = $this-> calcRent(); $player->debit($price); $owners[$this->owner]->earnRent($price); echo $player->name.'租用道具成功'; return $price; } public function sell($player){ global $owners; $price=$this->price->getAmount(); if($player -> salary-> getAmount() > $price){ echo '道具:'.$this-> game.$this->name.'-出售成功 '; //这里实际上对应着数据库的更新操作 $player->debit($price); $owners[$this->owner]->earnRent($price); unset($owners[$this->owner]); $this->owner = $player->name; $owners[$this->owner]=$player; return $price; }else{ echo '道具出售失败 '; } } public function __toString(){ return $this->game.$this->owner.$this->name; } } //一个模拟的游戏玩家类 class Player { public $salary; public $name; public function __construct($name){ $this->salary = new Dollar(9000); $this->name = $name; } //支出 public function debit($nums){ $price= $this->salary ->getAmount(); if($price < $nums){ echo '钱包的钱不够了'; return false; } $flag=$this->salary-> debit($nums); if($flag){ $this->salary = $flag; return true; }else{ return false; } } //收入 public function earnRent($nums){ $this->salary-> add($nums); } } //具体的道具类 class Street extends Property{ private $tax_rate=1; public function __construct($owner,$game,$name,$price,$tax_rate){ parent::__construct($owner,$game,$name,$price); $this-> tax_rate = $tax_rate; } //返回一个Dollar对象 publ