Design Pattern Examples
Overview of object-oriented design patterns
HandlerChain_Exercise.cs
Go to the documentation of this file.
1
5
6using System;
7using System.Collections.Generic;
8using System.Linq;
9using System.Text;
10using System.Threading.Tasks;
11
13{
29 internal class HandlerChain_Exercise
30 {
38 {
39 // Note: This creates each window and adds the new window to the given
40 // HandlerChain object.
41 //
42 // This example doesn't care about each individual window so the
43 // return value is ignored.
44 MessageWindow.CreateWindow("Window 1", 0, 0, 10, 10, handlerChain);
45 MessageWindow.CreateWindow("Window 2", 20, 0, 5, 5, handlerChain);
46 MessageWindow.CreateWindow("Window 3", 30, 10, 15, 15, handlerChain);
47 }
48
52 // ! [Using HandlerChain in C#]
53 public void Run()
54 {
55 Console.WriteLine();
56 Console.WriteLine("Handler Chain Exercise");
57
58 // Construct a handler chain and populate with windows that can
59 // handle messages.
60 HandlerChain handlerChain = new HandlerChain();
62
63 Console.WriteLine(" Handler Chain at start:");
64 Console.WriteLine(handlerChain);
65
66 // Now pass messages to the windows.
67
68 Console.WriteLine(" Select Window 2");
69 handlerChain.SendMessage(new Message(MessageType.ButtonDown, new MessagePosition(22, 1)));
70 handlerChain.SendMessage(new Message(MessageType.ButtonUp, new MessagePosition(22, 1)));
71 Console.WriteLine(" Current handler chain:");
72 Console.WriteLine(handlerChain);
73
74 Console.WriteLine(" Select Window 3");
75 handlerChain.SendMessage(new Message(MessageType.ButtonDown, new MessagePosition(35, 11)));
76 handlerChain.SendMessage(new Message(MessageType.ButtonUp, new MessagePosition(35, 11)));
77 Console.WriteLine(" Current handler chain:");
78 Console.WriteLine(handlerChain);
79
80 Console.WriteLine(" Select Window 1");
81 handlerChain.SendMessage(new Message(MessageType.ButtonDown, new MessagePosition(4, 4)));
82 handlerChain.SendMessage(new Message(MessageType.ButtonUp, new MessagePosition(4, 4)));
83 Console.WriteLine(" Current handler chain:");
84 Console.WriteLine(handlerChain);
85
86 Console.WriteLine(" Close Window 2");
87 handlerChain.SendMessage(new Message(MessageType.ButtonDown, new MessagePosition(24, 0)));
88 handlerChain.SendMessage(new Message(MessageType.ButtonUp, new MessagePosition(24, 0)));
89 Console.WriteLine(" Current handler chain:");
90 Console.WriteLine(handlerChain);
91
92 Console.WriteLine(" Done.");
93 }
94 // ! [Using HandlerChain in C#]
95 }
96}
Example of using the HandlerChain Pattern or Chain of Responsibility pattern in C#.
void _HandlerChain_ConstructWindowChain(HandlerChain handlerChain)
Helper method to construct a list of windows. Messages will be passed to these windows via the Handle...
void Run()
Executes the example for the HandlerChain Pattern in C#.
Represents a list of handlers that all implement the IMessageHandler interface. This list can be dyna...
void SendMessage(Message message)
Send a message to each of the handlers in the list, protected by a multi-threading lock.
Represents a rectangular region that can handle messages directed to that region.
static MessageWindow CreateWindow(string title, int x, int y, int width, int height, HandlerChain handlerChain)
Creates an instance of the MessageWindow class with the specified attributes and adds the new instanc...
The namespace containing all Design Pattern Examples implemented in C#.
MessageType
Type of message handled by MessageWindow.
Represents a message sent to the windows. A message contains a type and a position.
Position of the message in global coordinates (same scope of coordinates as windows)....