Factory Method
[[Design Patterns — 7]]
- Concept of Factory Method Pattern
- Class Diagram for Data Driven Factory Method Pattern
- Code Example of Data Driven Factory Design
- Class Diagram for Config Driven Factory Method Pattern
- Code Example of Config Driven Factory Design
Concept of Factory Method Pattern:
- Clients can create object based on the event type, or based on the data that is available at the time of creation.
- Keep the client class independent of the concrete types of the object. Make the client interact with the object through an interface.
- Allow the addition to new concrete types of the object at a later time without affecting the client code.
There are several variations of the design pattern based on the data that is available or based on the configuration that is required.
Class Design Diagram for Data Driven Factory Method Pattern:
In Data Driven Factory Design the Factory takes into consideration data that is available at the time of product creation in order to determine which type of product is suitable for the purpose.
OR
The Class Diagram of Data Driven Factory Design can be described as
Get Prince Jha’s stories in your inbox
Join Medium for free to get updates from this writer.
Client: Class in this role represents the user class. It is aware of the ProductIF interface which helps it to understand how to use the product. It is aware of the FactoryIF interface which helps it to understand how to use a product factory. Client class is independent of the concrete factory types, concrete product types. It is easy to add a product/product factory without affecting the client class.
FactoryIF: Interface that represents the method that the concrete factory classes should implement. Usually declares a method which the user should be able to use. The Client class uses the interface to interact with the factory objects. The factory interface achieves independence between the concrete product factory classes and the clients.
FactoryA, FactoryB, FactoryC: They implement the FactoryIF interface. The concrete factories know about the concrete product types. These classes encapsulate the logic of under what circumstances which product type to create.
ProductIF: Interface that represents the methods which the Client class can use to interact with the concrete product types. The clients interact with the product objects using the interface. The ProductIF interface achieves independence between client and concrete product types.
ProductA, ProductB, ProductB: They implement the ProductIF interface. They encapsulate the data and logic needed to represent the product.
Code Example to Demonstrate the use of Data Driven Factory Design.
// TRY TO DISTRIBUTE CODE IN MULTIPLE FILES
enum Color {
RED, YELLOW, GREEN, BLACK, BLUE
};
enum ContentType {
TEXT, IMAGE, ANY
}
class PrintSetting {
private final ContentType contentType; // content type represents what is contained
private final int requiredSpeed; // number of characters per second to be printed
private final double maxCost;
public PrintSetting(ContentType type, int speed, double cost) {
contentType = type;
requiredSpeed = speed;
maxCost = cost;
}
// check if the available printer satisfies the required settings
public static boolean isSuitable(PrintSetting required, PrintSetting available) {
boolean contentTypeOk = (required.contentType == available.contentType ||
available.contentType == ContentType.ANY);
boolean speedOk = required.requiredSpeed <= available.requiredSpeed;
boolean costOk = required.maxCost >= available.maxCost;
return contentTypeOk && speedOk && costOk;
}
}
interface Printer {
public void print(String content);
}
interface PrinterFactory {
public Printer createPrinter(PrintSetting dataSettings);
}
class InkJetPrinter implements Printer {
public static final PrintSetting THRESHOLD = new PrintSetting(
ContentType.TEXT, // prints only text
210, // medium speed
6000.00 // maximum cost
);
private String name; // name of printer
private Color color; // color of the ink
public InkJetPrinter(String name, Color color) {
this.name = name;
this.color = color;
}
public void print(String content) {
System.out.printf("INKJET PRINTER %s WORKING", name);
System.out.println("Content Text ===> " + content);
}
}
class DeskJetPrinter implements Printer {
public static final PrintSetting THRESHOLD = new PrintSetting(
ContentType.IMAGE, // prints only image
300, // medium speed
8500.50 // maximum cost
);
private String name; // name of printer
private Color color; // color of the ink
public DeskJetPrinter(String name, Color color) {
this.name = name;
this.color = color;
}
public void print(String content) {
System.out.printf("DESKJET PRINTER %s WORKING", name);
System.out.println("Content Image ===> " + content);
}
}
class LaserPrinter implements Printer {
public static final PrintSetting THRESHOLD = new PrintSetting(
ContentType.ANY, // prints all kind of docs
600, // medium speed
12000.20 // maximum cost
);
private String name; // name of printer
private Color color; // color of the ink
public LaserPrinter(String name, Color color) {
this.name = name;
this.color = color;
}
public void print(String content) {
System.out.printf("LASER PRINTER %s WORKING", name);
System.out.println("Content Any ===> " + content);
}
}
class ConcretePrinterFactory implements PrinterFactory {
public Printer createPrinter(PrintSetting settings) {
if (PrintSetting.isSuitable(settings, LaserPrinter.THRESHOLD)) {
return new LaserPrinter("TAHER LASER", Color.BLACK);
} else if (PrintSetting.isSuitable(settings, DeskJetPrinter.THRESHOLD)) {
return new DeskJetPrinter("PRINCE DESKJET", Color.BLUE);
} else if (PrintSetting.isSuitable(settings, InkJetPrinter.THRESHOLD)) {
return new InkJetPrinter("TUSHAR INKJET", Color.BLACK);
}
else {
return null;
}
}
}
public class Client {
public static void main(String [] args) {
PrinterFactory factoryObj = new ConcretePrinterFactory();
PrintSetting requiredSetting = new PrintSetting(ContentType.ANY, 350, 18000);
Printer printer = factoryObj.createPrinter(requiredSetting);
printer.print("Wowwww!!!!!😍😍😍");
}
}Class Design Diagram for Configuration Driven Factory Method Pattern:
In some scenarios, the Factory does not take into consideration the data that is available in order to decide which product type to create. Instead the factory only limits itself to creating a specific type of product. Such Factory have now resulted in a configuration based factory design where the kind of factory chosen by the client will lead to the kind of product that client will get to work with.
The Class Diagram of Configuration Driven Factory Design can be described as
Client: Class in this role represents the user class. It is aware of the ProductIF interface which helps it to understand how to use the product. It is aware of the FactoryIF interface which helps it to understand how to use a product factory. Client class is independent of the concrete factory types, concrete product types. It is easy to add a product/product factory without affecting the client class.
FactoryIF: Interface that represents the method that the concrete factory classes should implement. Usually declares a method which the user should be able to use. The Client class uses the interface to interact with the factory objects. The factory interface achieves independence between the concrete product factory classes and the clients.
FactoryA, FactoryB, FactoryC: They implement the FactoryIF interface. The config driven design of factory is different in the role. Each concrete factory often associates itself to only one corresponding concrete product type. Adding a concrete product type may require adding a concrete factory corresponding to it.
ProductIF: Interface that represents the methods which the Client class can use to interact with the concrete product types. The clients interact with the product objects using the interface. The ProductIF interface achieves independence between client and concrete product types.
ProductA, ProductB, ProductB: They implement the product interface. They represent a concrete product. They encapsulate the data and logic needed to represent the product.
Example Code to demonstrate Config Driven Factory Design:
// TRY TO DISTRIBUTE CODE IN MULTIPLE FILES
enum Color {
RED, YELLOW, GREEN, BLACK, BLUE
};
enum BrakeIntensity {
LOW, MEDIUM, HIGH
}
interface CarFactory {
Car createCar();
}
interface Car {
void accelerate(int amount);
void horn();
void brake(BrakeIntensity intensity);
}
class BmwFactory implements CarFactory {
private Color color;
public BmwFactory(Color theColor) {
color = theColor;
}
public Car createCar() {
return new Bmw(color);
}
}
class AudiFactory implements CarFactory {
private Color color;
public AudiFactory(Color theColor) {
color = theColor;
}
public Car createCar() {
return new Audi(color);
}
}
class LamboFactory implements CarFactory {
private Color color;
public LamboFactory(Color theColor) {
color = theColor;
}
public Car createCar() {
return new Lamborghini(color);
}
}
class Lamborghini implements Car {
private static int MAX_SPEED = 220;
private static int LOW_INTENSITY_BRAKE = 10;
private static int MEDIUM_INTENSITY_BRAKE = 25;
private Color color;
private int currentSpeed;
public Lamborghini(Color theColor) {
color = theColor;
currentSpeed = 0;
}
public void accelerate(int amount) {
currentSpeed = Math.min(currentSpeed + amount, MAX_SPEED);
System.out.println("Lamborghini accelerate -> now speed " + currentSpeed);
}
public void horn() {
System.out.println("Lamborghini HORNN!!!!");
}
public void brake(BrakeIntensity intensity) {
if (intensity == BrakeIntensity.LOW) {
currentSpeed = Math.max(currentSpeed - LOW_INTENSITY_BRAKE, 0);
} else if (intensity == BrakeIntensity.MEDIUM) {
currentSpeed = Math.max(currentSpeed - MEDIUM_INTENSITY_BRAKE, 0);
} else {
currentSpeed = 0;
}
System.out.println("Lamborghini brake -> now speed " + currentSpeed);
}
}
class Audi implements Car {
private static int MAX_SPEED = 180;
private static int LOW_INTENSITY_BRAKE = 5;
private static int MEDIUM_INTENSITY_BRAKE = 30;
private Color color;
private int currentSpeed;
public Audi(Color theColor) {
color = theColor;
currentSpeed = 0;
}
public void accelerate(int amount) {
currentSpeed = Math.min(currentSpeed + amount, MAX_SPEED);
System.out.println("Audi accelerate -> now speed " + currentSpeed);
}
public void horn() {
System.out.println("Audi HORNN!!!!");
}
public void brake(BrakeIntensity intensity) {
if (intensity == BrakeIntensity.LOW) {
currentSpeed = Math.max(currentSpeed - LOW_INTENSITY_BRAKE, 0);
} else if (intensity == BrakeIntensity.MEDIUM) {
currentSpeed = Math.max(currentSpeed - MEDIUM_INTENSITY_BRAKE, 0);
} else {
currentSpeed = 0;
}
System.out.println("Audi brake -> now speed " + currentSpeed);
}
}
class Bmw implements Car {
private static int MAX_SPEED = 200;
private static int LOW_INTENSITY_BRAKE = 2;
private static int MEDIUM_INTENSITY_BRAKE = 20;
private Color color;
private int currentSpeed;
public Bmw(Color theColor) {
color = theColor;
currentSpeed = 0;
}
public void accelerate(int amount) {
currentSpeed = Math.min(currentSpeed + amount, MAX_SPEED);
System.out.println("BMW accelerate -> now speed " + currentSpeed);
}
public void horn() {
System.out.println("BMW HORNN!!!!");
}
public void brake(BrakeIntensity intensity) {
if (intensity == BrakeIntensity.LOW) {
currentSpeed = Math.max(currentSpeed - LOW_INTENSITY_BRAKE, 0);
} else if (intensity == BrakeIntensity.MEDIUM) {
currentSpeed = Math.max(currentSpeed - MEDIUM_INTENSITY_BRAKE, 0);
} else {
currentSpeed = 0;
}
System.out.println("BMW brake -> now speed " + currentSpeed);
}
}
public class Client {
public static void main(String [] args) {
Car carObj = new AudiFactory(Color.BLACK).createCar();
carObj.accelerate(40);
carObj.horn();
carObj.brake(BrakeIntensity.LOW);
}
}







