-
Notifications
You must be signed in to change notification settings - Fork 11
Factory Pattern
All we are well know the concept of Object oriented programming language. Lets talk about Factory Design pattern.
What is the most usual method of creating an instance of a class in Object Oriented programming language? Most people will answer this question: “using new keyword“. Well, it is considered old fashioned now. Lets see how?
If object creation code is spread in whole application, and if you need to change the process of object creation then you need to go in each and every place to make necessary changes. So by this Article and sample we can understand how Factory design pattern helps in that case.
"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 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."
Factory design pattern in Java one of the core design pattern which is used heavily not only in JDK but also in various Open Source framework such as Spring, Struts and Apache along with decorator design pattern in Java.
Steps:
- Create an interface Currency.java.
interface Currency {
String getSymbol();
}
- Create concrete classes implementing the same interface.
- Rupee.java, SGDollar.java, USDollar.java
class Rupee implements Currency {
@Override
public String getSymbol() {
return "Rs";
}
}
class SGDollar implements Currency {
@Override
public String getSymbol() {
return "SGD";
}
}
class USDollar implements Currency {
@Override
public String getSymbol() {
return "USD";
}
}
- Create CurrencyFactory.java
class CurrencyFactory {
public static Currency createCurrency (String country) {
if (country. equalsIgnoreCase ("India")){
return new Rupee();
}else if(country. equalsIgnoreCase ("Singapore")){
return new SGDDollar();
}else if(country. equalsIgnoreCase ("US")){
return new USDollar();
}
throw new IllegalArgumentException("No such currency");
}
}
- Create Client as factory.java.
public class Factory {
public static void main(String args[]) {
Currency currency = CurrencyFactory.createCurrency("India");
System.out.println(currency.getSymbol());
}
}
- Factory pattern provides approach to code for interface rather than implementation.
- Factory pattern removes the instantiation of actual implementation classes from client code, making it more robust, less coupled and easy to extend.
- Factory pattern provides abstraction between implementation and client classes through inheritance.
- The creation of an object precludes its reuse without significant duplication of code.
- The lifetime management of the generated objects must be centralized to ensure a consistent behavior within the application.
- 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.
- java.util.Calendar, ResourceBundle and NumberFormat getInstance() methods uses Factory pattern.
- valueOf() method in wrapper classes like Boolean, Integer etc.