observer design pattern in coldfusion
April 14th, 2008
The observer design pattern is used in application development to observe the state of an object. The best way to think about it is the publish / subscribe model. In this pattern you have one object that is being observed (the subject), and a group of objects watching the subject called observers or listeners. This pattern is an excellent example of loose coupling, because our classes can interact with very little knowledge of each other.
(read about the pattern @ http://en.wikipedia.org/wiki/Observer_pattern )
This post will demonstrate the usage of this pattern in ColdFusion
In the example I am creating a user list and the list if being watched by a logging observers that prints the message out to the browser anytime the new user gets added
IObservable.cfc
The Object that will provide notification to registered observers/listeners needs to implement this interface, in our case it will be UserList.cfc
[The requested file http://www.cfdesignpatterns.com/wp-content/uploads/2008/04/IObservable.cfc could not be found]
IObserver.cfc
The Object that will recieve notifications has to implement this interface, in our case it will be UserListLogger.cfc
[The requested file http://www.cfdesignpatterns.com/wp-content/uploads/2008/04/IObserver.cfc could not be found]
UserList.cfc
This Class implements the IObservable interface
[The requested file http://www.cfdesignpatterns.com/wp-content/uploads/2008/04/UserList.cfc could not be found]
UserListLogger.cfc
This Class implements the IObserver interface and at present only displays the information on browser
[The requested file http://www.cfdesignpatterns.com/wp-content/uploads/2008/04/UserListLogger.cfc could not be found]
test.cfm
The code here creates a UserList and registers the UserListLogger with it. The remaining part of the code adds user to the list and as soon the user is add, the UserListLogger gets notification of change.
[The requested file http://www.cfdesignpatterns.com/wp-content/uploads/2008/04/test.cfm could not be found]
The output of test.cfm will look like this
Arjun added to the user list
Vivaan added to the user list
Something worth watching is how the loose coupling of components, the UserList does not know what UserListLogger is doing or going to do with the notification. In fact there can be multiple listeners attached with UserList e.g. listeners to perform data base insert, listeners for email notification etc. The UserList does not care about how many or what type of listeners is attached to it, instead it just focuses on its task of notifying to the registered listeners about the change.
As you can see by following this pattern we can create ColdFusion applications that can adapt to changes and is easily maintainable.
Entry Filed under: Behavioral patterns, code snippet
3 Comments Add your own
1. Bruce | April 15th, 2008 at 1:03 am
very easy to follow explanation and example of this pattern - thank you for posting it
2. Kevin Parker | October 15th, 2009 at 3:34 am
Thank you for this post and code: It really helped me understand how to implement the Observer pattern in ColdFusion.
As I played with the example, I noticed that the way you register the listener in your .CFM page doesn’t allow me to “unregister” it using that method in the “observable” object. But if I instead create the object separately, then refer to it’s variable name in the “registerObserver” method, I could then unregister and reregister it with no problems:
This would output:
Larry added to the user list
Moe added to the user list
Curly added to the user list
Thanks again for making this make sense to me!
3. Adam Cameron | September 27th, 2012 at 9:04 pm
Hey, it’s a bit of an old post, but I’ve been thinking about the observer pattern for ColdFusion over the last couple of days for something I’m working on, and of all the stuff I’ve read, this is the most well-thought-out I’ve encountered.
I was expecting to see this sort of thing in the current popular frameworks, but none of them cover this sort of thing, or if they have an approximation, it’s implemented rather strangely, or very how-the-framework-needs-to-work-centric (if that makes sense).
This is implementation agnostic, and elegant in its simplicity.
Nice work.
–
Adam
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed