Monday, January 5, 2009

Design Patterns -- Coldfusion

Sometime back while working on a project we came up with a system for reporting, at that time I got to learn and use some of the design patterns. I am describing them below

1. Facade(Structural Pattern) -- While creating a system, we come across an issue that the subsytem grows and it becomes complex with large number of smaller objects. As a solution of this, what needs to be done is, Introduce a single component that provides a simple API to the set of components within the subsystems. Applying this pattern we actually reduce coupling of the subsystems and the subsytem are managed as different pieces by their own components.

2. Front Controller(J2EE Presentation Tier) -- The second problem that we came across was to have every request follow the same logic(security, layout etc), well the solution is simple, just have all the request go through a common single file which decides how to process the request. So now we have centralized controller and this moves the logic from individual pages to the controller.

3. Data Access Object (J2EE Integration Tier) -- This helps you separate your data access from any business Logic. Basically DAO will have 4 methods a) Inserting Data b) updating data c) deleting data d) selecting data. Now you have to make minimal changes if you database changes from MySQL to Oracle or XML or anything else.

4. Factory (Creational Pattern) -- Just like factories in real life which creates things, the factory pattern job is to create other objects. So you can say it is an INTERFACE for creating an object. Then after creating an object, have the subclasses decide which class to call.

5. Dependency Injection -- In conventional method, if an object needs to gain access of a particular service, then either it holds a reference to the location of the service or has access to some service locator. In contrast, in DI, when an object is created a reference to an implementation of that type of service will automatically be injected. Why DI? - because it allows you to create alternative implementation of a given service type.

Then we also used security in the system by having all the files beginning with _(underscore) and adding small piece of code in Application.cfc file which did not allow to access files that begin with underscore. One of the other things used in the project was ERROR COLLECTION classes and a page which showed the errors.

References for above article a) b) c)

No comments: