Factory pattern in Java is heavily used everywhere including JDK, open
source library and other frameworks.
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.
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.
Some more advantages of factory method design pattern is:
1. Static factory method used in factory design pattern enforces use of Interface than implementation which itself a good practice. for example:
Map synchronizedMap = Collections.synchronizedMap(new HashMap());
2. Since static factory method have return type as Interface, it allows you to replace implementation with better performance version in newer release.
3. Another advantage of static factory method pattern is that they can cache frequently used object and eliminate duplicate object creation. Boolean.valueOf() method is good example which caches true and false boolean value.
4. Factory method pattern is also recommended by Joshua Bloch in Effective Java.
5 Factory method pattern offers alternative way of creating object.
6. Factory pattern can also be used to hide information related to creation of object.
That’s all on Factory design patten in Java for now. This is one of the most used patterns in Java library and different Java frameworks. Summary is try to use Factory pattern whenever you see an opportunity to encapsulate object creation code and see chance of creating different object in near future.
Sample:
interface Currency {
String getSymbol();
}
// Concrete Rupee Class code
class Rupee implements Currency {
@Override
public String getSymbol() {
return "Rs";
}
}
// Concrete SGD class Code
class SGDDollar implements Currency {
@Override
public String getSymbol() {
return "SGD";
}
}
// Concrete US Dollar code
class USDollar implements Currency {
@Override
public String getSymbol() {
return "USD";
}
}
Read more: http://javarevisited.blogspot.com/2011/12/factory-design-pattern-java-example.html#ixzz1qhbGgBze
// Factroy Class code
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");
}
}
// Factory client code
public class Factory {
public static void main(String args[]) {
String country = args[0];
Currency rupee = CurrencyFactory.createCurrency(country);
System.out.println(rupee.getSymbol());
}
}