适配器模式和观察者模式

模式名称:适配器模式

名称意思:想想插座,可以将2相插槽转接为3相插槽

<?php
class Message{
		private $message;//用来承载日志内容的
		function __construct($m){
			$ip=$_SERVER['REMOTE_ADDR'];
			$this->message=$ip.':'.$m;
		}
		function getMessage(){
			return $this->message;
		}
}
class LogFile{
	public static function write($messageObj){
		$message=$messageObj->getMessage();
		$h=fopen('./log.log','a');
		fwrite($h,$message."\n");
		fclose($h);
	}
}


$message=new Message('this is a log!!');
LogFile::write($message);</b>
?>

<?php
class Message{
		private $message;//用来承载日志内容的
		function __construct($m){
			$ip=$_SERVER['REMOTE_ADDR'];
			$this->message=$ip.':'.$m;
		}
		function getMessage(){
			return $this->message;
		}
}

class MessageAdapter extends Message{
	private $ip;
	private $text;
	function __construct($m){
		//适配器往往需要调用父类的某些方法
		parent::__construct($m);
		$message=$this->getMessage();//127.0.0.1:this is a log!!
		$arr=explode(':',$message);
		$this->ip=$arr[0];
		$this->text=$arr[1];
	}
	function getIp(){
		return $this->ip;
	}
	function getText(){
		return $this->text;
	}
}

class LogFile{
	public static function write($messageObj){
		$message=$messageObj->getMessage();
		$h=fopen('./log.log','a');
		fwrite($h,$message."\n");
		fclose($h);
	}
}

class LogCSV{
	public static function write($messageObj){
		$ip=$messageObj->getIp();
		$text=$messageObj->getText();
		$message=$ip.','.$text;
		$h=fopen('./log.csv','a');
		fwrite($h,$message."\n");
		fclose($h);
	}
}


$message=new MessageAdapter('this is a log!!');
LogCSV::write($message);
?>

模式名称:观察者模式
名称意思:观察目标是否有状态的改变,一旦改变,自己也相应发生变化

<?php
/*
class Goods{
		public $name;
		public $num;
		public function __construct($name,$num){
			$this->name=$name;
			$this->num=$num;
		}
		function buy(){
			//更新数据库,可以将该商品的数量减去num
			
		}
		function back(){
			//更新数据库,可以将该商品的数量加回num
		}
}
 */
class Goods{
		public $name;
		public $num;
		private $observers=array();
		public function __construct($name,$num){
			$this->name=$name;
			$this->num=$num;
		}
		function setObserver($type,$observerObj){
			$this->observers[$type][]=$observerObj;
		}
		function useObserver($type){
			if(isset($this->observers[$type])){
				foreach($this->observers[$type] as $observer){
					$observer->update($this);
				}
			}
		}
		function buy(){
			//更新数据库,可以将该商品的数量减去num
			$this->useObserver('buy');
		}
		function back(){
			//更新数据库,可以将该商品的数量加回num
			$this->useObserver('back');
		}
}
//添加购买插件
class BuyObserver{
	public function update($goodsObj){
		$name=$goodsObj->name;
		$num=$goodsObj->num;
		echo "该用户购买了{$name},买了{$num}个";
	}
}
//
//添加退货插件
class BackObserver{
	public function update($goodsObj){
		$name=$goodsObj->name;
		$num=$goodsObj->num;
		echo "该用户购退了{$name},买了{$num}个";
	}
}

	$goods=new Goods('book',10);
	$buyObserver=new BuyObserver;
	$goods->setObserver('buy',$buyObserver);

	$backObserver=new BackObserver;
	$goods->setObserver('back',$backObserver);


	$goods->buy();

	echo '<hr/>';
	$goods->back();
?>

<?php
class Goods{
		public $name;
		public $num;
		public function __construct($name,$num){
			$this->name=$name;
			$this->num=$num;
		}
		function buy(){
			//更新数据库,可以将该商品的数量减去num
			
		}
		function back(){
			//更新数据库,可以将该商品的数量加回num
		}
}
 
class GoodsAdapter extends Goods{
		//public $name;
		//public $num;
		private $observers=array();
		public function __construct($name,$num){
			parent::_construct($name,$num);
		}
		function setObserver($type,$observerObj){
			$this->observers[$type][]=$observerObj;
		}
		function useObserver($type){
			if(isset($this->observers[$type])){
				foreach($this->observers[$type] as $observer){
					$observer->update($this);
				}
			}
		}
		function buy(){
			parent::buy();
			$this->useObserver('buy');
		}
		function back(){
			parent::back();
			$this->useObserver('back');
		}
}
//添加购买插件
class BuyObserver{
	public function update($goodsObj){
		$name=$goodsObj->name;
		$num=$goodsObj->num;
		echo "该用户购买了{$name},买了{$num}个";
	}
}
//
//添加退货插件
class BackObserver{
	public function update($goodsObj){
		$name=$goodsObj->name;
		$num=$goodsObj->num;
		echo "该用户购退了{$name},买了{$num}个";
	}
}

	$goods=new GoodsAdapter('book',10);
	$buyObserver=new BuyObserver;
	$goods->setObserver('buy',$buyObserver);

	$backObserver=new BackObserver;
	$goods->setObserver('back',$backObserver);


	$goods->buy();

	echo '<hr/>';
	$goods->back();
?>


你可能感兴趣的:(适配器模式和观察者模式)