大话设计模式-备忘录模式

大话设计模式-备忘录模式_第1张图片

//发起人类
class Originator {

	public function __get($key) {
		return isset($this->$key) ? $this->$key : null;
	}

	public function __set($key, $value) {
		$this->$key = $value;
	}

	//需要保存的属性,可能有多个
	private $state;

	//创建备忘录,将当前需要保存的信息导入并实例化出一个Memento对象
	public function createMemento() {
		return new Memento($this->state);
	}

	//恢复备忘录,将Memento导入
	public function setMemento(Memento $memento) {
		$this->state = $memento->state;
	}

	public function show() {
		echo 'state = '.$this->state.'<br/>';
	}

}

//备忘录类
class Memento {

	public function __get($key) {
		return isset($this->$key) ? $this->$key : null;
	}

	public function __set($key, $value) {
		$this->$key = $value;
	}

	//需要保存的数据属性,可以是多个
	private $state;

	//构造方法,将相关数据导入
	function __construct($state) {
		$this->state = $state;
	}
}

//管理者类
class Caretaker {
	private $memento;

	public function __get($key) {
		return isset($this->$key) ? $this->$key : null;
	}

	public function __set($key, $value) {
		$this->$key = $value;
	}
}

$o = new Originator();
//Originator初始状态,状态属性为‘On’
$o->state = 'On';
$o->show();

//保存状态时,由于有了很好的封装,可以隐藏Orginator的实现细节
$c = new Caretaker();
$c->memento = $o->createMemento();

//Originator改变了状态属性为‘Off’
$o->state = 'Off';
$o->show();

//恢复原初状态
$o->setMemento($c->memento);
$o->show();

class GameRole {
	public function __get($key) {
		return isset($this->$key) ? $this->$key : null;
	}

	public function __set($key, $value) {
		$this->$key = $value;
	}
	//生命力
	private $vit;

	//攻击力
	private $atk;

	//防御力
	private $def;

	//状态显示
	public function stateDisplay() {
		echo '角色当前状态:<br/>';
		echo '体力:'.$this->vit.'<br/>';
		echo '攻击力'.$this->atk.'<br/>';
		echo '防御力'.$this->def.'<br/><br/>';
	}

	//获得初始状态
	public function getInitState() {
		$this->vit = 100;
		$this->atk = 100;
		$this->def = 100;
	}

	//战斗
	public function fight() {
		$this->vit = 0;
		$this->atk = 0;
		$this->def = 0;
	}

	//保存角色状态
	public function saveState() {
		return new RolestateMemento($this->vit, $this->atk, $this->def);
	}

	//恢复角色状态
	public function recoveryState(RoleStateMemento $memento) {
		$this->vit = $memento->vit;
		$this->atk = $memento->atk;
		$this->def = $memento->def;
	}
}

//角色状态储存箱
class RoleStateMemento {
	private $vit;
	private $atk;
	private $def;
	public function __get($key) {
		return isset($this->$key) ? $this->$key : null;
	}

	public function __set($key, $value) {
		$this->$key = $value;
	}
	public function __construct($vit, $atk, $def) {
		$this->vit = $vit;
		$this->atk = $atk;
		$this->def = $def;
	}
}

//角色状态管理者
class RoleStateCaretaker {
	private $memento;
	public function __get($key) {
		return isset($this->$key) ? $this->$key : null;
	}

	public function __set($key, $value) {
		$this->$key = $value;
	}
}

//客户端
//大战Boss前
$lixiaoyao = new GameRole();
//游戏角色初始化状态,三项指标数值都是100
$lixiaoyao->getInitState();
$lixiaoyao->stateDisplay();

//保存进度
//保存进度时,由于封装在Memento中,因此我们并不知道保存了哪些具体的角色数据
$stateAdmin = new RoleStateCaretaker();
$stateAdmin->memento = $lixiaoyao->saveState();

//大战Boss时,损耗严重
$lixiaoyao->fight();
$lixiaoyao->stateDisplay();

//恢复之前状态
$lixiaoyao->recoveryState($stateAdmin->memento);
$lixiaoyao->stateDisplay();

你可能感兴趣的:(设计模式,c,function,null,Class)