1 """
2 PureMVC Python Port by Toby de Havilland <toby.de.havilland@puremvc.org>
3 PureMVC - Copyright(c) 2006-08 Futurescale, Inc., Some rights reserved.
4 Your reuse is governed by the Creative Commons Attribution 3.0 License
5 """
6
7 import puremvc.interfaces
8 import puremvc.patterns.facade
9
10 -class Observer(object, puremvc.interfaces.IObserver):
11 """
12 A base C{IObserver} implementation.
13
14 An C{Observer} is an object that encapsulates information
15 about an interested object with a method that should
16 be called when a particular C{INotification} is broadcast.
17
18 In PureMVC, the C{Observer} class assumes these responsibilities:
19
20 Encapsulate the notification (callback) method of the interested object.
21
22 Encapsulate the notification context (this) of the interested object.
23
24 Provide methods for setting the notification method and context.
25
26 Provide a method for notifying the interested object.
27
28 @see: L{View<org.puremvc.as3.core.view.View>}
29 @see: L{Notification<org.puremvc.as3.patterns.observer.Notification>}
30 """
31 notify = None
32 context = None
33
34 - def __init__(self, notifyMethod, notifyContext):
35 """
36 Constructor.
37
38 The notification method on the interested object should take
39 one parameter of type C{INotification}
40
41 @param notifyMethod: the notification method of the interested object
42 @param notifyContext: the notification context of the interested object
43 """
44 self.setNotifyMethod(notifyMethod)
45 self.setNotifyContext(notifyContext)
46
48 """
49 Set the notification method.
50
51 The notification method should take one parameter of type C{INotification}.
52
53 @param notifyMethod: the notification (callback) method of the interested object.
54 """
55 self.notify = notifyMethod
56
57 - def setNotifyContext(self, notifyContext):
58 """
59 Set the notification context.
60
61 @param notifyContext: the notification context (this) of the interested object.
62 """
63 self.context = notifyContext
64
66 """
67 Get the notification method.
68
69 @return: the notification (callback) method of the interested object.
70 """
71 return self.notify
72
74 """
75 Get the notification context.
76
77 @return: the notification context (C{this}) of the interested object.
78 """
79 return self.context
80
82 """
83 Notify the interested object.
84
85 @param notification: the C{INotification} to pass to the interested object's notification method.
86 """
87 self.getNotifyMethod()(notification)
88
90 """
91 Compare an object to the notification context.
92
93 @param obj: the object to compare
94 @return: boolean indicating if the object and the notification context are the same
95 """
96 return (obj is self.context)
97
98
99 -class Notifier(object, puremvc.interfaces.INotifier):
100 """
101 A Base C{INotifier} implementation.
102
103 C{MacroCommand, Command, Mediator} and C{Proxy}
104 all have a need to send C{Notifications}.
105
106 The C{INotifier} interface provides a common method called
107 C{sendNotification} that relieves implementation code of
108 the necessity to actually construct C{Notifications}.
109
110 The C{Notifier} class, which all of the above mentioned classes
111 extend, provides an initialized reference to the C{Facade}
112 Singleton, which is required for the convienience method
113 for sending C{Notifications}, but also eases implementation as these
114 classes have frequent C{Facade} interactions and usually require
115 access to the facade anyway.
116
117 @see: L{Facade<org.puremvc.as3.patterns.facade.Facade>}
118 @see: L{Mediator<org.puremvc.as3.patterns.mediator.Mediator>}
119 @see: L{Proxy<org.puremvc.as3.patterns.proxy.Proxy>}
120 @see: L{SimpleCommand<org.puremvc.as3.patterns.command.SimpleCommand>}
121 @see: L{MacroCommand<org.puremvc.as3.patterns.command.MacroCommand>}
122 """
123
124 facade = None
125
131
133 """
134 Create and send an C{INotification}.
135
136 Keeps us from having to construct new INotification
137 instances in our implementation code.
138
139 @param notificationName: the name of the notiification to send
140 @param body: the body of the notification (optional)
141 @param type: the type of the notification (optional)
142 """
143 self.facade.sendNotification(notificationName, body, type)
144
145
146 -class Notification(object, puremvc.interfaces.INotification):
147 """
148 A base C{INotification} implementation.
149
150 PureMVC does not rely upon underlying event models such
151 as the one provided with Flash, and ActionScript 3 does
152 not have an inherent event model.</P>
153
154 The Observer Pattern as implemented within PureMVC exists
155 to support event-driven communication between the
156 application and the actors of the MVC triad.</P>
157
158 Notifications are not meant to be a replacement for Events
159 in Flex/Flash/Apollo. Generally, C{IMediator} implementors
160 place event listeners on their view components, which they
161 then handle in the usual way. This may lead to the broadcast of C{Notification}s to
162 trigger C{ICommand}s or to communicate with other C{IMediators}. C{IProxy} and C{ICommand}
163 instances communicate with each other and C{IMediator}s
164 by broadcasting C{INotification}s.</P>
165
166 A key difference between Flash C{Event}s and PureMVC
167 C{Notification}s is that C{Event}s follow the
168 'Chain of Responsibility' pattern, 'bubbling' up the display hierarchy
169 until some parent component handles the C{Event}, while
170 PureMVC C{Notification}s follow a 'Publish/Subscribe'
171 pattern. PureMVC classes need not be related to each other in a
172 parent/child relationship in order to communicate with one another
173 using C{Notification}s.
174
175 @see: L{Observer<org.puremvc.as3.patterns.observer.Observer>}
176 """
177
178 name = None
179 body = None
180 type = None
181
182 - def __init__(self, name, body=None, type=None):
183 """
184 Constructor.
185
186 @param name: name of the C{Notification} instance. (required)
187 @param body: the C{Notification} body. (optional)
188 @param type; the type of the C{Notification} (optional)
189 """
190 self.name = name
191 self.body = body
192 self.type = type
193
195 """
196 Get the name of the C{Notification} instance.
197
198 @return: the name of the C{Notification} instance.
199 """
200 return self.name
201
202 - def setBody(self, body):
203 """
204 Set the body of the C{Notification} instance.
205 """
206 self.body = body
207
209 """
210 Get the body of the C{Notification} instance.
211
212 @return: the body object.
213 """
214 return self.body
215
217 """
218 Set the type of the C{Notification} instance.
219 """
220 self.type = type;
221
223 """
224 Get the type of the C{Notification} instance.
225
226 @return: the type
227 """
228 return self.type;
229
231 """
232 Get the string representation of the C{Notification} instance.
233
234 @return: the string representation of the C{Notification} instance.
235 """
236 msg = "Notification Name: "+self.getName();
237
238 bd = "None"
239 if self.body is not None:
240 bd = str(self.body)
241
242 ty = "None"
243 if self.type is not None:
244 ty = self.type
245
246 msg += "\nBody:"+bd
247 msg += "\nType:"+ty
248 return msg;
249