C++ 設計模式 狀態模式直譯器模式迭代器模式(含程式碼)

前言:

很開心自己的筆記收穫了一百位粉絲,想寫在前面告訴大家,我也是一位跨專業學生,自己也在學習的過程中,釋出的文章均為自己的筆記(未校對),不建議一些正在學習的同學把我的筆記作為資料。大家還是找一些專業的輔導機構或者是大v。有錯誤的地方會及時糾正。謝謝大家

狀態模式

就是物件的行為依賴於他所處的狀態

透過使用者的狀態改變使用者的行為

案例

我在八點起床,吃早飯,九點出發去上班,十點開始工作,十點吃午飯,我在不同的時間要幹不同的事情。

#include#include“string”#include“list”using namespace std;class State{public: virtual void doSomeThing(Worker *w) = 0;};class Worker{public: Worker(); int getHour() { return m_hour; } void setHour(int hour) { m_hour = hour; } State* getCurrentState() { return m_currstate; } void setCurrentState(State* state) { m_currstate=state; } void doSomeThing() { m_currstate->doSomeThing(this); }private: int m_hour; State *m_currstate; };class Statel :public State{public: void doSomeThing(Worker *w);};class State2 :public State{public: void doSomeThing(Worker *w);};void Statel::doSomeThing(Worker *w){ if (w->getHour() == 7 || w->getHour() == 8) { cout << “吃早飯” << endl; } else { delete w->getCurrentState(); w->getCurrentState(new State2); w->getCurrentState()->doSomeThing(w); }}void State2::doSomeThing(Worker *w){ if (w->getHour() == 9 || w->getHour() == 10) { cout <<“逛街,買衣服,買鞋子” << endl; } else { delete w->getCurrentState(); w->getCurrentState(new State1); cout << “當前時間” << w->getHour() << “未知狀態 ” << endl; }}Worker::Worker(){ m_currstate = new State1;}void main(){ Worker *w1 = new Worker; w1->getHour(7); w1->doSomeThing(); w1->getHour(9); w1->doSomeThing(); delete w1; cout << “hello。。。” << endl; system(“pause”); return;}

直譯器模式

就是描述瞭如何為簡單的語言定義一個語法,如何在該語言中表示一個句子,以及如何解釋這些句子

#include#include“string”#include“list”using namespace std;class Context{public: Context(int num) { this->m_num = num; } int getNum() { return m_num; } int getRes() { return m_res; } void setNum(int num) { this->m_num = num; } void setRes(int res) { this->m_res = res; }private: int m_num; int m_res;};class Experssion{public: virtual void interpreter(Context *context) = 0;private: Context *m_context;};//加法class PlusExpression :public Experssion{public: PlusExpression() { this->context =NULL; } virtual void interpreter(Context*context) { int num = context->getNum(); num++; context->setNum(num); context->setRes(num); }private: Context *context;};class MinusExpression :public Experssion{public: MinusExpression() { this->context = NULL; } virtual void interpreter(Context*context) { int num = context->getNum(); num——; context->setNum(num); context->setRes(num); }private: Context *context;};void main(){ Experssion *experssion = NULL; Context *context = NULL; Experssion *experssion2 = NULL; context = new Context(10); cout << context->getNum() << endl; experssion = new PlusExpression; expression->interpreter(context); cout << context->getRes() << endl; experssion2 = new MinusExpression; expression2->interpreter(context); cout << context->getRes() << endl; cout << “hello。。。” << endl; system(“pause”); return;}

迭代器模式

是提供了一種方法順序來訪問一個聚合物件中的各個元素,而又不需要暴露該物件的內部表示

#include“iostream”using namespace std;typedef int Object;#define SIZE 5class MyIterator{public: virtual void First() = 0; virtual void Next() = 0; virtual bool IsDone() = 0; virtual Object CurrentItem() = 0;};class Aggregate{public: virtual Object getItem(int index) = 0; virtual MyIterator *CreateIterator() = 0; virtual int getSize() = 0;protected : Object object[SIZE];};class ContreteIterator :public MyIterator{ public: ContreteIterator(Aggregate *ag) { _ag = ag; _current_index = 0; } virtual void First() { _current_index = 0; } virtual void Next() { if (_current_index < _ag->getSize()) { _current_index++; } } virtual bool IsDone() { return (_current_index == _ag->getSize()); } virtual Object CurrentItem() { return _ag->getItem(_current_index); } private: int _current_index; Aggregate *_ag;};class ContreteAggregate :public Aggregate{public: ContreteAggregate() { for (int i = 0; i < SIZE; i++) { object[i] = i + 100; } } virtual Object getItem(int index) { return object[index]; } virtual MyIterator *CreateIterator() { return new ContreteIterator(this); } virtual int getSize() { return SIZE; }};void main(){ Aggregate *ag = new ContreteAggregate; MyIterator *it = ag->CreateIterator(); for (; !(it->IsDone()); it->Next()) { cout << it->CurrentItem() << “”; } delete it; delete ag; system(“pause”); return;}