Design Pattern Examples
Overview of object-oriented design patterns
HandlerChain_Exercise.cpp
Go to the documentation of this file.
1
5
6#include <iostream>
7
12
13namespace // Anonymous
14{
15 using namespace DesignPatternExamples_cpp;
16
24 {
25 // Note: This creates each window and adds the new window to the given
26 // HandlerChain object.
27 //
28 // This example doesn't care about each individual window so the
29 // return value is ignored.
30 MessageWindow::CreateWindow("Window 1", 0, 0, 10, 10, handlerChain);
31 MessageWindow::CreateWindow("Window 2", 20, 0, 5, 5, handlerChain);
32 MessageWindow::CreateWindow("Window 3", 30, 10, 15, 15, handlerChain);
33 }
34
35} // end namespace Anonmyous
36
37
39{
40
56 // ! [Using HandlerChain in C++]
58 {
59 std::cout << std::endl;
60 std::cout << "HandlerChain Exercise" << std::endl;
61
62 // Construct a handler chain and populate with windows that can
63 // handle messages.
64 HandlerChain::unique_ptr_t handlerChain = std::make_unique<HandlerChain>();
65 _HandlerChain_ConstructWindowChain(handlerChain.get());
66
67 std::cout << " Handler Chain at start:" << std::endl;
68 std::cout << handlerChain->ToString() << std::endl;
69
70 // Now pass messages to the windows.
71
72 std::cout << " Select Window 2" << std::endl;
73 handlerChain->SendMessage(std::make_unique<Message>(MessageType::ButtonDown, MessagePosition(22, 1)).get());
74 handlerChain->SendMessage(std::make_unique<Message>(MessageType::ButtonUp, MessagePosition(22, 1)).get());
75 std::cout << " Current handler chain:" << std::endl;
76 std::cout << handlerChain->ToString() << std::endl;
77
78 std::cout << " Select Window 3" << std::endl;
79 handlerChain->SendMessage(std::make_unique<Message>(MessageType::ButtonDown, MessagePosition(35, 11)).get());
80 handlerChain->SendMessage(std::make_unique<Message>(MessageType::ButtonUp, MessagePosition(35, 11)).get());
81 std::cout << " Current handler chain:" << std::endl;
82 std::cout << handlerChain->ToString() << std::endl;
83
84 std::cout << " Select Window 1" << std::endl;
85 handlerChain->SendMessage(std::make_unique<Message>(MessageType::ButtonDown, MessagePosition(4, 4)).get());
86 handlerChain->SendMessage(std::make_unique<Message>(MessageType::ButtonUp, MessagePosition(4, 4)).get());
87 std::cout << " Current handler chain:" << std::endl;
88 std::cout << handlerChain->ToString() << std::endl;
89
90 std::cout << " Close Window 2" << std::endl;
91 handlerChain->SendMessage(std::make_unique<Message>(MessageType::ButtonDown, MessagePosition(24, 0)).get());
92 handlerChain->SendMessage(std::make_unique<Message>(MessageType::ButtonUp, MessagePosition(24, 0)).get());
93 std::cout << " Current handler chain:" << std::endl;
94 std::cout << handlerChain->ToString() << std::endl;
95
96 std::cout << " Done." << std::endl;
97 }
98 // ! [Using HandlerChain in C++]
99
100} // end namespace
Implementation of the HandlerChain class and declaration of the IMessageHandler interface used in the...
static void _HandlerChain_ConstructWindowChain(int windowIds[3])
Helper function to construct a list of windows. Messages will be passed to these windows via the Hand...
Implementation of the Message and MessagePosition structs used in the HandlerChain Pattern.
Declaration of the MessageWindow class and WindowRectangle class used in the HandlerChain Pattern.
Represents a list of handlers that all implement the IMessageHandler interface. This list can be dyna...
std::unique_ptr< HandlerChain > unique_ptr_t
Declaration of the HandlerChain_Exercise() function as used in the HandlerChain Pattern.
The namespace containing all Design Pattern Examples implemented in C++.
void HandlerChain_Exercise()
Example of using the Handle Chain or Chain of Responsibility design pattern.
@ ButtonUp
Take an action on the currently selected window.
@ ButtonDown
Selects a window based on position.
Position of the message in global coordinates (same scope of coordinates as windows)....