Posts with the tag 'interface'


observer design pattern in coldfusion

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.

Download Source Code

2 comments April 14th, 2008


 Subscribe in a RSS reader


CourseLookup.us - Providing course and conference information to enhance your knowledge base.

Tags

Calendar

February 2012
M T W T F S S
« Dec    
 12345
6789101112
13141516171819
20212223242526
272829  

Posts by Month

Posts by Category