I am learnig the simple factory pattern, and I would like to know if all the methods in my factory are valid for such a pattern:
public class Bmw implements Car {
private String color;
private boolean hasXDrive;
public Bmw() {
}
public Bmw(String color) {
this.color = color;
}
public Bmw(String color, boolean hasXDrive) {
this.color = color;
this.hasXDrive = hasXDrive;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isHasXDrive() {
return hasXDrive;
}
public void setHasXDrive(boolean hasXDrive) {
this.hasXDrive = hasXDrive;
}
}
public class Audi implements Car {
private String color;
private int turnAssistLevel;
public Audi() {
}
public Audi(String color) {
this.color = color;
}
public Audi(String color, int turnAssistLevel) {
this.color = color;
this.turnAssistLevel = turnAssistLevel;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getTurnAssistLevel() {
return turnAssistLevel;
}
public void setTurnAssistLevel(int turnAssistLevel) {
this.turnAssistLevel = turnAssistLevel;
}
}
public class SimpleCarFactory {
// 1. make empty cars
public Car makeCar(CarType carType) {
switch (carType) {
case AUDI:
return new Audi();
case BMW:
return new Bmw();
default:
throw new RuntimeException("No such car type!");
}
}
// 2. make cars with colors
public Car makeCarWithColor(CarType carType, String color) {
switch (carType) {
case AUDI:
return new Audi(color);
case BMW:
return new Bmw(color);
default:
throw new RuntimeException("No such car type!");
}
}
// 3. BMW has an option that differentiate it from any other car. We cannot use a general car factory anymore
public Car makeBmw(String color, boolean hasXDrive) {
return new Bmw(color, hasXDrive);
}
// 4. Audi has a turnAssistLevel option
public Car makeAudi(String color, int turnAssistLevel) {
return new Audi(color, turnAssistLevel);
}
// 5. The same as #1, only it is static now make empty cars
public static Car staticMakeCar(CarType carType) {
switch (carType) {
case AUDI:
return new Audi();
case BMW:
return new Bmw();
default:
throw new RuntimeException("No such car type!");
}
}
}
I've added in code comments the variants of the methods. I'm asking these question because normally, you create a subclass based on some discriminator (CarType). But you can also have constructor parameters.
Also, I'm not sure what to do when your related objects have different constructors.
Please tell me which of the methods from SimpleCarFactory are qualified to follow the simple factory pattern?
Kind regards,
It all depends on what is your factory is going to be used for.
1) If you are going to pass this factory to some generic instantiator then your factory must implement some interface common to all factories used by that instantiator. This interface is likely to have single method with generic options:
public interface CarFactory {
Car makeCar(CarOptions options);
}
2) If you are going to invoke your factory "manually" from different parts of your code, then I would say your approach is right. makeAudi("red", true)
looks much more readable than makeCar(new CarOptions(CarType.AUDI, "red", ...))
.