标签:面试 分享图片 cout push pattern lse gets etl code
#include<iostream> #include<string> #include<vector> using namespace std; class memento{//用于记录状态的类 string state; public: memento(string s){ state = s; } string getState(){ return state; } }; class originator{//当前状态类 string state; public: void setState(string s){ state = s; } string getState(){ return state; } memento *saveStateToMemento(){ return new memento(state); } void getStateFromMemento(memento * m){ state = m->getState(); } }; class CareTaker{//管理过去状态的类 public: vector<memento*> mementoList; void add(memento* m){ mementoList.push_back(m); } memento * get(int index){ return mementoList[index]; } memento * getLast(){ if(mementoList.empty()) return nullptr; else{ memento* temp = mementoList.back(); mementoList.pop_back(); return temp; } } }; class MementoPatternDemo{ public: static void method(){ originator * ori = new originator(); CareTaker * ct = new CareTaker(); ori->setState("State #1"); ori->setState("State #2"); ct->add(ori->saveStateToMemento()); ori->setState("State #3"); ct->add(ori->saveStateToMemento()); ori->setState("State #4"); cout<<"Current state:"<<ori->getState()<<endl; memento * lastState; while(lastState = ct->getLast()){ ori->getStateFromMemento(lastState); cout<<"Last state is:"<<ori->getState()<<endl; } // ori->getStateFromMemento(ct->get(0)); // cout<<"Current state:"<<ori->getState()<<endl; // ori->getStateFromMemento(ct->get(1)); // cout<<"Current state:"<<ori->getState()<<endl; } }; int main(){ MementoPatternDemo::method(); }
类图:
在上一次面试中考到了、、、当时没答出来,感觉这个设计模式还是挺有用的,特别是在编辑器或者整个流程之中有undo,redo等需要记录过程状态时。
标签:面试 分享图片 cout push pattern lse gets etl code
原文地址:https://www.cnblogs.com/J1ac/p/9693570.html