28 January 2009

What is Creational Pattern?

As name suggests, the creational patterns are used in object creation. Any object oriented programming language will give you an infrastructure to create objects and perform whole lot of operations. One of the important and primary operations in any application is creating object. For example, in Java, you can create objects of any concrete types using "new" operator. However, the Java compiler or JVM do not know, are you creating objects in right way.  The way we choose to create objects decides many aspects such as reusability, maintainability and flexibility. Though we create objects in an application, we will creating objects for so many reasons. For instance, in the case of Singleton it enforces creating only one object for a class (type). As the purpose and scope of the objects vary, the way in which they are created should also vary to reap full benefits of Object Oriented Programming. There cannot be single solution for whole a lot of problems.

Let us consider a real world example. Assume that a car manufacture is planning to manufacture new brand (assume Swift by Maruti). While designing the car, the designers will focus on individual aspects of the car. There will also be a mechcanism to customize any model or to add features to the car. The extra fittings or specific model (Vxi, Zxi) will have its own assembling mechanisms. The dealers or the customers do not need to worry about how the car is made. They just give which model they want and the car is delivered to them. Internally, Maruti will have its own processes and standards to manufacture car hiding all the manufacturing details. It may choose to manufacture the parts or procure it from a different vendor. It is all internal Maruthi and the user does not need to know too much of details. It is all about exposing needed level of abstraction to the clients - not more and not less.

Also, as a developer, it does not make sense to fill your code with lot of "new" operator. By using "new" operator in lot of places makes the code to break maintainability and flexibility. When few more classes are added to the package or namespace, the developer will face a nightmare maintaining the code and quality of the code is also degrades.

So, the creational patterns helps to create objects in a better way rather than using "new" operator. When you use creational patterns (or any design pattern), you will be programming to an interface and not to the implementation. When you program to interface, your code becomes flexible and maintainable. In the next series of posts, we will be discussing creational patterns one-by-one.

No comments: