1. 程式人生 > >設計模式-GOF行為模式(1-5)

設計模式-GOF行為模式(1-5)

行為型的模式有11種:
1. 責任鏈Chain of Responsibility
2. 命令Command
3. 直譯器Interpreter
4. 迭代器Interator
5. 中介者Mediator
6. 備忘錄Memento
7. 監聽Observer
8. 狀態State
9. 策略Strategy
10. 模板Template
11. 訪問者Visitor

1. 責任鏈模式 Chain of Responsibility

傳遞責任響應,類似於債務傳遞 子父¬爺爺…,直到有一方響應的責任,結束了處理。
ChainOfResponsibility

類的實現上: 從Handle繼承的類預設均能把handleRequest處理向上傳遞
Class Handle
{
public:
ARespon(Handle *a){m_pA = a;}
virtual handleRequest ()
{
If (m_pA) m_pA->proc();
}
Private:
Handle *m_pA;
}

2. 命令 Command

常見於對選單命令的響應,選單繫結命令,命令裡面呼叫接收者執行動作。
對於Invoke和Receiver進行了解耦。
Invoke繫結Command,執行Command::execute來進行動作的執行。

同時,Command中可以儲存執行狀態,用於還原。Invoke呼叫Command::unexcute來進行動作的還原。
Command

類的實現上: Command的execute方法呼叫Receiver的動作來執行操作。
class Receiver
{
virtual void action()
{
}
}
class Command
{
virtual execute() = 0;
}
class ConcreteCommand
{
public:
ConcreteCommand(Receiver* pReceiver):m_pReceiver(pReceiver){}
execute()
{
State 儲存;
m_pReceiver->action();
}
unexecute()
{
State還原;
}
Private:
State;
Receiver* m_pReceiver;
}

void main()
{
Receiver* pReceiver = new ConcreteReceiver();
Command* pCommand = new ConcreteCommand(pReceiver);
Invoker* pInvoker = new Invoker(pCommand);

pInvoker->call();

}

時序圖:
CommandSequence

3. 直譯器Interpreter

Interpreter模式提供了評估語言語法或表示式的方法。
此模式主要用於自定義獨特語法的解析。例如某些程式解析自定義表示式。
Interpreter

類的實現上:
TerminalExpression解析字串是否包含指定資訊;
AndExpression包含兩個Expression,解析字串是否同時符合兩個要求。
class Expression
{
public:
virtual bool interpreter();
};

class TerminalExpression : public Expression
{
public:
TerminalExpression(String str):m_data(str){}
bool interpreter(String content)
{
return content.has(m_data);
}
private:
String m_data;
};

class AndExpression : public Expression
{
public:
AndExpression (Expression *expr1, Expression *expr2):m_expr1 (expr1), m_expr2(expr2){}
bool interpreter(String content)
{
return m_expr1->interpreter(content) && m_expr2->interpreter(content);
}
private:
Expression *m_expr1;
Expression *m_expr2;
};

4. 迭代器Iterator

迭代器提供一種順序訪問聚合物件中元素的方法,並且不暴露物件的內部資料結構。
迭代器提供了訪問開始、下一個、是否結束、當前項的統一的介面。
Iterator

類的實現上:
template
class Iterator
{
public:
virtual T* first() = 0;
virtual T* next() = 0;
virtual bool isDone() = 0;
virtual T* current() = 0;
};
class Aggregate
{
public:
virtual Iterator createIterator() = 0;
};
class ConcreteIterator;
class ConcreteAggregate : public Aggregate
{
public:
virtual Iterator* createIterator()
{
return new ConcreteIterator(this);
};
ConcreteIterator Iterator()
{
return ConcreteIterator(this);
};
friend class ConcreteIterator;
void addItem(int i){m_vector.push_back(i);}
protected:
size_t size(){return m_vector.size();};
int* at(size_t i){return m_vector.at(i);}
std::vector m_vector;
};

template
class ConcreteIterator : public Iterator
{
public:
ConcreteIterator(ConcreteAggregate *pAggregate): m_pAggregate(pAggregate), m_cursor (0){};
virtual T* first()
{
m_cursor = 0;
if (m_pAggregate->size() == 0) return nullptr;
return m_pAggregate->at(0);
}
virtual T* next()
{
++m_cursor;
if (m_pAggregate->size() <= m_cursor) return nullptr;
return m_pAggregate->at(m_cursor);
}
virtual bool isDone()
{
return m_cursor < m_pAggregate->size();
}
virtual T* current()
{
if (m_pAggregate->size() <= m_cursor) return nullptr;
return m_pAggregate->at(0);
}
protected:
size_t m_cursor;
ConcreteAggregate *m_pAggregate;
};

int main()
{
ConcreteAggregate aggregate;
aggregate.addItem(0);
aggregate.addItem(1);
aggregate.addItem(2);
Iterator* iter = aggregate.createIterator();
for (; iter->isDone(); iter->next())
{
printf(“%d\n”, *(iter->current()));
}
delete iter;
};

5. 中介者Mediator

用一箇中介物件來封裝一系列物件的互動操作。
中介者使得各物件間不需要顯示的相互引用,使其耦合性降低。

Mediator:中介者定義介面用於與各同事(Colleague)的物件通訊。
Mediator

類的實現上:
class Mediator
{
public:
virtual void ColleagueChange() = 0;
};
class Colleague
{
public:
virtual Mediator* getMediator() = 0;
virtual void action() = 0;
};
class ConcreteColleague1 : public Colleague
{
public:
ConcreteColleague1(Mediator* p):m_pMediator(p){};
virtual Mediator* getMediator()
{
return m_pMediator;
};
virtual void action()
{
printf(“action from colleague1”);
}
private:
Mediator* m_pMediator;
};

class ConcreteColleague2 : public Colleague
{
public:
ConcreteColleague1(Mediator* p):m_pMediator(p){};
virtual Mediator* getMediator()
{
return m_pMediator;
};
virtual void action()
{
printf(“action from colleague2”);
}
private:
Mediator* m_pMediator;
};

class ConcreteMediator : public Mediator
{
public:
ConcreteMediator()
{
m_pCol1 = new ConcreteColleague1(this);
m_pCol2 = new ConcreteColleague2(this);
};
virtual void ColleagueChange()
{
m_pCol1->action();
m_pCol2->action();
};
Colleague colleague1(){return m_pCol1;};
Colleague colleague1(){return m_ pCol2;};
private:
Colleague* m_pCol1;
Colleague* m_pCol2;
};