Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

The factory pattern is also one of the creation patterns and is used to create instances that have the same behavior, which is specific to different types of instances. For example, apples and bananas are fruits, they both have color properties, and their colors are different. When we need to create different instances under different conditions or scenarios, we need to consider whether we can use the factory pattern. And factory mode is divided into simple factory mode, factory method mode, abstract factory mode, the following one to understand the simple factory mode and factory method mode writing.

Simple Factory model

The simple factory pattern caller doesn’t know how to create an instance, it just needs to tell what instance to create.

writing

  1. Create an interface Fruit and specify the abstract method color to return the Fruit color:
public interface Fruit {
	String color();
}
Copy the code
  1. Create the Apple and Banana classes to implement the Fruit interface, re-color the method, and return its own color.
public class Apple implements Fruit{ @Override public String color() { return "red"; } } public class Banana implements Fruit{ @Override public String color() { return "yellow"; }}Copy the code
  1. Create a factory class that returns different instances based on different parameters.
public class FruitFactory { Fruit getFruit(String name) { if(name.equals("apple")) { return new Apple(); } if(name.equals("banana")) { return new Banana(); } return new Apple(); }}Copy the code
  1. Call using the factory class to get different instances.
FruitFactory factory = new FruitFactory();
Fruit apple = factory.getFruit("apple");
Fruit banana = factory.getFruit("banana");
System.out.println("apple:" + apple.color());
System.out.println("banana:" + banana.color());
Copy the code

Running results:

Apple: red banana: yellowCopy the code

Factory method pattern

Unlike the simple factory pattern, the factory method pattern creates a factory interface, and then each Fruit creates a factory to implement that interface, deferring instantiation of a class to subclasses.

writing

  1. Create the factory interface and declare the create instance method createFruit()
public interface Factory {
    Fruit createFruit();
}
Copy the code
  1. Create a factory class for Apple and Banana instances to implement the factory interface.
public class AppleFactory implements Factory{ @Override public Fruit createFruit() { return new Apple(); } } public class BananaFactory implements Factory{ @Override public Fruit createFruit() { return new Banana(); }}Copy the code
  1. Instantiate the Apple and Banana factories respectively and call the createFruit method to get the instance.
Factory appleFactory = new AppleFactory(); Factory bananaFactory = new BananaFactory(); System.out.println("apple: "+ applefactory.createfruit ().color()); System. The out. Println (" banana: "+ bananaFactory. CreateFruit (). The color ());Copy the code

Differences between the simple factory pattern and the factory method pattern

  • The simple factory mode method delays the instantiation of a class to subclasses by passing different instance types directly to the caller to obtain instances.
  • The factory method pattern moves the instantiation logic to the caller, where the caller chooses whether to use AppleFactory or BananFactory to create the instance. So when you add a fruit type, you create not only a fruit class, but also an additional fruit factory class.

Design patterns: singleton pattern