设计模式 - ChainOfResponsibility模式(职责链模式)
作用:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
UML结构图:
抽象基类:
1)Handler:定义一个处理请求的接口,在图中这个接口就是HandleRequset函数,这个类同时有一个指向Handler对象的指针,指向后续的处理请求的对象(如果有的话)。
解析:
这个模式把可以处理一个请求的对象以链的形式连在了一起,让这些对象都有处理请求的机会.好比原来看古装电视中经常看到皇宫中召见某人的时候,太监们(可以处理一个请求的对象)就会依次的喊:传XX...这样一直下去直到找到这个人为止.ChainOfResponsibility模式也是这样的处理请求的,如果有后续的对象可以处理,那么传给后续的对象处理,否则就自己处理请求.这样的设计把请求的发送者和请求这种的处理者解耦了,好比发号的皇帝不知道到底是哪个太监最后会找到他要找到的人一般,只管发出命令就OK了。
代码实现:
ChainOfResponsibility.h
1
2 #ifndef CHAINOFRESPONSIBILITY_H
3 #define CHAINOFRESPONSIBILITY_H
4
5 #include <stdio.h>
6
7 // 抽象基类,定义一个处理请求的接口
8 class Handler
9 {
10 public:
11 Handler(Handler *pSuccessor = NULL);
12 virtual ~Handler();
13
14 // 纯虚函数,由派生类实现
15 virtual void HandleRequset() = 0;
16
17 protected:
18 Handler* m_pSuccessor;
19 };
20
21 class ConcreateHandler1
22 : public Handler
23 {
24 public:
25 ConcreateHandler1(Handler *pSuccessor = NULL) : Handler(pSuccessor){}
26 virtual ~ConcreateHandler1(){}
27
28 virtual void HandleRequset();
29 };
30
31 class ConcreateHandler2
32 : public Handler
33 {
34 public:
35 ConcreateHandler2(Handler *pSuccessor = NULL) : Handler(pSuccessor){}
36 virtual ~ConcreateHandler2(){}
37
38 virtual void HandleRequset();
39 };
40
41 #endif
42
2 #ifndef CHAINOFRESPONSIBILITY_H
3 #define CHAINOFRESPONSIBILITY_H
4
5 #include <stdio.h>
6
7 // 抽象基类,定义一个处理请求的接口
8 class Handler
9 {
10 public:
11 Handler(Handler *pSuccessor = NULL);
12 virtual ~Handler();
13
14 // 纯虚函数,由派生类实现
15 virtual void HandleRequset() = 0;
16
17 protected:
18 Handler* m_pSuccessor;
19 };
20
21 class ConcreateHandler1
22 : public Handler
23 {
24 public:
25 ConcreateHandler1(Handler *pSuccessor = NULL) : Handler(pSuccessor){}
26 virtual ~ConcreateHandler1(){}
27
28 virtual void HandleRequset();
29 };
30
31 class ConcreateHandler2
32 : public Handler
33 {
34 public:
35 ConcreateHandler2(Handler *pSuccessor = NULL) : Handler(pSuccessor){}
36 virtual ~ConcreateHandler2(){}
37
38 virtual void HandleRequset();
39 };
40
41 #endif
42
ChainOfResponsibility.cpp
1 #include "ChainOfResponsibility.h"
2 #include <iostream>
3
4 Handler::Handler(Handler *pSuccessor /* = NULL*/)
5 : m_pSuccessor(pSuccessor)
6 {
7 }
8
9 Handler::~Handler()
10 {
11 delete m_pSuccessor;
12 m_pSuccessor = NULL;
13 }
14
15 void ConcreateHandler1::HandleRequset()
16 {
17 if (NULL != m_pSuccessor)
18 {
19 m_pSuccessor->HandleRequset();
20 }
21 else
22 {
23 std::cout << "HandleRequset by ConcreateHandler1\n";
24 }
25 }
26
27 void ConcreateHandler2::HandleRequset()
28 {
29 if (NULL != m_pSuccessor)
30 {
31 m_pSuccessor->HandleRequset();
32 }
33 else
34 {
35 std::cout << "HandleRequset by ConcreateHandler2\n";
36 }
37 }
38
2 #include <iostream>
3
4 Handler::Handler(Handler *pSuccessor /* = NULL*/)
5 : m_pSuccessor(pSuccessor)
6 {
7 }
8
9 Handler::~Handler()
10 {
11 delete m_pSuccessor;
12 m_pSuccessor = NULL;
13 }
14
15 void ConcreateHandler1::HandleRequset()
16 {
17 if (NULL != m_pSuccessor)
18 {
19 m_pSuccessor->HandleRequset();
20 }
21 else
22 {
23 std::cout << "HandleRequset by ConcreateHandler1\n";
24 }
25 }
26
27 void ConcreateHandler2::HandleRequset()
28 {
29 if (NULL != m_pSuccessor)
30 {
31 m_pSuccessor->HandleRequset();
32 }
33 else
34 {
35 std::cout << "HandleRequset by ConcreateHandler2\n";
36 }
37 }
38
Main.cpp
1
2 #include "ChainOfResponsibility.h"
3 #include <stdlib.h>
4
5 int main()
6 {
7 Handler *p1 = new ConcreateHandler1();
8 Handler *p2 = new ConcreateHandler2(p1);
9
10 p2->HandleRequset();
11
12 delete p2;
13
14 system("pause");
15
16 return 0;
17 }
2 #include "ChainOfResponsibility.h"
3 #include <stdlib.h>
4
5 int main()
6 {
7 Handler *p1 = new ConcreateHandler1();
8 Handler *p2 = new ConcreateHandler2(p1);
9
10 p2->HandleRequset();
11
12 delete p2;
13
14 system("pause");
15
16 return 0;
17 }