Skip to content

Commit

Permalink
state and strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
me115 committed Oct 9, 2014
1 parent 8750c60 commit 011f084
Show file tree
Hide file tree
Showing 52 changed files with 1,681 additions and 21 deletions.
Binary file added _static/State.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _static/State_eg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _static/Strategy.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _static/seq_State.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _static/seq_State_eg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _static/seq_Strategy.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _static/state1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _static/state2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 64 additions & 0 deletions behavioral_patterns/behavioral.rst
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

39 changes: 39 additions & 0 deletions code/State/ConcreteStateA.cpp
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());
}
27 changes: 27 additions & 0 deletions code/State/ConcreteStateA.h
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_)
36 changes: 36 additions & 0 deletions code/State/ConcreteStateB.cpp
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());
}

26 changes: 26 additions & 0 deletions code/State/ConcreteStateB.h
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_)
30 changes: 30 additions & 0 deletions code/State/Context.cpp
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);
}
26 changes: 26 additions & 0 deletions code/State/Context.h
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_)
27 changes: 27 additions & 0 deletions code/State/State.cpp
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){

}
23 changes: 23 additions & 0 deletions code/State/State.h
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_)
Loading

0 comments on commit 011f084

Please sign in to comment.