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.
Example: Let’s suppose an application asks for entering the name and sex of a person. If the sex is Male (M), it displays welcome message saying Hello Mr.
The skeleton of the code can be given here.
public class Person { | ||
| // name string public String name; // gender : M or F private String gender; public String getName() { | |
}// End of class |
This is a simple class Person having methods for name and gender. Now, we will have two sub-classes, Male and Female which will print the welcome message on the screen.
public class Male extends Person { | ||
| public Male(String fullName) { System.out.println("Hello Mr. "+fullName); } | |
}// End of class |
Also, the class Female
public class Female extends Person { | ||
| public Female(String fullNname) { System.out.println("Hello Ms. "+fullNname); } | |
}// End of class |
Now, we have to create a client, or a SalutationFactory which will return the welcome message depending on the data provided.
public class SalutationFactory { | ||
| public static void main(String args[]) { SalutationFactory factory = new SalutationFactory(); factory.getPerson(args[0], args[1]); } public Person getPerson(String name, String gender) { | |
}// End of class |
This class accepts two arguments from the system at runtime and prints the names.
Running the program:
After compiling and running the code on my computer with the arguments Prashant and M:
java Prashant M
The result returned is: “Hello Mr. Prashant”.
When to use a Factory Pattern?The Factory patterns can be used in following cases:
1. When a class does not know which class of objects it must create.
2. A class specifies its sub-classes to specify which objects to create.
3. In programmer’s language (very raw form), you can use factory pattern where you have to create an object of any one of sub-classes depending on the data provided.
No comments:
Post a Comment