Sunday, February 1, 2015

what is factory method in java

Definition

Provides an abstraction or an interface and lets subclass or implementing classes decide which class or method should be instantiated or called, based on the conditions or parameters given.

Where to use & benefits

Connect parallel class hierarchies.
A class wants its subclasses to specify the object.
A class cannot anticipate its subclasses, which must be created.
A family of objects needs to be separated by using shared interface.
The code needs to deal with interface, not implemented classes.
Hide concrete classes from the client.
Factory methods can be parameterized.
The returned object may be either abstract or concrete object.
Providing hooks for subclasses is more flexible than creating objects directly.
Follow naming conventions to help other developers to recognize the code structure.
Related patterns include
Abstract Factory , which is a layer higher than a factory method.
Template method, which defines a skeleton of an algorithm to defer some steps to subclasses or avoid subclasses
Prototype, which creates a new object by copying an instance, so it reduces subclasses.
Singleton, which makes a returned factory method unique.

0 comments:

Post a Comment

 
© 2014 technical online tips