White words of factory design pattern

show me the code and take to me , to do it more clear to say
GitHub project JavaHouse synchronous included
like to point a praise chant! Your support is my share of power!

Introduced

We often hear the factory model, for example, with a factory pattern Spring matter, the interview will be asked about the factory pattern, etc., which see the factory pattern is more important in the daily development of a design pattern. In fact, the factory model is relatively simple a design pattern, and this year seems to understand the factory pattern, you are embarrassed to say that you learned programming as (manual dog's head). Well, allow me to look tortured soul, do you really know the factory model.

UML 类图

Do not worry, we take a look at UML class diagram. Learn about how a code indicating a UML class diagram. Look at how to represent an entity class. code show as below:

public class B {
    
    private String name;
    private String password;
    
    public void test(){
        
    }
}
复制代码

This is a simple entity class. His look UML class diagram as follows:

After reading the entity classes, we take a look at the interface, as follows:

public interface B {
    void test();
}
复制代码

This is a simple access class. General Interface to put a few ways to hold property. His look UML class diagram as follows:

After reading the entity classes and interfaces, let's see if they have anything to do with it. Take a look at how inheritance, said:

A glance through his arrow Features --- hollow. The arrow points to the parent class. This is expressed inheritance.

Read inheritance, we look at how to implement interface represents.

Here we can see a similar relationship with the inheritance, but to realize becomes dashed arrows pointing interface class.

Not finished, no, we look at the dependency, this relationship is relatively weak, as is typically a class parameter represents another class, UML diagram is as follows:

Dependency is a broken line, a half arrow

Here, that's common uml class diagram shows.

Simple Factory

Get to the point, what is the factory model, let's look at a simple factory pattern, factory pattern is not in fact a simple factory pattern, but he can help us to understand the factory model, so let's look at a simple factory pattern.

Requirements: We need to do a computer can subtract, multiply and divide, to use a simple factory pattern.

UML 类图:

Mathematical:

import lombok.Data;

@Data
public abstract class Operation {
    private Double numA;
    private Double numB;
    /**
     * 运算方法
     */
    abstract Double getResult();
}
复制代码

Class addition:

public class OperationAdd extends Operation{

    @Override
    Double getResult() {
        return getNumA() + getNumB();
    }
}
复制代码

Multiplication categories:

public class OperationMul extends Operation{
    @Override
    Double getResult() {
        return getNumA() * getNumB();
    }
}
复制代码

Division categories:

public class OperationDiv extends Operation{
    @Override
    Double getResult() {
        if (getNumB() == 0){
            throw new RuntimeException("除数不能为零!");
        }
        return getNumA() / getNumB();
    }
}


复制代码

Simple factory class:

public class CreateOperationFactory {
    public static Operation createOperation(String operate){
        Operation operation = null;
        switch (operate){
            case "+":
                operation = new OperationAdd();
                System.out.println("这是+法");
                break;
            case "/":
                operation = new OperationDiv();
                System.out.println("这是/法");
                break;
            case "*":
                operation = new OperationMul();
                System.out.println("这是*法");
                break;
        }
        return operation;
    }
}

复制代码

Test categories:

public class Test {
    public static void main(String[] args) {
        Operation operation = CreateOperationFactory.createOperation("+");
        operation.setNumA(1D);
        operation.setNumB(2D);
        System.out.println(operation.getResult());

        operation = CreateOperationFactory.createOperation("*");
        operation.setNumA(1D);
        operation.setNumB(2D);
        System.out.println(operation.getResult());

        operation = CreateOperationFactory.createOperation("/");
        operation.setNumA(1D);
        operation.setNumB(2D);
        System.out.println(operation.getResult());
    }
}
复制代码

Output Interface:

这是+法
3.0
这是*法
2.0
这是/法
0.5
复制代码

Factory Pattern

Requirements: To add another subtraction, ah? The above subtraction is not it? I guess you certainly did not find no subtraction above. See also thought I was lazy, get away with it. Things are not so simple.

In general, add a subtraction Well, simple, I directly went down inside plus a branch factory class does not get away. Haha, everyone knows, this time, we have violated an important principle - the principle of openness is closed. The so-called open closed principle, we should not repair the source code, the code ancestral you dared to touch, try, but by inheriting the parent class or implement an interface to expand the code to achieve our objective.

uml 类图:

code show as below:

public interface IFactory {
    Operation createOperation(String operate);
}


public class OperationAddFactory implements IFactory{
    @Override
    public Operation createOperation(String operate) {
        Operation operation = new OperationAdd();
        System.out.println("这是+法");
        return operation;
    }
}
复制代码

Bickering

Yes, this is part of their own bars.

What is factory pattern ah! I own newa corresponding object is not to solve the problem, why write a factory class out. (Bar the bar ah, this is the thing wants)

Ok? I can not seem to refute ????? it is not there (with my third finger push, my glasses). Imagine a place to go to use newit. We look at newa dozen places, and then found to be the class name, this time how to do. A one change slightly, own newobjects, cried and have to be changed completely. But if the factory class, we only need to change a place can be, is that simple.

One advantage to this it? I still like one by one newah.

Certainly not (this is a bar fine ah), of course, a simple newobject does not need to write out a class. But newan object is not so simple, we need to be a target to operate it. Spring IOC default mechanism for creating an object is a singleton, then we need now is the only subject of how to do. At this time, we can write is a singleton class inside the factory, to achieve our objective.

These are my bickering link.

to sum up

No matter what design patterns, in fact, is to code reuse, write less code, we do know what model is what model, but also to understand the role. As the saying goes, the purpose of martial arts is to germinal, the starting point of the war for peace. In short, we know these know why.

See here are people.

reference

juejin.im/post/5d318b...
"Westward Design Patterns"
www.zhihu.com/question/24... - factory design pattern What is the use?

No micro-channel public attention, ready to move end reading

No public .jpg

Guess you like

Origin juejin.im/post/5e093d8e6fb9a0164a10dd02