zoukankan      html  css  js  c++  java
  • 一天一个设计模式(16)——备忘录模式

    备忘录模式

    <?php
    class Memento{
        private $_state;
        public function __construct($state){
            $this->_state=$state;
        }
    
        public function getState(){
            return $this->_state;
        }
    }
    
    class MementoManager{
        private $_mementoList=[];
        public function add($memento){
    $this->_mementoList[]=$memento;
        }
        public function get($index){
    return $this->_mementoList[$index];
        }
    }
    
    class Demo{
        public $state;
        public function save(){
            return new Memento($this->state);
        }
    
        public function get($memento){
    return $memento->getState();
        }
    
        public function set($memento){
            $this->state=$memento->getState();
        }
    }
    
    $demo=new Demo();
    $manager=new MementoManager();
    
    $demo->state='state1';
    $manager->add($demo->save());
    
    $demo->state='state2';
    $demo->state='state3';
    $manager->add($demo->save());
    
    $demo->state='state4';
    echo $demo->state.PHP_EOL;
    
    $demo->set($manager->get(1));
    echo $demo->state.PHP_EOL;
    
    $demo->set($manager->get(0));
    echo $demo->state.PHP_EOL;
    View Code

    本文来自博客园,作者:Bin_x,转载请注明原文链接:https://www.cnblogs.com/Bin-x/p/7363463.html

  • 相关阅读:
    linux 软件多版本共存
    git new
    centos 7 重新设置分区大小
    yum 多线程插件,apt多线程插件
    配置opencv cmake
    cmake 配置
    OpenCV 静态库 CMAKE 文件
    cron
    开课啦
    pytorch转onnx问题
  • 原文地址:https://www.cnblogs.com/Bin-x/p/7363463.html
Copyright © 2011-2022 走看看