Make a little progress every day, keep at it, and you will always be different. Come on!

ZooKeeper, Spring bucket, source code, Dubbo, Elasticsearch, Redis, MySql, RabbitMQ, Kafka, Linux, microservices, etc.

Continue to update, welcome to pay attention to my public back-end architecture!

Today’s focus is on factory design patterns, which can save us a lot of time and redundant code. The power of the factory design pattern will become clear later when we learn about Spring source code, AOP, things, and so on.

Old rule, first on the table of contents:

I. Introduction to factory design mode

Ii. Classification of factory design mode

3. Simple factory design mode

4. Factory method design mode

Fifth, abstract factory design pattern

Six, three models summary

I. Introduction to factory design mode

The factory design pattern is the most commonly used instantiation object pattern, which can reduce the coupling of the program and provide great convenience for the later maintenance and modification. You will select implementation classes and create objects for unified management and control. This decouples the caller from our implementation class.

Ii. Classification of factory design mode

There are three kinds of factory design mode: simple factory design mode, factory method design mode and abstract factory design mode.

Each design pattern has its own advantages and disadvantages and can be used according to different business scenarios. Let’s take a look at each of these design patterns and demonstrate how they work.

3. Simple factory design mode

As we all know, the notebook we use every day has many brands, such as lenovo, Dell, Asus and so on. Each brand is made by a different manufacturer. We use the simple factory design pattern to achieve:

We created a computer interface, lenovo and Dell implemented the computer interface and created a factory. Used to produce computers, and then created a client to use. We’ll post the code for that

/** * @description: simple factory * @author: aerCool * @since: 2020-03-15 09:23 * @version: V1.0.0 */ public interface Computer {void run(); }Copy the code
public class Lenovo implements Computer {

    @Override
    public void run() {
        System.out.println("I'm lenovo Notebook."); }}Copy the code
public class Dell implements Computer {

    @Override
    public void run() {
        System.out.println("I'm Dell Notebook."); }}Copy the code
public class Factory {

    public static Computer create(String name) {

        if (name == null) {
            return null;
        }

        if ("Lenovo".equals(name)) {
            return new Lenovo();
        }

        if ("Dell,".equals(name)) {
            return new Dell();
        }

        returnnull; }}Copy the code
public class Client {

    public static void main(String[] args) {
        Computer lenovo = Factory.create("Lenovo");
        Computer dell = Factory.create("Dell,"); lenovo.run(); dell.run(); }}Copy the code

The output

I'm Lenovo Notebook. I'm Dell NotebookCopy the code

Advantages and disadvantages of the simple factory model

Advantages: The simple factory pattern can determine which concrete class objects should be created based on information given from the outside world. It clearly distinguishes their responsibilities and powers, which is conducive to the optimization of the entire software architecture.

Disadvantages: It is obvious that the factory class centralizes the creation logic for all instances, violating the highly cohesive responsibility assignment principle.

4. Factory design method and mode

Factory Method pattern, also known as polymorphic Factory pattern. In the factory method pattern, the core factory class is no longer responsible for all product creation, but passes the specific creation work to subclasses. The core class becomes an abstract factory role, responsible only for the interfaces that a specific factory subclass must implement, without touching the details of which product class should be instantiated.

Let’s show you what we’ve done. We’ve adjusted it as follows

Instead of having the same factory make computers, we have specific computer manufacturers make them. The responsibilities are more clear. Lenovo computers are produced in Lenovo factories, and Dell computers are produced in Dell factories, and the output is guaranteed to households.

public class LenovoFactory implements Factory {

    @Override
    public Computer create() {
        returnnew Lenovo(); }}Copy the code
public class DellFactory implements Factory {

    @Override
    public Computer create() {
        returnnew Dell(); }}Copy the code

Eventually, this is how computers were made in simple factories

public class Client { public static void main(String[] args) { Computer lenovo = new LenovoFactory().create(); Computer dell = new DellFactory().create(); lenovo.run(); dell.run(); }}Copy the code

The output is as follows

I'm Lenovo Notebook. I'm Dell NotebookCopy the code

We find it very convenient to maintain different types of products, the single responsibility principle.

But if one day the boss says not only do we want to make computers, but we also want to make the parts ourselves instead of buying them. How do we do that?

This is where our abstract factory design pattern comes into play. Keep reading.

Fifth, abstract factory design pattern

Abstract factories are simply factories of factories. Abstract factories can create concrete factories and produce concrete products by concrete factories.

The specific factory is understood as the packaging factory may be more appropriate, such as lenovo packaging factory needs to assemble products, including monitors, motherboards, memory and so on.

Let’s see

We have graphics card manufacturers, motherboard manufacturers, graphics card: AMD and GTX graphics card, motherboard: Gigabyte and microstar.

For example, our Lenovo computer is with GTX graphics card, Gigabyte motherboard; Dell with AMD graphics card, microstar motherboard, we look at the specific code how to achieve

/** * @description: 显卡 * @author: aerCool * @since: 2020-03-15 10:18 * @version: V1.0.0 */ public interface Display {void show(); }Copy the code
/** * @description: motherboard * @author: aerCool * @since: 2020-03-15 10:20 * @version: V1.0.0 */ public interface MainBoard {void run(); }Copy the code
/** * @description: AMD显卡 * @author: aerCool * @since: 2020-03-15 10:22 * @version: V1.0.0 */ public class implements Display {@override public voidshow() {
        System.out.println("AMD General Graphics Card"); }}Copy the code
/** * @description: GTX graphics * @author: aerCool * @since: 2020-03-15 10:21 * @version: V1.0.0 */ public class implements Display {@override public voidshow() {
        System.out.println("GTX High Performance Graphics Card"); }}Copy the code
/** * @description: aerCool * @author: aerCool * @since: 2020-03-15 10:23 * @version: V1.0.0 */ public class JiJiaMianBoard implements MainBoard {@override public voidrun() {
        System.out.println("Gigabyte Motherboard"); }}Copy the code
/** * @author: aerCool * @since: 2020-03-15 10:24 * @version: v1.0.0 */ public class WeiXinMainBoard implements MainBoard {

    @Override
    public void run() {
        System.out.println("Microstar motherboard"); }}Copy the code

The parts are ready. We’re going to pack lenovo and Dell

/** * @description: aerCool * @author: aerCool * @since: 2020-03-15 09:48 * @version: V1.0.0 */ public implements Factory {@override public DisplaycreateDdisplay() {// With GTX graphics cardreturn new GTXDisplay();
    }

    @Override
    public MainBoard createMainBoard() {// With gigabyte motherboardreturnnew JiJiaMianBoard(); }}Copy the code
/** * @author: aerCool * @author: aerCool * @since: 2020-03-15 09:48 * @version: V1.0.0 */ public implements Factory {@override public DisplaycreateDdisplay() {// Use AMD graphics cardreturn new AMDDisplay();
    }

    @Override
    public MainBoard createMainBoard() {// Use mSI motherboardreturn new WeiXinMainBoard(); }}Copy the code

Ok, let’s see what happens

/** * @description: test * @author: aerCool * @since: 2020-03-15 09:30 * @version: V1.0.0 */ public class Client {public static void main(String[] args) {Factory lenovo = new LenovoFactory(); Display levDisplay = lenovo.createDdisplay(); MainBoard levMainBoard = lenovo.createMainBoard(); levDisplay.show(); levMainBoard.run(); System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -"); Factory dell = new DellFactory(); Display dellDdisplay = dell.createDdisplay(); MainBoard dellMainBoard = dell.createMainBoard(); dellDdisplay.show(); dellMainBoard.run(); }}Copy the code

The output is as follows

GTX high-performance graphics gigabyte motherboard -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - AMD graphics CARDS in general Msi motherboardCopy the code

Abstract factory model may not be very easy to understand, try it yourself, later we analyze the Sping source code when further split explain. We can understand it well with Spring’s bean factory.

Six, three models summary

1. If the product is relatively certain and the change is relatively small, it is recommended to use a simple factory.

2. There are many application scenarios of factory method in daily work.

3. Abstract methods are widely used in large projects and some source code. , such as Spring’s Bean factory.

Well, that’s it for today. Any mistakes are welcome.

Like to remember to pay attention to my public number: back-end architecture advanced study together!!