Series of review

  1. Learn some design patterns. – Overview

Introduction to the

Factory Pattern is one of the most commonly used design patterns. This type of design pattern is the creation pattern, which provides the best way to create objects quickly and easily.

In the factory pattern, we create objects without exposing the callers to the logical flow of creating them, and by using a common interface to point to the newly created objects. The factory pattern defines an interface to create an object and lets subclasses decide which factory class to instantiate. The factory pattern defers the creation process to subclasses to solve the interface selection problem.

For example

  1. When I buy a car, I can pick it up from the factory, and I don’t have to worry about how the car was made or what the details of the car look like.

  2. I want to buy a phone, I don’t need to know how it’s made, I just need to know if I want an iPhoneX or a HUAWEI P10, and the factory will make it for me.

advantages

  1. Create an object, just know the name of it, like I just know the phone is called iPhoneX;

  2. High scalability, if you want to add a product, just extend a factory class can;

  3. To implement masking, the user only cares about the interface.

Disadvantages Every time an implementation is added, a concrete class and object implementation factory need to be added, so that the number of classes in the system increases exponentially, which increases the complexity of the system to a certain extent, but also increases the dependence of the system concrete class.

Usage scenarios

  1. Logger: Records may be recorded to a local file, a remote server, printed to a terminal, etc. The user only needs to know where to record instructions.

  2. Database access: when the user does not know which kind of database the system uses at last, and the database may change, it is Oracle now, it may change to MySQL, etc., at this time, only the dialect and driver can be changed.

  3. Design a framework to connect to the server, either through “TCP” or “UDP” or other protocols, as a product class, and implement an interface together.

The instance

Take, for example, the manufacturing plant that produces the iPhone.

Simple Factory model

1. First need to create an iPhone interface and its implementation class: interface iPhone, implementation class iphone7\iphone8\iphonex;

public interface Iphone {    void build();
}Copy the code
public class Iphone7 implements Iphone {    @Override    public void build() {
        System.out.println("iphone7 get!");
    }
}Copy the code
public class Iphone8 implements Iphone {    @Override    public void build() {
        System.out.println("iphone8 get!");
    }
}Copy the code
public class Iphonex implements Iphone {    @Override    public void build() {
        System.out.println("iphonex get!");
    }
}Copy the code

2. Then define the factory class iphoneFactory

public class IphoneFactory { public Iphone getIphone(String iphoneType) { if (iphoneType == null) { return null; } if (iphoneType.equalsIgnoreCase("iphone7")) { return new Iphone7(); } else if (iphoneType.equalsIgnoreCase("iphone8")) { return new Iphone8(); } else if (iphoneType.equalsIgnoreCase("iphonex")) { return new Iphonex(); } return null; }}Copy the code

3. Then you can produce anything you want, like an iphonex hahaha

public class FactoryDemo { public static void main(String[] args) { IphoneFactory iphonefactory = new IphoneFactory(); Iphone iphone = iphonefactory.getIphone("iphonex"); iphone.build(); }}Copy the code

Factory method pattern

1. First need to create an iPhone interface and its implementation class: interface iPhone, implementation class iphone7\iphone8\iphonex;

public interface Iphone {    void build();
}Copy the code
public class Iphone7 implements Iphone {    @Override    public void build() {
        System.out.println("iphone7 get!");
    }
}Copy the code
public class Iphone8 implements Iphone {    @Override    public void build() {
        System.out.println("iphone8 get!");
    }
}Copy the code
public class Iphonex implements Iphone {    @Override    public void build() {
        System.out.println("iphonex get!");
    }
}Copy the code

2. Then define the factory interface iphoneFactory

Public interface IphoneFactory {public iphonegetiphone (); }Copy the code

3. Implement the factory interface

public class Iphone7Factory implements IphoneFactory { @Override public Iphone getIphone() { return new Iphone7(); }}Copy the code
public class Iphone8Factory implements IphoneFactory { @Override public Iphone getIphone() { return new Iphone8(); }}Copy the code
public class IphonexFactory implements IphoneFactory { @Override public Iphone getIphone() { return new Iphonex(); }}Copy the code

3. Then you can make anything you want, like an iphone8

public class FactoryDemo { public static void main(String[] args) { IphoneFactory iphonefactory = new Iphone8Factory(); Iphone iphone = iphonefactory.getIphone(); iphone.build(); }}Copy the code

This article has been hosted on GitHub, welcome Star

The factory pattern source: https://github.com/holtenko/DesignPatterns/tree/master/src/Factory