Factory model of design pattern

Factory model of design pattern

Label (Space Separation): Design Mode Factory Mode

  • The Impression of Design Patterns
  • Application of Design Patterns
  • Generation of Factory Design Patterns
  • Classification of Factory Design Patterns

The Concept of Design Patterns

> Everything has evolved and everything has developed according to this law. For example, programming, from the beginning of machine language assembly language to C language to c++ Java python... and so on, is constantly evolving. So today's theme design mode is also developed from this, in fact, design mode was initially applied in the field of architecture, and finally evolved to all walks of life. To return to the truth, what is the design pattern? Design pattern is a solution to a certain kind of problem in a specific environment.

Application of Design Patterns

> Design patterns are widely used in programming, for example, factory patterns are widely used in open source framework Spring, which is also the main content of this paper. Then there are the decorative patterns used in the IO processing classes of Java language itself. In fact, we often use these design patterns, but you have not noticed them, so why use these design patterns? This question goes back to the idea of design patterns, so it is not difficult to understand design patterns by asking ourselves. Uses. In fact, it is a series of solutions to solve problems, and these solutions are very effective in dealing with such problems. PS: You can try to look at Spring's source code without using design patterns. You will find out unexpectedly.

Generation of Factory Model

> The emergence of factory model is like showing factories in society. We can reflect on why these projects are needed in society. What would happen without these factories? Because we are in a very developed social environment, then we need to push this process backwards. If there is no factory, if you need a bicycle, what will you do? First of all, you need to make your own iron - > strike iron - > make wheels... And so on. You need to make a list of things. Yes, that's it. Well, it's a terrible thing to think about. In fact, this is a way of thinking, but also a way of learning, learn to assume that there is no such thing, and then the role becomes very clear.

Classification of Factory Patterns

  • SimpleFactory Model
  • abstractFactory Model

Simple factory model

> What is a simple factory model: not all factories are omnipotent factories, that is, some small processing factories in our society, such as the blacksmiths near you, they can only produce simple and single things.

For instance The code for adding, subtracting, multiplying and dividing in the calculator is as follows As you can see from the code, OperationFactory can only construct specific + - */ operators. Imagine if you ask for the remainder? What should I do? There are many ways to add another switch case directly. What if there is no source code? So, that's AbstractFactory.

OperationFactory
public class OperationFactory {

    public static Operation createOperation(String operationCode) {

        Operation operation = null;

        switch (operationCode) {
            case "+":
                operation = new OperationAdd();
                break;
            case "-":
                operation = new OperationSub();
                break;
            case "*":
                operation = new OperationMul();
                break;
            case "/":
                operation = new OperationDiv();
                break;
            default:
                operation = new OperationAdd();
                break;
        }

        return operation;

    }
Operation
public interface Operation {

    int computer(int x, int y);

}
Operation-related subclasses
public class OperationAdd implements Operation {
    @Override
    public int computer(int x, int y) {
        return x+y;
    }
}
public class OperationDiv implements Operation{

    @Override
    public int computer(int x, int y) {
        return x/y;
    }
}
public class OperationMul implements Operation {

    @Override
    public int computer(int x, int y) {
        return x * y ;
    }
}
public class OperationSub implements Operation{
    @Override
    public int computer(int x, int y) {
        return x - y;
    }
}

AbstractFactory Model

> The reason why there is no abstract factory model is that the simple factory model can not meet the demand. The current demand needs flexible expansion. That is to say, I can produce whatever I need. If there is no corresponding production plan before, I can increase the production plan only by adding a production chain instead of rebuilding one. A factory. In the code, if there is no source code, the dependent source code needs to be extended, then the implementation of the dependency does not need to be reproduced, just inherit or implement a class in the source code can be simply extended.

More is said than an example can clearly show. In the development process, we often encounter payment related things, such as Alipay, WeChat, UnionPay... Many payments. We're considering designing a PayFactory project that uses unused payment tools based on the customer's choice. The code is as follows

Pay correlation
    public abstract class Pay {
    public abstract String pay();
}
     public class AliPay extends Pay {
    @Override
    public  String pay() {
        return "Alipay payment";
    }
}
 public class WXPay extends Pay {
    @Override
    public String pay() {
        return "Wechat Payment";
    }
}
PayFactory correlation
public abstract class PayFactory {
    public  abstract Pay createAliPay();
    public  abstract Pay createWxPay();

}
 public class DefaultPayFactory extends PayFactory {

    @Override
    public Pay createAliPay() {
        return new AliPay();
    }

    @Override
    public Pay createWxPay() {
        return new WXPay();
    }
}

What do you need to do if you need to expand a two-dollar payment to pay more? The code is as follows

Custom Pay Related
public class QddPay extends Pay {
    @Override
    public String pay() {
        return "Dual payment";
    }
}
Implementing PayFactory correlation
public class CustomPayFactory extends DefaultPayFactory {

    @Override
    public Pay createAliPay() {
        return super.createAliPay();
    }

    @Override
    public Pay createWxPay() {
        return super.createWxPay();
    }

    public Pay createQddPay() {
        return new QddPay();
    }

}

> In summary, we can see the role of factory mode. The above code can run the test on its own. I wish you all success in your studies.

Keywords: Programming Java Spring Assembly Language

Added by gibbo101 on Wed, 10 Jul 2019 23:45:00 +0300