Thursday, February 16, 2012

Creational Patterns - 2.Abstract Factory

Abstract Factory will be the right choice, when you are dealing with different types of objects sharing some common features. We can, abstract those features and define seperate factory implementations for creating the objects.

In otherwords, this is a factory of factories.

Example Use Case:-

This will be an extension to the factory method use case.
Now, the ABC Company needs some variety of cars. They are in need of Sedan Cars and HatchBack Cars. Similarly, PQR and XYZ companies are also demanding the same.

How can we handle this ?

CarFactory factory=new ABCCarFactory();
factory.createCar();


This block of code would be fine for a normal requirement as discussed in factory method
use case. For this new requirement we need some thing like,

CarFactory factory=new ABCCarFactory();
factory.createSedanCar();
factory.createHatchBackCar();


In future, ABC company may ask for SUV cars. so, we need to think for a seam less modification to the design.
Now,we categorize the cars based on their types(Sedan,Hatchback,SUV,etc).Let the companies(ABC,PQR,etc) write their own implementation of the types.

The design looks as specified below,


We have seperate CarFactories for every company. ABCCarFactory deals with the sedan and hatchback cars of ABC Company only.

This will enable us to add new Factories for new companies without any modification to the existing behavior. Similarly, new vehicle types like SUV can also be introduced without much
effort.

Wednesday, February 15, 2012

Creational Patterns - 1.Factory Method

This is the simplest of all the GoF(Gang Of Four) Design patterns. It deals with the task of creating an object for a class. Instead of creating objects in our business classes, we are delegating the task to a Factory class. This will help us to maintain and manage the objects without affecting the business flow.

Example Use Case:-

Consider, we are in a process of creating a Car for ABC Company as mentioned below,
Car car=new Car();
car.createCar();

In future, companies like PQR,XYZ,etc may ask us to create cars for them.
So, we need to maintain the create() for each and every company in our Car class itself.

Car car=new Car();
car.createABCCar();
car.createPQRCar();
car.createXYZCar();


The above approach would create a tight dependency over the Car creation process.
We can solve this problem and create a loose dependency with the following design:-




When we create the cars based on the above design, it would be easier to accommodate any number of new companies for creating their cars. For providing cars to PQR, we need to create a PQRCar Class by inheriting Car and an implementation to the Factory for creating the PQRCar.
The business logic for creating a car would be something similar to,

CarFactory factory=new ABCCarFactory();
CarFactory factory=new PQRCarFactory();

Car car=factory.createCar();


Thus, the car creation logic will be handled by the CarFactory based on the instantiation
of the factory.

Design Patterns Simplified

This will be the first in the series of posts to explain Core Java Design Patterns with simple examples.
This series has been organized in three parts,
  1. Creational Patterns
  2. StructuralPatterns and
  3. Behavioral Patterns
Every Pattern will have an example Use Case and a design to drive it.