forked from me115/design_patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
52 changed files
with
1,681 additions
and
21 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
.. _behavioral: | ||
|
||
行为型模式 | ||
==================== | ||
|
||
|
||
行为型模式(Behavioral Pattern)是对在不 | ||
同的对象之间划分责任和算法的抽象化。 | ||
|
||
行为型模式不仅仅关注类和对象的结构,而 | ||
且重点关注它们之间的相互作用。 | ||
|
||
通过行为型模式,可以更加清晰地划分类与 | ||
对象的职责,并研究系统在运行时实例对象 | ||
之间的交互。在系统运行时,对象并不是孤 | ||
立的,它们可以通过相互通信与协作完成某 | ||
些复杂功能,一个对象在运行时也将影响到 | ||
其他对象的运行。 | ||
|
||
行为型模式分为类行为型模式和对象行为型模式两种: | ||
|
||
- 类行为型模式:类的行为型模式使用继承关系在几个类之间 | ||
分配行为,类行为型模式主要通过多态等方式来分配父类与 | ||
子类的职责。 | ||
- 对象行为型模式:对象的行为型模式则使用对象的聚合关联 | ||
关系来分配行为,对象行为型模式主要是通过对象关联等方 | ||
式来分配两个或多个类的职责。根据“合成复用原则”,系 | ||
统中要尽量使用关联关系来取代继承关系,因此大部分行为 | ||
型设计模式都属于对象行为型设计模式。 | ||
|
||
**包含模式** | ||
|
||
- 职责链模式(Chain of Responsibility) | ||
重要程度:3 | ||
- 命令模式(Command) | ||
重要程度:4 | ||
- 解释器模式(Interpreter) | ||
重要程度:1 | ||
- 迭代器模式(Iterator) | ||
重要程度:5 | ||
- 中介者模式(Mediator) | ||
重要程度:2 | ||
- 备忘录模式(Memento) | ||
重要程度:2 | ||
- 观察者模式(Observer) | ||
重要程度:5 | ||
- 状态模式(State) | ||
重要程度:3 | ||
- 策略模式(Strategy) | ||
重要程度:4 | ||
- 模板方法模式(Template Method) | ||
重要程度:3 | ||
- 访问者模式(Visitor) | ||
重要程度:1 | ||
|
||
**目录** | ||
|
||
.. toctree:: | ||
:maxdepth: 2 | ||
:numbered: 2 | ||
|
||
simple_factory | ||
factory_method | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/////////////////////////////////////////////////////////// | ||
// ConcreteStateA.cpp | ||
// Implementation of the Class ConcreteStateA | ||
// Created on: 09-十月-2014 17:20:58 | ||
// Original author: colin | ||
/////////////////////////////////////////////////////////// | ||
|
||
#include "ConcreteStateA.h" | ||
#include "ConcreteStateB.h" | ||
#include "Context.h" | ||
#include <iostream> | ||
using namespace std; | ||
|
||
State * ConcreteStateA::m_pState = NULL; | ||
ConcreteStateA::ConcreteStateA(){ | ||
|
||
} | ||
|
||
|
||
|
||
ConcreteStateA::~ConcreteStateA(){ | ||
|
||
} | ||
|
||
|
||
State * ConcreteStateA::Instance() | ||
{ | ||
if ( NULL == m_pState) | ||
{ | ||
m_pState = new ConcreteStateA(); | ||
} | ||
return m_pState; | ||
} | ||
|
||
|
||
void ConcreteStateA::handle(Context * c){ | ||
cout << "doing something in State A.\n done,change state to B" << endl; | ||
c->changeState(ConcreteStateB::Instance()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/////////////////////////////////////////////////////////// | ||
// ConcreteStateA.h | ||
// Implementation of the Class ConcreteStateA | ||
// Created on: 09-十月-2014 17:20:58 | ||
// Original author: colin | ||
/////////////////////////////////////////////////////////// | ||
|
||
#if !defined(EA_84158F08_E96A_4bdb_89A1_4BE2E633C3EE__INCLUDED_) | ||
#define EA_84158F08_E96A_4bdb_89A1_4BE2E633C3EE__INCLUDED_ | ||
|
||
#include "State.h" | ||
|
||
class ConcreteStateA : public State | ||
{ | ||
|
||
public: | ||
virtual ~ConcreteStateA(); | ||
|
||
static State * Instance(); | ||
|
||
virtual void handle(Context * c); | ||
|
||
private: | ||
ConcreteStateA(); | ||
static State * m_pState; | ||
}; | ||
#endif // !defined(EA_84158F08_E96A_4bdb_89A1_4BE2E633C3EE__INCLUDED_) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/////////////////////////////////////////////////////////// | ||
// ConcreteStateB.cpp | ||
// Implementation of the Class ConcreteStateB | ||
// Created on: 09-十月-2014 17:20:59 | ||
// Original author: colin | ||
/////////////////////////////////////////////////////////// | ||
|
||
#include "ConcreteStateB.h" | ||
#include "ConcreteStateA.h" | ||
#include "Context.h" | ||
#include <iostream> | ||
using namespace std; | ||
|
||
State * ConcreteStateB::m_pState = NULL; | ||
ConcreteStateB::ConcreteStateB(){ | ||
|
||
} | ||
|
||
State * ConcreteStateB::Instance() | ||
{ | ||
if ( NULL == m_pState) | ||
{ | ||
m_pState = new ConcreteStateB(); | ||
} | ||
return m_pState; | ||
} | ||
|
||
ConcreteStateB::~ConcreteStateB(){ | ||
|
||
} | ||
|
||
void ConcreteStateB::handle(Context * c){ | ||
cout << "doing something in State B.\n done,change state to A" << endl; | ||
c->changeState(ConcreteStateA::Instance()); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/////////////////////////////////////////////////////////// | ||
// ConcreteStateB.h | ||
// Implementation of the Class ConcreteStateB | ||
// Created on: 09-十月-2014 17:20:59 | ||
// Original author: colin | ||
/////////////////////////////////////////////////////////// | ||
|
||
#if !defined(EA_A6960661_6D76_42cc_98F8_30BF46FBA4CC__INCLUDED_) | ||
#define EA_A6960661_6D76_42cc_98F8_30BF46FBA4CC__INCLUDED_ | ||
|
||
#include "State.h" | ||
|
||
class ConcreteStateB : public State | ||
{ | ||
|
||
public: | ||
static State * Instance(); | ||
|
||
virtual ~ConcreteStateB(); | ||
|
||
virtual void handle(Context * c); | ||
private: | ||
ConcreteStateB(); | ||
static State * m_pState; | ||
}; | ||
#endif // !defined(EA_A6960661_6D76_42cc_98F8_30BF46FBA4CC__INCLUDED_) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/////////////////////////////////////////////////////////// | ||
// Context.cpp | ||
// Implementation of the Class Context | ||
// Created on: 09-十月-2014 17:20:59 | ||
// Original author: colin | ||
/////////////////////////////////////////////////////////// | ||
|
||
#include "Context.h" | ||
#include "ConcreteStateA.h" | ||
|
||
Context::Context(){ | ||
//default is a | ||
m_pState = ConcreteStateA::Instance(); | ||
} | ||
|
||
|
||
|
||
Context::~Context(){ | ||
|
||
} | ||
|
||
|
||
void Context::changeState(State * st){ | ||
m_pState = st; | ||
} | ||
|
||
|
||
void Context::request(){ | ||
m_pState->handle(this); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/////////////////////////////////////////////////////////// | ||
// Context.h | ||
// Implementation of the Class Context | ||
// Created on: 09-十月-2014 17:20:59 | ||
// Original author: colin | ||
/////////////////////////////////////////////////////////// | ||
|
||
#if !defined(EA_F245CF81_2A68_4461_B039_2B901BD5A126__INCLUDED_) | ||
#define EA_F245CF81_2A68_4461_B039_2B901BD5A126__INCLUDED_ | ||
|
||
#include "State.h" | ||
|
||
class Context | ||
{ | ||
|
||
public: | ||
Context(); | ||
virtual ~Context(); | ||
|
||
void changeState(State * st); | ||
void request(); | ||
|
||
private: | ||
State *m_pState; | ||
}; | ||
#endif // !defined(EA_F245CF81_2A68_4461_B039_2B901BD5A126__INCLUDED_) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/////////////////////////////////////////////////////////// | ||
// State.cpp | ||
// Implementation of the Class State | ||
// Created on: 09-十月-2014 17:20:59 | ||
// Original author: colin | ||
/////////////////////////////////////////////////////////// | ||
|
||
#include "State.h" | ||
|
||
|
||
State::State(){ | ||
|
||
} | ||
|
||
|
||
|
||
State::~State(){ | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
void State::handle(Context * c){ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/////////////////////////////////////////////////////////// | ||
// State.h | ||
// Implementation of the Class State | ||
// Created on: 09-十月-2014 17:20:59 | ||
// Original author: colin | ||
/////////////////////////////////////////////////////////// | ||
|
||
#if !defined(EA_8AB26F2E_677E_42cb_98AF_64B7018246A2__INCLUDED_) | ||
#define EA_8AB26F2E_677E_42cb_98AF_64B7018246A2__INCLUDED_ | ||
|
||
class Context; | ||
|
||
class State | ||
{ | ||
|
||
public: | ||
State(); | ||
virtual ~State(); | ||
|
||
virtual void handle(Context * c); | ||
|
||
}; | ||
#endif // !defined(EA_8AB26F2E_677E_42cb_98AF_64B7018246A2__INCLUDED_) |
Oops, something went wrong.