Saturday, March 7, 2015

Design Patterns - An eye for similarities - Part 4

Factory Pattern - Lets keep churning

Of all the patterns, I think the factory pattern is one of the patterns that is most used. Nearly everyone must have gone by a usage of this with or without realizing it.

In a real world scenario, a factory can produce a number of Products. Each of this product will have their own way of making it, but they all fall under the general idea of the factory. So, whenever the user wants some product, the factory manager just makes that product and passes it to the user.

The internal implementation is kept hidden. So the user does not have to know how the way in which the instance is given, whether a pool is kept or it is created as and when a request comes and so on and so forth. The only thing that the user knows is that they will get a fresh instance every time they ask for it.

The diagram below explains this process.


ShapeFactory sf = new ShapeFactory();
Shape circle = shapeFactory.getShape("circle");

So, the users essentially do not need to know what is happening inside the factory as long as they get the shape that they asks for.

Part 2 [ Strategy and Observer Pattern ]
Part 3 [ Decorator Pattern ]

No comments :