Design Pattern Examples
Overview of object-oriented design patterns
HandlerChain_Class.cs
Go to the documentation of this file.
1
6
7using System;
8using System.Collections.Generic;
9using System.Text;
10
12{
18 public interface IMessageHandler
19 {
24 int ID { get; }
25
33 bool ProcessMessage(Message message);
34
39 string ToString();
40 }
41
42
43
44 //========================================================================
45 //========================================================================
46 //========================================================================
47
48
49
55 public class HandlerChain
56 {
60 List<IMessageHandler> _messageHandlers = new List<IMessageHandler>();
61
66 object _messageHandlersLock = new object();
67
68
74 public void SendMessage(Message message)
75 {
76 // We make a copy of the handlers so our processing of handlers
77 // is not impacted by updates to the master handler list.
78 IMessageHandler[] copyof_MessageHandlers;
80 {
81 copyof_MessageHandlers = new IMessageHandler[_messageHandlers.Count];
82 _messageHandlers.CopyTo(copyof_MessageHandlers);
83 }
84
85 foreach (IMessageHandler window in copyof_MessageHandlers)
86 {
87 if (window.ProcessMessage(message))
88 {
89 break;
90 }
91 }
92 }
93
94
103 public void AddHandler(IMessageHandler window)
104 {
106 {
107 // Add the handler if not already in the list.
108 if (_messageHandlers.FindIndex(w => w.ID == window.ID) == -1)
109 {
110 _messageHandlers.Add(window);
111 }
112 }
113 }
114
115
124 public void RemoveHandler(IMessageHandler window)
125 {
127 {
128 int foundIndex = _messageHandlers.FindIndex(w => w.ID == window.ID);
129 if (foundIndex != -1)
130 {
131 _messageHandlers.RemoveAt(foundIndex);
132 }
133 }
134 }
135
136
143 public override string ToString()
144 {
145 StringBuilder output = new StringBuilder();
146
147 IMessageHandler[] copyof_MessageHandlers;
149 {
150 copyof_MessageHandlers = new IMessageHandler[_messageHandlers.Count];
151 _messageHandlers.CopyTo(copyof_MessageHandlers);
152 }
153
154 foreach (IMessageHandler window in copyof_MessageHandlers)
155 {
156 output.AppendFormat(" {0}{1}", window.ToString(), Environment.NewLine);
157 }
158 return output.ToString();
159 }
160 }
161}
Represents a list of handlers that all implement the IMessageHandler interface. This list can be dyna...
void AddHandler(IMessageHandler window)
Add an instance of the IMessageHandler interface to end of the list of handlers, protected by a multi...
void RemoveHandler(IMessageHandler window)
Remove an instance of the IMessageHandler interface from the list, protected by a multi-threading loc...
List< IMessageHandler > _messageHandlers
The list of message handlers.
override string ToString()
Convert this HandlerChain to a string, protected by a multi-threading lock.
object _messageHandlersLock
Object used to lock access to the message handlers list for multi-threaded support.
void SendMessage(Message message)
Send a message to each of the handlers in the list, protected by a multi-threading lock.
Represents a handler in a chain of handlers. All objects that participate in the HandlerChain class m...
string ToString()
Convert the handler to a string.
bool ProcessMessage(Message message)
Called with a message on each window.
int ID
ID of the window. This is used to uniquely identify a window in the collection.
The namespace containing all Design Pattern Examples implemented in C#.
Represents a message sent to the windows. A message contains a type and a position.