Design Pattern Examples
Overview of object-oriented design patterns
handlerchain_class.py
Go to the documentation of this file.
8
9from abc import ABC, abstractmethod
10from threading import Lock
11from io import StringIO
12
13from .handlerchain_message_class import Message
14
15
17class IMessageHandler(ABC):
18
19
21 @property
22 @abstractmethod
23 def ID(self) -> int:
24 pass
25
26
34 @abstractmethod
35 def ProcessMessage(self, message : Message) -> bool:
36 pass
37
38
42 @abstractmethod
43 def ToString(self) -> str:
44 pass
45
46
47#========================================================================
48#========================================================================
49#========================================================================
50
51
52
55class HandlerChain:
56
57
58 def __init__(self) -> None:
59 self._messageHandlers = [] # type: list[IMessageHandler]
61
62
67
68
69
74 def SendMessage(self, message : Message):
75 # We make a copy of the handlers so our processing of handlers
76 # is not impacted by updates to the master handler list.
77 copyof_MessageHandlers = None
78 with self._messageHandlersLock:
79 copyof_MessageHandlers = self._messageHandlers.copy()
80
81 for window in copyof_MessageHandlers:
82 if window.ProcessMessage(message):
83 break
84
85
86
94 def AddHandler(self, window : IMessageHandler) -> None:
95 with self._messageHandlersLock:
96 existsInList = False
97 for handler in self._messageHandlers:
98 if window.ID == handler.ID:
99 existsInList = True
100 break
101 if not existsInList:
102 self._messageHandlers.append(window)
103
104
105
113 def RemoveHandler(self, window : IMessageHandler) -> None:
114 with self._messageHandlersLock:
115 foundWindow = None
116 for handler in self._messageHandlers:
117 if window.ID == handler.ID:
118 foundWindow = window
119 break
120 if foundWindow:
121 self._messageHandlers.remove(foundWindow)
122
123
124
130 def ToString(self) -> str:
131 output = StringIO()
132
133 copyof_MessageHandlers = None
134 with self._messageHandlersLock:
135 copyof_MessageHandlers = self._messageHandlers.copy()
136
137 for window in copyof_MessageHandlers:
138 output.write(" {0}\n".format(window.ToString()))
139 return output.getvalue()
str ToString(self)
Convert this HandlerChain to a string, protected by a multi-threading lock.
_messageHandlersLock
Lock object used to lock access to the message handlers list for multi-threaded support.
def SendMessage(self, Message message)
Send a message to each of the handlers in the list, protected by a multi-threading lock.
None AddHandler(self, IMessageHandler window)
Add an instance of the IMessageHandler interface to end of the list of handlers, protected by a multi...
None RemoveHandler(self, IMessageHandler window)
Remove an instance of the IMessageHandler interface from the list, protected by a multi-threading loc...
bool ProcessMessage(self, Message message)
Called with a message on each window.
int ID(self)
Property getter for the ID of the window: value = o.ID.