php在线备忘录,php设计模式 Memento (备忘录模式)

25种php设计模式,你全都知道吗?下面用代码介绍备忘录模式(Memento模式)<?php

/**

* 备忘录模式

*

* 在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样以后就可以将该对象恢复到保存的状态

*/

class Memento {

private $_state = null;

public function __construct($state) {

$this->_state = $state;

}

public function getState() {

return $this->_state;

}

}

class Caretaker {

private $_memento = null;

public function getMemento() {

return $this->_memento;

}

public function setMemento($memento) {

$this->_memento = $memento;

}

}

class Originator {

private $_state = null;

public function getState() {

return $this->_state;

}

public function setState($state) {

$this->_state = $state;

}

public function createMemento() {

returnnewMemento($this->_state);

}

public function setMemento($memento) {

$this->_state = $memento->getState();

}

public function display() {

echo "state = " . $this->_state . "
";

}

}

$objOriginator = new Originator();

$objOriginator->setState(0);

$objOriginator->display();

$objCareTaker = new CareTaker();

$objCareTaker->setMemento($objOriginator->createMemento());

$objOriginator->setState(1);

$objOriginator->display();

$objOriginator->setMemento($objCareTaker->getMemento());

$objOriginator->display();

其他相关设计模式:

转载随意~请带上教程地址吧^^

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值