(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)
行为型的模式有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
参考:https://yuerblog.cc/2017/01/17/gof-design-pattern-of-behavioral-pattern/
参考:http://www.cnblogs.com/gaochundong/p/design_pattern_mediator.html
参考:http://blog.csdn.net/caolaosanahnu/article/details/19647257
传递责任响应,类似于债务传递 子父¬爷爷…,直到有一方响应的责任,结束了处理。
类的实现上: 从Handle继承的类缺省均能把handleRequest处理向上传递
Class Handle
{
public:
ARespon(Handle *a){m_pA = a;}
virtual handleRequest ()
{
If (m_pA) m_pA->proc();
}
Private:
Handle *m_pA;
}
常见于对菜单命令的响应,菜单绑定命令,命令里面调用接收者执行动作。
对于Invoke和Receiver进行了解耦。
Invoke绑定Command,执行Command::execute来进行动作的执行。
同时,Command中可以保存执行状态,用于还原。Invoke调用Command::unexcute来进行动作的还原。
类的实现上: 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();
…
}
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;
};
迭代器提供一种顺序访问聚合对象中元素的方法,并且不暴露对象的内部数据结构。
迭代器提供了访问开始、下一个、是否结束、当前项的统一的接口。
类的实现上:
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;
};
用一个中介对象来封装一系列对象的交互操作。
中介者使得各对象间不需要显示的相互引用,使其耦合性降低。
Mediator:中介者定义接口用于与各同事(Colleague)的对象通信。
类的实现上:
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;
};
(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)