Design Pattern Examples
Overview of object-oriented design patterns
handlerchain_exercise.py
Go to the documentation of this file.
6
7from .handlerchain_class import HandlerChain
8from .handlerchain_messagewindow_class import MessageWindow, MessageWindowFactory
9from .handlerchain_message_class import Message, MessagePosition, MessageType
10
11
20def _HandlerChain_ConstructWindowChain(handlerChain : HandlerChain) -> None:
21 # Note: This creates each window and adds the new window to the given
22 # HandlerChain object.
23 #
24 # This example doesn't care about each individual window so the
25 # return value is ignored.
26 MessageWindowFactory.CreateWindow("Window 1", 0, 0, 10, 10, handlerChain)
27 MessageWindowFactory.CreateWindow("Window 2", 20, 0, 5, 5, handlerChain)
28 MessageWindowFactory.CreateWindow("Window 3", 30, 10, 15, 15, handlerChain)
29
30
31
32
45
46# ! [Using HandlerChain in Python]
48 print()
49 print("Handler Chain Exercise")
50
51 # Construct a handler chain and populate with windows that can
52 # handle messages.
53 handlerChain = HandlerChain()
55
56 print(" Handler Chain at start:")
57 print(handlerChain.ToString())
58
59 # Now pass messages to the windows.
60
61 print(" Select Window 2")
62 handlerChain.SendMessage(Message(MessageType.ButtonDown, MessagePosition(22, 1)))
63 handlerChain.SendMessage(Message(MessageType.ButtonUp, MessagePosition(22, 1)))
64 print(" Current handler chain:")
65 print(handlerChain.ToString())
66
67 print(" Select Window 3")
68 handlerChain.SendMessage(Message(MessageType.ButtonDown, MessagePosition(35, 11)))
69 handlerChain.SendMessage(Message(MessageType.ButtonUp, MessagePosition(35, 11)))
70 print(" Current handler chain:")
71 print(handlerChain.ToString())
72
73 print(" Select Window 1")
74 handlerChain.SendMessage(Message(MessageType.ButtonDown, MessagePosition(4, 4)))
75 handlerChain.SendMessage(Message(MessageType.ButtonUp, MessagePosition(4, 4)))
76 print(" Current handler chain:")
77 print(handlerChain.ToString())
78
79 print(" Close Window 2")
80 handlerChain.SendMessage(Message(MessageType.ButtonDown, MessagePosition(24, 0)))
81 handlerChain.SendMessage(Message(MessageType.ButtonUp, MessagePosition(24, 0)))
82 print(" Current handler chain:")
83 print(handlerChain.ToString())
84
85 print(" Done.")
86# ! [Using HandlerChain in Python]
Represents a list of handlers that all implement the IMessageHandler interface.
Position of the message in global coordinates (same scope of coordinates as windows).
None _HandlerChain_ConstructWindowChain(HandlerChain handlerChain)
Helper method to construct a list of windows.
def HandlerChain_Exercise()
Example of using the HandlerChain Pattern or Chain of Responsibility Pattern.