The Simple Factory pattern is not really a design pattern, but rather a common programming habit! It is also called the Static Factory Method pattern.

The simple factory pattern is probably the simplest pattern in the factory pattern family, and it is also the most commonly used pattern.

As always, we learn the simple Factory model by simulating real problems!

Questions lead

Suppose we want to open a pizzeria, and we need a way to implement an orderPizza, depending on the type of pizza. The general code is as follows:

Pizza orderPizza(String type) {
	Pizza pizza;
	
	if(type.equals("cheese")) {
		pizza = new CheesePizza();
	} else if (type.equals("greek")) {
		pizza = new GreekPizza();
	} else if (type.equals("cock")) {
		pizza = new CockPizza();
	}
	
	pizza.prepare();
	pizza.bake();
	pizza.cut();
	pizza.box();
	
	return pizza;
}
Copy the code

This part of the code is in the implementation of a new pizza work, according to different types

	Pizza pizza;
	
	if(type.equals("cheese")) {
		pizza = new CheesePizza();
	} else if (type.equals("greek")) {
		pizza = new GreekPizza();
	} else if (type.equals("cock")) {
		pizza = new CockPizza();
	}
Copy the code

Obviously, this is a problem, and if we want to increase or decrease a pizza category, we need to modify the orderPizza method. Closing the modification cannot be implemented.

At the same time, we realized that after the new pizza object, we did the following work:

	pizza.prepare();
	pizza.bake();
	pizza.cut();
	pizza.box();
Copy the code

It’s all the same! This part is the constant part. Therefore, we consider that we can extract the changed and unchanged parts to achieve the purpose of encapsulation.

Build a simple pizza factory

We will extract part of the code of the new object that will change, and encapsulate it with another object, which according to its function is to create a pizza, we will call it factory. In this way, the orderPizza method becomes the customer of the factory object. When a pizza object is needed, it is as if the factory object is asking to make one. This is the design idea of a simple factory.

public class SimplePizzaFactory {
	public Pizza createPizza(String type) {
		Pizza pizza = null;
		
		if(type.equals("cheese")) {
			pizza = new CheesePizza();
		} else if (type.equals("greek")) {
			pizza = new GreekPizza();
		} else if (type.equals("cock")) {
			pizza = new CockPizza();
		}
		
		returnpizza; }}Copy the code

This implements the simple factory pattern, and the simple factory object can provide pizza to multiple pizza shops at the same time, so when changes occur, we just need to modify this class!

Refactoring PizzaStore class

public class PizzaStore {
	SimplePizzaFactory factory;
	
	public PizzaStore(SimplePizzaFactory factory) {
		this.factory = factory;
	}
	
	Pizza orderPizza(String type) {
	Pizza pizza;
	
	pizza = factory.createPizza(type);
	
	pizza.prepare();
	pizza.bake();
	pizza.cut();
	pizza.box();
	
	returnpizza; }}Copy the code

Take a look at the simple factory pattern class diagram we designed:

PizzaStore is a customer of the factory. He obtains pizza instance from the factory object. Pizza is the product of the factory object, and the factory object completes the work of instantiating pizza instance.

summary

The simple factory pattern is not strictly a design pattern, but a programming habit. Its core idea is to take the instantiated code that changes and create a new simple factory class to encapsulate it!