There are tons of tutorials online explain the simple factory. Few of them explain the question: “What if I don’t want to use simple factory design pattern”.
I’ll tell you what if you don’t use simple factory design pattern. Let’s say I’m rich and I own an auto warehouse where I have my BMW, Audi, Benz and so on.
When I want to drive the BMW, I can choose from engines: V4, V6 and V8. I can also choose ties, 18”, 19” and 20”. And so on. Almost every time I want to drive, I can customize my car. Let’s say, in this case, I only care about engines. Usually, I have to specify engines like: “BMWV4-v2.0-1058″, which is a V4 engine, 2.0L and identification number 1058. And I have a class Car, BMW, Audi and Benz are extends the Car. Here are my cars:
//Define the parent class. When we Init a Car, we need to specify the engine.
public class Car {
protected String engine = null;
public Car(){
}
public Car(String engine){
this.engine = engine;
}
public void drive() {
System.out.print("Driving Car with engine: "+this.engine);
}
}
//Those are the implementation of cars
class BMW extends Car{
public BMW(String engine){
super.engine = engine;
}
@Override
public void drive() {
System.out.println("Driving BWM with engine: "+super.engine);
}
}
class Audi extends Car{
public Audi(String engine){
super.engine = engine;
}
@Override
public void drive() {
System.out.println("Driving Audi with engine: "+super.engine);
}
}
class Benz extends Car{
public Benz(String engine){
super.engine = engine;
}
@Override
public void drive() {
System.out.println("Driving Benz with engine: "+super.engine);
}
}
As we can see, the constructor of a car will take the engine type. And now, I want to Drive:
//This is me, I'm rich and I want to drive my cars.
public class RichMan {
public static void main(String[] args){
//Monday, drive my BMW. Oh Yeah~
Car MondayCar = new BMW("BMWV4-v2.0-1058");
MondayCar.drive();
//Tuseday, drive the Benz
Car TuesdayCar = new Benz("BenzV6-v2.5-4315");
TuesdayCar.drive();
//Wed, drive the Audi, Let's roll
Car WedCar = new Audi("AudiV8-v3.0-3513-supercharged");
WedCar.drive();
}
}
Every thing looks good. But the problem is: I have to remember my engines’ name. Sometimes, its not easy to remember them. Image that I’m super rich and have tons of cars.
This is exactly the problem if we don't use factory pattern: We need to know every car. We must konw everything to create a car.
To work this out, I decide to hire someone working at my car warehouse. And the whole car warehouse is a Factory now. The Factory knows exactly what are the engines. And everyday, The factory will get the car I want ready.
public class MyCarFactory {
private Car myCar;
//Now, My factory will remember all the engine type
public MyCarFactory(String car){
if(car.equals("BMW")){
this.setMyCar(new BMW("BMWV4-v2.0-1058"));
}
else if(car.endsWith("Benz")){
this.setMyCar(new Benz("BenzV6-v2.5-4315"));
}
else{
this.setMyCar(new Audi("AudiV8-v3.0-3513-supercharged"));
}
/*
* Here, in the future, if I have more cars, I can add them here,
* If new upgrade of engine, I can change the engine here.
* I can even hire someone to work in this factory and do all the things for me.
* At any memont, as a rich man, I only drive, no need to know details.
*/
}
public Car getMyCar() {
return myCar;
}
public void setMyCar(Car myCar) {
this.myCar = myCar;
}
}
Now, for me, as a rich man, I only need to do:
//This is me, I'm rich and I want to drive my cars.
public class RichMan {
public static void main(String[] args){
//Monday, drive my BMW. Oh Yeah~
Car MondayCar = new BMW("BMWV4-v2.0-1058");
MondayCar.drive();
//Tuseday, drive the Benz
Car TuesdayCar = new Benz("BenzV6-v2.5-4315");
TuesdayCar.drive();
//Wed, drive the Audi, Let's roll
Car WedCar = new Audi("AudiV8-v3.0-3513-supercharged");
WedCar.drive();
//Thursday, I don't need to remember any of my car's engine type.
Car ThursdayCar = new MyCarFactory("Audi").getMyCar();
ThursdayCar.drive();
}
}
That’s it. This is the idea. I’m god damn rich and I don’t want to remember all the details of getting my fancy car ready to drive. My factory will take care of it and I only need to drive. Cheers~ - See more at my blog: Why we use simple factory design pattern? Reasons and examples