I am trying to learn creational design patterns, and i think i understand Factory pattern now. But on moving to Abstract Factory Pattern, I couldn't find its use. I know i miss something with this, but no idea where.
In Abstract Factory Pattern we will have a an Abstract Factory, and Concrete Factories wil return the instance. Suppose we are dealing with creation of Cars. We will have an Abstract Factory like
public interface CarFactory{
public Car getCar();
}
And our concrete Factories will be something like
public class AudiFactory{
public Car getCar(){
return new Audi();
}
}
public class VolvoFactory{
public Car getCar(){
return new Volvo();
}
}
And in user class we will use it like
CarFactory factory = new AudiFactory();
Car carAudi = factory.getCar();
factory = new VolvoFactory();
Car carVolvo = factory.getCar();
I think we can build the same functionality using Factory Pattern too
public class CarFactory{
public Car getCar(String make){
if("Audi".equals(make))
return new Audi();
else if("Volvo".equals(make))
return new Volvo();
}
}
And in user class we can
CarFactory factory = new CarFactory();
Car carAudi = factory.getCar("Audi");
Car carVolvo = factory.getCar("Volvo");
If my understanding is correct(please correct me if its wrong), Why we need another design pattern for this?
Aucun commentaire:
Enregistrer un commentaire