Wednesday 7 August 2013

Creational Design Patterns -> Factory Pattern

Motivation:
The Factory Design Pattern is probably the most used design pattern in modern programming languages like Java and C#. It comes in different variants and implementations. If you are searching for it, most likely, you'll find references about the GoF patterns: Factory Method and Abstract Factory.

In this article we'll describe a flavor of factory pattern commonly used nowdays. You can also check the original Factory Method pattern which is very similar.


Intent:
•    creates objects without exposing the instantiation logic to the client.
•    refers to the newly created object through a common interface

The factory method pattern is an object-oriented creational design pattern to implement the concept of factories and deals with the problem of creating objects (products) without specifying the exact classof object that will be created. The essence of this pattern is to "Define an interface for creating an object, but let the classes that implements the interface decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses."[1]
Factory design pattern is used to create objects or Class in Java and it provides loose coupling and high cohesion. Factory pattern encapsulate object creation logic which makes it easy to change it later when you change how object gets created or you can even introduce new object with just change in one class. In GOF pattern list Factory pattern is listed as Creation design pattern. Factory should be an interface and clients first either creates factory or get factory which later used to create objects.



The implementation is really simple

•    The client needs a product, but instead of creating it directly using the new operator, it asks the factory object for a new product, providing the information about the type of object it needs.
•    The factory instantiates a new concrete product and then returns to the client the newly created product(casted to abstract product class).
•    The client uses the products as abstract products without being aware about their concrete implementation.



The factory pattern can be used when:

•    The creation of an object precludes its reuse without significant duplication of code.
•    The creation of an object requires access to information or resources that should not be contained within the composing class.
•    The lifetime management of the generated objects must be centralized to ensure a consistent behavior within the application.
Factory methods are common in toolkits and frameworks, where library code needs to create objects of types that may be subclassed by applications using the framework.

Encapsulation
Factory methods encapsulate the creation of objects. This can be useful, if the creation process is very complex; for example, if it depends on settings in configuration files or on user input.

Consider as an example a program that reads image files. The program supports different image formats, represented by a reader class for each format.
Each time the program reads an image, it needs to create a reader of the appropriate type based on some information in the file. This logic can be encapsulated in a factory method. This approach has also been referred to as the Simple Factory


When to use Factory design pattern in Java

•    Static Factory methods are common in frameworks where library code needs to create objects of types which may be sub classed by applications using the framework.       
•    Some or all concrete products can be created in multiple ways, or we want to leave open the option that in the future there may be new ways to create the concrete product.
•    Factory method is used when Products don't need to know how they are created.
•    We  can use factory pattern where we have to create an object of any one of sub-classes depending on the data provided

Advantage of Factory method Pattern in Java:
 
Factory pattern in Java is heavily used everywhere including JDK, open source library and other frameworks.In following are main advantages of using Factory pattern in Java:

1) Factory method design pattern decouples the calling class from the target class, which result in less coupled and highly cohesive code?
E.g.: JDBC is a good example for this pattern; application code doesn't need to know what database it will be used with, so it doesn't know what database-specific driver classes it should use. Instead, it uses factory methods to get Connections, Statements, and other objects to work with. Which gives you flexibility to change your back-end database without changing your DAO layer in case you are using ANSI SQL features and not coded on DBMS specific feature?

2) Factory pattern in Java enables the subclasses to provide extended version of an object, because creating an object inside factory is more flexible than creating an object directly in the client. Since client is working on interface level any time you can enhance the implementation and return from Factory.

3) Another benefit of using Factory design pattern in Java is that it encourages consistency in Code since every time object is created using Factory rather than using different constructor at different client side.

4) Code written using Factory design pattern in Java is also easy to debug and troubleshoot because you have a centralized method for object creation and every client is getting object from same place.

Sources: Wikipedia, OODesign, TutorialsPoint, Javarevisited

No comments:

Post a Comment