[Boolan] C++第十三周 C++设计模式(三)
来源:互联网 发布:vc打印机编程 编辑:程序博客网 时间:2024/06/10 18:25
设计模式
状态变化 模式
1. Memento - 备忘录
Memento 模式的关键就是要在不破坏封装行的前提下,捕获并保存一个类的内部 状态,这样就可以利用该保存的状态实施恢复操作
#include <iostream>#include <string>using namespace std;class Originator;class Memento{ private: friend class Originator; typedef string State; Memento() :_sdt(""){} Memento(const State& sdt) :_sdt(sdt){} ~Memento(){} void SetState(const State& sdt) { _sdt = sdt; } State GetState(){ return _sdt; } private: State _sdt;};class Originator{ public: typedef string State; Originator() :_sdt(""), _mt(NULL) {} Originator(const State& sdt) :_sdt(sdt), _mt(NULL) {} ~Originator(){} Memento* CreateMemento() { return new Memento(_sdt); } void SetMemento(Memento* men) { men = _mt; } void RestoreToMemento(Memento* mt) { this->_sdt = mt->GetState(); } State GetState() { return _sdt; } void SetState(const State& sdt) { _sdt = sdt; } void PrintState() { cout << this->_sdt << "..." << endl; } private: State _sdt; Memento* _mt;};int main(int argc, char* argv[]) { Originator* o = new Originator(); o->SetState("old"); //备忘前状态 o->PrintState(); Memento* m = o->CreateMemento(); o->SetState("new"); //修改状态 o->PrintState(); o->RestoreToMemento(m); //恢复状态 o->PrintState(); return 0;}
2. State - 状态模式
允许一个对象在其内部状态改变时改变它的行为。从而使对象看起来似乎修改了其行为。
#include <iostream>using namespace std;class State;class Context{ public: Context() :_state(NULL){ } Context(State* state){ this->_state = state; } ~Context(){ } void OperationInterface(); void OperationChangeState(); private: friend class State; bool ChangeState(State* state) { this->_state = state; return true; } private: State* _state;};class State{ public: State(){} virtual ~State(){} virtual void OperationInterface(Context*) { cout << "State::..." << endl; } virtual void OperationChangeState(Context*) = 0; protected: bool ChangeState(Context* con, State* st){ con->ChangeState(st); return true; }};class ConcreteStateB;class ConcreteStateA : public State{ public: ConcreteStateA(){} virtual ~ConcreteStateA(){} virtual void OperationInterface(Context*){ cout << "ConcreteStateA::OperationInterface..." << endl; } virtual void OperationChangeState(Context* con);};class ConcreteStateB : public State{ public: ConcreteStateB(){} virtual ~ConcreteStateB(){} virtual void OperationInterface(Context*){ cout << "ConcreteStateB::OperationInterface..." << endl; } virtual void OperationChangeState(Context* con){ OperationInterface(con); this->ChangeState(con, new ConcreteStateA); }};void ConcreteStateA::OperationChangeState(Context* con){ OperationInterface(con); this->ChangeState(con, new ConcreteStateB);}void Context::OperationInterface(){ _state->OperationInterface(this);}void Context::OperationChangeState(){ _state->OperationChangeState(this);}int main(int argc, char *argv[]){ State *st = new ConcreteStateA(); Context* con = new Context(st); con->OperationChangeState(); con->OperationChangeState(); con->OperationChangeState(); if(con != NULL) delete con; if(st != NULL) st = NULL; return 0;}
数据结构式
1. Composite - 组合模式
将对象组合成树形结构以表示“部分-整体”的层次结构。 Composite使得用户对单个对象和组合对象的使用具有一致 性(稳定)。
#include <iostream>#include <vector>using namespace std;class Component{ public: Component(){} ~Component(){} public: virtual void Operation() = 0; virtual void Add(const Component&) { } virtual void Remove(const Component&){ } virtual Component* GetChild(int) { return NULL; }};class Composite : public Component{ public: Composite(){} ~Composite(){} void Operation(){ vector<Component*>::iterator comIter = comVec.begin(); for(; comIter != comVec.end(); comIter++) { (*comIter)->Operation(); } } void Add(Component* com){ comVec.push_back(com); } void Remove(Component * com) { vector<Component*>::iterator iterVec = comVec.begin(); for(; iterVec != comVec.end(); iterVec++) { if(*iterVec == com) { comVec.erase(iterVec); return; } } } Component* GetChild(int index) { return comVec[index]; } private: vector<Component*> comVec;};class Leaf : public Component{ public: Leaf(){} ~Leaf(){} void Operation() { cout << "Leaf operation..." << endl; }};int main(int argc, char *argv[]){ Leaf * l = new Leaf(); l->Operation(); Composite* com = new Composite(); com->Add(l); com->Operation(); cout << "----------------" << endl; Component* ll = com->GetChild(0); ll->Operation(); return 0;}
2. Chain of Responsibility - 职责链
使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递请求,直到有一个对象处理它为止。
#include <iostream>using namespace std;class Handle{ public: virtual ~Handle(){ delete _succ; } virtual void HandleRequest() = 0; void SetSuccessor(Handle* succ){ _succ = succ; } Handle* GetSuccessor(){ return _succ; } protected: Handle() :_succ(0){} Handle(Handle* succ) : _succ(succ){} private: Handle* _succ;};class ConcreteHandleA : public Handle{ public: ConcreteHandleA(){} ~ConcreteHandleA(){} ConcreteHandleA(Handle* succ) :Handle(succ){} void HandleRequest(){ if(this->GetSuccessor() != 0) { cout << "ConcreteHandleA 我把处理权给后继节点..." << endl; this->GetSuccessor()->HandleRequest(); } else{ cout << "ConcreteHandleA 没有后继了,我必须自己处理..." << endl; } }};class ConcreteHandleB : public Handle{ public: ConcreteHandleB(){} ~ConcreteHandleB(){} ConcreteHandleB(Handle* succ) :Handle(succ){} void HandleRequest(){ if(this->GetSuccessor() != 0) { cout << "ConcreteHandleB 我把处理权给后继节点..." << endl; this->GetSuccessor()->HandleRequest(); } else{ cout << "ConcreteHandleB 没有后继了,我必须自己处理..." << endl; } }};int main(int argc, char *argv[]) { Handle* h1 = new ConcreteHandleA(); Handle* h2 = new ConcreteHandleB(); h1->SetSuccessor(h2); h1->HandleRequest(); return 0;}
行为变化
2. Visitor - 访问器
表示一个作用于某对象结构中的各元素的操作。使得可以在不改变(稳定)各元素的类的前提下定义(扩展)作用于这些元素的新操作(变化)。
#include <iostream>using namespace std;class Element;class Visitor{ public: virtual ~Visitor(){} virtual void VisitConcreteElementA(Element* elm) = 0; virtual void VisitConcreteElementB(Element* elm) = 0; protected: Visitor(){}};class ConcreteVisitorA : public Visitor{ public: ConcreteVisitorA(){} virtual ~ConcreteVisitorA(){} virtual void VisitConcreteElementA(Element* elm){ cout << "I will visit ConcreteElementA..." << endl; } virtual void VisitConcreteElementB(Element* elm){ cout << "I will visit ConcreteElementB..." << endl; }};class ConcreteVisitorB : public Visitor{ public: ConcreteVisitorB(){} virtual ~ConcreteVisitorB(){} virtual void VisitConcreteElementA(Element* elm){ cout << "I will visit ConcreteElementA..." << endl; } virtual void VisitConcreteElementB(Element* elm){ cout << "I will visit ConcreteElementB..." << endl; }};class Element{ public: virtual ~Element(){} virtual void Accept(Visitor* vis) = 0; protected: Element(){}};class ConcreteElementA : public Element{ public: ConcreteElementA(){} ~ConcreteElementA(){} void Accept(Visitor *vis) { vis->VisitConcreteElementA(this); cout << "visiting ConcreteElementA..." << endl; }};class ConcreteElementB : public Element{ public: ConcreteElementB(){} ~ConcreteElementB(){} void Accept(Visitor *vis) { cout << "visiting ConcreteElementB..." << endl; vis->VisitConcreteElementB(this); }};int main(int argc, char *argv[]) { Visitor* vis = new ConcreteVisitorA(); Element* elm = new ConcreteElementA(); elm->Accept(vis); return 0;}
3. Interpreter - 解析器
给定一个语言,定义它的文法的一种表示,并定义一种解释器,这个解释器使用该表示来解释语言中的句子。
#include <iostream>#include <string>using namespace std;class Context{ public: Context(){} ~Context(){}};class AbstractExpression{ public: virtual ~AbstractExpression(){} virtual void Interpret(const Context& c) { } protected: AbstractExpression(){}};class TerminalExpression : public AbstractExpression{ public: TerminalExpression(const string statement) : _statement(statement){} ~TerminalExpression(){} void Interpret(const Context& c) { cout << this->_statement << " TerminalExpression" << endl; } private: string _statement;};class NonterminalExpression : public AbstractExpression{ public: NonterminalExpression(AbstractExpression* expression, int times) : _expression(expression), _times(times) { } ~NonterminalExpression(){} void Interpret(const Context& c) { for(int i = 0; i < _times; i++) { this->_expression->Interpret(c); } } private: AbstractExpression* _expression; int _times;};int main(int argc, char *argv[]){ Context *c = new Context(); AbstractExpression *te = new TerminalExpression("Hello"); AbstractExpression* nte = new NonterminalExpression(te, 2); nte->Interpret(*c); return 0;}
0 0
- [Boolan] C++第十三周 C++设计模式(三)
- 【boolan c++】面向对象的三大方式以及设计模式
- Boolan 设计模式 第二周
- [Boolan] C++第十二周 C++设计模式(二)
- (Boolan)C++设计模式 <三> ——策略模式(Strategy)
- Boolan C++设计模式 第二周笔记
- Boolan C++设计模式 第三周笔记
- Boolan C++ 设计模式 第三周
- Boolan 设计模式_1
- Boolan 设计模式_2
- Boolan 设计模式_3
- boolan——c++学习笔记之设计模式三
- [boolan]设计模式的学习
- [boolan]设计模式学习二
- [boolan]设计模式 观察者学习
- Boolan 设计模式 第一周
- 设计模式(C++)
- C 语言设计模式(三) 双链表
- mysql索引优化篇(一)
- java保留整数
- 黏滞位的作用以及如何设置
- 解决spring4.x中的MappingJacksonHttpMessageConverter错误
- Apache common collection的使用(3)
- [Boolan] C++第十三周 C++设计模式(三)
- 跟着第一行代码学安卓(第二版书)-1
- NIO Buffer To String
- BP学习记录
- bzoj1420&bzoj1319 Discrete Root
- Linux服务器基本安全防范
- BootStrap(二) -- 在表格行添加按钮并取得选中行数据
- HDU
- python PIL的win64安装经验