Introduction to the

The Factory Pattern is specifically responsible for instantiating a large number of classes that have a common interface. The factory pattern can dynamically decide which classes to instantiate without having to know in advance which classes to instantiate each time.

Simple Factory model

Simple Factory Pattern: Also known as Static Factory Method Pattern, it belongs to the class creation Pattern. In the simple factory pattern, you can return instances of different classes depending on the parameters. The simple factory pattern specifically defines a class that is responsible for creating instances of other classes, which usually have a common parent class. Simple factories are not one of the 23 design patterns.

The instance

All kinds of happy water (refers to myself) happy water (product interface)

public interface Kls {

    String name(a);

}
Copy the code

Fat House Happy Water – Cola (specific product)

public class Coke implements Kls {
    @Override
    public String name(a) {
        return "Fat House Happy water - Coke."; }}Copy the code

Happy Water – Sprite (Specific product)

public class Sprite implements Kls {
    @Override
    public String name(a) {
        return "Happy Water - Sprite"; }}Copy the code

Happy Water Works (Factory)

public class KlsFactory {

    public static Kls getFzs(String type) throws Exception {
        Kls fzs = null;
        if ("coke".equalsIgnoreCase(type)) {
            fzs = new Coke();
        } else if ("sprite".equalsIgnoreCase(type)) {
            fzs = new Sprite();
        }
        if (Objects.isNull(fzs)) {
            throw new RuntimeException("No happy water.");
        }
        returnfzs; }}Copy the code

Fat House (client)

public class Fz {
    @Test
    public void drink(a) throws Exception {
        // Make coke
        Kls coke = KlsFactory.getFzs("coke");
        System.out.println("Fat house begins to drink:" + coke.name());

        // Make Sprite
        Kls sprite = KlsFactory.getFzs("sprite");
        System.out.println("Fat house begins to drink:"+ sprite.name()); }}Copy the code

UML class diagrams

Advantages:

  1. Hiding the implementation: for products like Coke and Sprite, the client doesn’t need to know the implementation, just how to use it.

  2. The client just needs to use the object directly, and doesn’t care how the object is new;

  3. Coupling: there is no hard code like new XXX on the client side. The late designer changes the framework and all classes (except the factory class) are changed. As long as the name of the factory class and method name are not changed, the client code will not change a line.

Disadvantages:

  1. Extensions are cumbersome, requiring changes to existing code. If you want to add a new product class, you need to change the factory class code.
  2. With all the logic in the factory class, the product will be extremely difficult to read, and if it goes wrong, it will be all GG.

Factory method pattern

The Factory Method pattern is a step further from the simple Factory. Instead of providing a single Factory class to create all objects, we provide different factories for different objects. That is, each object has a factory corresponding to it.

The instance

Follow the happy water example above. The KlsFactory is abstracted from the common method, and then the concrete KlsFactory is realized respectively.

Happy water general factory

public interface Factory {
    /** * make happy water **@return Kls
     */
    Kls create(a);
}
Copy the code

The coke plant

public class CokeFactory implements Factory {
    @Override
    public Kls create(a) {
        return newCoke(); }}Copy the code

Sprite factory

public class SpriteFactory implements Factory {
    @Override
    public Kls create(a) {
        return newSprite(); }}Copy the code

Fat curtilage

public class Fz {
    @Test
    public void drink(a) throws Exception {
        // Make coke
        CokeFactory cokeFactory = new CokeFactory();
        Kls coke = cokeFactory.create();
        System.out.println("Fat house begins to drink:" + coke.name());

        // Make Sprite
        SpriteFactory spriteFactory = new SpriteFactory();
        Kls sprite = spriteFactory.create();
        System.out.println("Fat house begins to drink:"+ sprite.name()); }}Copy the code

Expand Fanta Happy Water Fanta Happy water

public class Fanta implements Kls {
    @Override
    public String name(a) {
        return "Happy Water -- Fanta."; }}Copy the code

The fender factory

public class FantaFactory implements Factory {
    @Override
    public Kls create(a) {
        return newFanta(); }}Copy the code

Fat house add

FantaFactory fantaFactory = new FantaFactory();
Kls fanta = fantaFactory.create();
System.out.println("Fat house begins to drink:" + fanta.name());
Copy the code

UML class diagrams

Advantages:

  1. Some aspects of the simple factory model are retained entirely;
  2. New products, do not need to change the existing code, just implement the correspondingProduct class,Factory implementation class.

Disadvantages:

  1. Increase system burden;

  2. When more new products are added, the amount of code will increase dramatically.

  3. A factory can only produce one product.