zoukankan      html  css  js  c++  java
  • 设计模式复习-备忘录模式

    #pragma once
    #include "stdafx.h"
    #include<string>
    #include<iostream>
    using namespace std;
    
    
    /*
    	设计模式-备忘录模式(Memento)
    	在不破坏对象封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。
    这样以后就可以将该对象恢复到原先保存的状态。
    */
    
    
    class Cmemento {//备忘录
    public:
    	string mstrState="";
    public:
    	Cmemento(const string &strState) {
    		mstrState = strState;
    	}
    };
    
    class COriginator {//发起人
    public:
    	string mstrState="";
    public:
    	Cmemento * CreateMemento() {
    		return new Cmemento(mstrState);
    	}
    	void SetMemento(Cmemento * pMemento) {
    		mstrState = pMemento->mstrState;
    	}
    	void Show() {
    		cout << "State=" << mstrState << endl;
    	}
    };
    
    class CCaretaker {
    public:
    	Cmemento *mpMemento=NULL;
    };
    
    
    int main() {
    	COriginator *P0 = new COriginator();
    	P0->mstrState = "On";
    	P0->Show();
    
    	CCaretaker *pC = new CCaretaker();
    	pC->mpMemento = P0->CreateMemento();
    
    	P0->mstrState = "Off";
    	P0->Show();
    
    	P0->SetMemento(pC->mpMemento);
    	P0->Show();
    
    	getchar();
    	return 0;
    }

  • 相关阅读:
    29
    28
    27
    950. 按递增顺序显示卡牌
    25
    20190624
    409. 最长回文串
    636. 函数的独占时间
    LeetCode 1046. 最后一块石头的重量(1046. Last Stone Weight) 50
    LeetCode 942. 增减字符串匹配(DI String Match) 49
  • 原文地址:https://www.cnblogs.com/csnd/p/12061913.html
Copyright © 2011-2022 走看看