I have been using factory pattern since a year now. Sometime I only feel that the real advantage would just be clean code. Let me explain,
interface A {
public void test();
}
class B implements A {
public void test() {
}
}
class C implements A {
public void test() {
}
}
class Factory {
public static A getObject(String name){
if(name.equalsIgnoreCase("B")){
return new B();
}else if(name.equalsIgnoreCase("C")){
return new C();
}
return new B();
}
}
public class Test {
public static void main(String[] args) {
A a = Factory.getObject(args[0]);
// if i dint use factory pattern
A nofactory=null;
if(args[0].equalsIgnoreCase("B")){
nofactory= new B();
}else if(args[0].equalsIgnoreCase("C")){
nofactory= new C();
}
}
}
From above code I feel factory pattern just beautifies the code, please let me know if my understanding is wrong.