Home > Software engineering >  Recently in learning design patterns, wrote down his own understanding, by the way..
Recently in learning design patterns, wrote down his own understanding, by the way..

Time:11-03

Summary:
Hard work for a year, finally can a raise, submit to the director for a raise, head a see, their functions and powers is not enough, batch no, director of the application is turned over to the director, director, found himself can't batch, apply to the general manager, general manager of a look, guy out breath, with brave dare to apply, talk to settle first, predict how things, look at next.
This is a typical chain of responsibility pattern, request processing formed a chain, until there is an object handle the request, the behaviour of the chain of responsibility pattern is a kind of object, in the chain of responsibility pattern, many objects from each object reference of its buyers together to form a chain, the request on the chain transfer, until a certain object on the chain decided to handle the request, send the request of the client does not know which one of the chain eventually deal with the request object, which makes the system can dynamically without affecting the clients situation reorganize the chain and allocation of responsibilities,

Class and instance:
Abstract Handler (Handler) role: define an interface for processing the request, if need be, interface can define a method, to set up and return to buyers of references, this role is often by an abstract class or interface implementation,
Specific role (ConcreteHandler) processing: specific treatment after receiving the request, can choose will be asked to dispose of, or the request to the buyers, due to the specific processing hold for buyers reference, therefore, if you need, specific processor can access,
Sample:
C/C + + code
?
1
2
3
4
5
6
7
8
9
10
11.
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include
#include
using namespace std;

//request
The class Request
{
Public:
Int m_nNumber;
};

//manager
The class Manager
{
Public:
Manager (string temp) {name=temp; }
Void SetSuccessor (temp) Manager * {Manager=temp; }
Virtual void GetRequest (Request * Request)=0.
Protected:
Manager * Manager;
String name;
};

//manager
The class CommonManager: public Manager
{
Public:
CommonManager (string strTemp) : the Manager (strTemp) {}
Virtual void GetRequest (Request * Request);
};

Void CommonManager: : GetRequest (Request * Request)
{
If (request - & gt; M_nNumber & gt;=0 & amp; & Request - & gt; M_nNumber & lt; 1000)
{
Cout & lt; }
The else
{
Manager - & gt; GetRequest (request);
}
}

//director
The class Majordomo: public Manager
{
Public:
Majordomo (string strTemp) : the Manager (strTemp) {}
Virtual void GetRequest (Request * Request);
};

Void Majordomo: : GetRequest (Request * Request)
{
If (request - & gt; M_nNumber & lt;
=5000){
Cout & lt; } the else
{
Manager - & gt; GetRequest (request);
}
}

//general manager
The class GeneralManager: public Manager
{
Public:
GeneralManager (string name) : the Manager (name) {}
Virtual void GetRequest (Request * Request)//general manager to handle all requests
{
Cout & lt; }
};

Int main () {

Manager * common=new CommonManager (" Manager zhang ");
Manager * major=new Majordomo (" director li ");
General GeneralManager *=new GeneralManager (" Zhao Zong ");
Common - & gt; SetSuccessor (major);
Major - & gt; SetSuccessor (general);
Request * rq=new Request ();

Rq - & gt; M_nNumber=999;
Common - & gt; GetRequest (rq);

Rq - & gt; M_nNumber=4999;
Common - & gt; GetRequest (rq);

Rq - & gt; M_nNumber=6999;
Common - & gt; GetRequest (rq);

The delete rq.
The delete major;
The delete common;
Delete the general;
return 0;
}


Key points and implementation:
1. Note: a request to the end of the chain may also have no, so must be configured properly.
2. The chain of responsibility pattern does not create a chain of responsibility, to the rest of the chain of responsibility should be created by system created,
3. The chain of responsibility pattern reduces the request of the sender and the receiver, the coupling between the multiple object has a chance to deal with this request, a chain can be a line, a tree, or it can be a ring, as shown in the figure below, the chain of responsibility is a part of the tree structure,


See where you raise application is general... : -))

I have the understanding of the other has to share blog:
I understand design patterns (c + + implementation) - Chain Of Responsibility Pattern (Chain Of Responsibility Pattern)
I understand design patterns (c + + implementation) - Proxy mode (the Proxy Pattern)
I understand design patterns (c + + implementation) - the Flyweight Pattern (the Flyweight Pattern)
I understand design patterns (c + + implementation) - appearance model (the Facade Pattern)
I understand design patterns (c + + implementation) - Decorator Pattern (the Decorator Pattern)
I understand design patterns (c + + implementation) - portfolio model (Composite Pattern)
I understand design patterns (c + + implementation) - Adapter Pattern (Adapter Pattern)
I understand design patterns (c + + implementation) - Prototype model (Prototype Pattern)
I understand the design of the model (c + + implementation) - Builder (Builder Pattern)
I understand design patterns (c + + implementation) - the Abstract Factory Pattern (the Abstract Factory Pattern)
I understand design patterns (c + + implementation), Factory Method Pattern, Factory Method Pattern)
I understand design patterns (c + + implementation) - Simple Factory Pattern (Simple Factory Pattern)
I understand design mode (c + + implementation) - Bridge (Bridge Pattern)
I understand design patterns (c + + implementation) - Singleton Pattern, Singleton Pattern)

CodePudding user response:

The look and Windows messaging mechanism is to imagine

Send a message (eg user buttons), to send a message queue, message loop to extract the message, sent to the specified window, specify the window for processing, or converted to other message, sent to other places, if you don't need to deal with the message, sent to the default function processing,

CodePudding user response:

Words, LZ send the wrong plate nullnull
  •  Tags:  
  • API
  • Related