Begin August 19, 2018 19:44:25
Simple factory mode
definition
As long as you say, as long as I have, I'll give it to you.
Simple Factory Pattern: also known as static factory method (Static Factory Method) mode. Design modes are divided into creation mode, structure mode and behavior mode. Simple factory mode belongs to creation mode. In simple factory mode, different instances can be returned according to different parameters. Simple factory mode specifically defines a class to be responsible for creating instances of other classes, and the created instances usually have a common parent Class.
role
Factory role: the factory role is responsible for implementing the internal logic for creating all instances
Abstract Product role: the abstract Product role is the parent class of all created objects and is responsible for describing the public interface of all instances
Concrete product: the object that is finally created
Class diagram
Sequence diagram
(I don't know if the painting is right)
Code example
Abstract product role (IOperation.java):
package com.mingmingcome.designpattern.factory.simplefactory; /** * @className: IOperation * @Description: Operation interface * @author: luhaoming * @date: 2018 August 19, 2014 4:43:14 PM */ public interface IOperation { public int getResult(int a, int b); }
The common interface of the instance created by the simple factory pattern: the operation interface.
Specific product roles (AddOperationImpl.java, SubOperationImpl.java, MulOperationImpl.java, DivOperationImpl.java):
package com.mingmingcome.designpattern.factory.simplefactory; /** * @className: AddOperationImpl * @Description: Addition implementation class * @author: luhaoming * @date: 2018 August 19, 2014 4:46:25 PM */ public class AddOperationImpl implements IOperation{ @Override public int getResult(int a, int b) { return a + b; } } package com.mingmingcome.designpattern.factory.simplefactory; /** * @className: SubOperationImpl * @Description: Subtraction implementation class * @author: luhaoming * @date: 2018 August 19, 2014 4:48:55 PM */ public class SubOperationImpl implements IOperation { @Override public int getResult(int a, int b) { return a - b; } } package com.mingmingcome.designpattern.factory.simplefactory; /** * @className: MulOperationImpl * @Description: Multiplication implementation class * @author: luhaoming * @date: 2018 August 19, 2014 4:52:04 PM */ public class MulOperationImpl implements IOperation { @Override public int getResult(int a, int b) { return a * b; } } package com.mingmingcome.designpattern.factory.simplefactory; /** * @className: DivOperationImpl * @Description: Subtraction implementation class * @author: luhaoming * @date: 2018 August 19, 2014 4:52:48 PM */ public class DivOperationImpl implements IOperation { @Override public int getResult(int a, int b) { return a / b; } }
The specific object to be created: addition, subtraction, multiplication and division operation.
Factory role (SimpleFactory.java):
package com.mingmingcome.designpattern.factory.simplefactory; /** * @className: SimpleFactory * @Description: Simple factory class * @author: luhaoming * @date: 2018 August 19, 2014 4:54:20 PM */ public class SimpleFactory { public static IOperation createOperation(String op) { IOperation operation = null; // 1. The scope of application is limited: byte, short, char, int and their packaging classes, as well as Enum and String // switch(op){ // case "+": // operation = new AddOperationImpl(); // break; // case "-": // operation = new SubOperationImpl(); // break; // case "*": // operation = new MulOperationImpl(); // break; // case "/": // operation = new DivOperationImpl(); // break; // } // 2. All objects if ("+".equals(op)) { operation = new AddOperationImpl(); } else if ("-".equals(op)) { operation = new SubOperationImpl(); } else if ("*".equals(op)) { operation = new MulOperationImpl(); } else if ("/".equals(op)) { operation = new DivOperationImpl(); } return operation; } }
It is responsible for creating an object factory. It can distinguish which object to instantiate through "+", "-", "*", "\" of String type. This judgment flag can be set to anything you want, and Chinese "addition", "subtraction", "multiplication" and "division" are also possible.
Test class (SimpleFactoryTest.java):
package com.mingmingcome.designpattern.factory.simplefactory; /** * @className: SimpleFactoryTest * @Description: Simple factory mode test class * @author: luhaoming * @date: 2018 August 19, 2005 5:05:36 PM */ public class SimpleFactoryTest { public static void main(String[] args) { int a = 999, b = 888; // plus IOperation operation = SimpleFactory.createOperation("+"); int add = operation.getResult(a, b); System.out.println("a '+' b:" + add); // reduce operation = SimpleFactory.createOperation("-"); operation.getResult(a, b); int sub = operation.getResult(a, b); System.out.println("a '-' b:" + sub); // ride operation = SimpleFactory.createOperation("*"); operation.getResult(a, b); int mul = operation.getResult(a, b); System.out.println("a '*' b:" + mul); // except operation = SimpleFactory.createOperation("/"); operation.getResult(a, b); int div = operation.getResult(a, b); System.out.println("a '/' b:" + div); } }
advantage
1. The factory class contains necessary judgment logic to determine when to create an instance of which product class. The client can exempt the responsibility of directly creating product objects and only "consume" products. The simple factory mode realizes the division of responsibilities and provides special factory classes for creating objects.
2. The client does not need to know the class name of the specific product class created, but only needs to know the parameters corresponding to the product class. For some complex class names, the user's memory can be reduced through the simple factory mode. In this paper, the client only needs to remember "+", "-", "*", "/".
3. By introducing the configuration file, you can replace and add new specific product classes without modifying any client code, which improves the flexibility of the system to a certain extent
shortcoming
1. Since the factory class centralizes all product creation logic, once it fails to work normally, the whole system will be affected.
2. Using simple factory mode will increase the number of classes in the system, and increase the complexity and understanding difficulty of the system to a certain extent.
3. It is difficult to expand the system. Once a new product is added, the factory logic has to be modified. If there are many product types, the factory logic may be too complex, which is not conducive to system expansion and maintenance.
4. Because the static factory method is used in the simple factory mode, the factory role cannot form a hierarchical structure based on inheritance.
Usage scenario
The factory class is responsible for creating fewer objects.
summary
Simple factory mode: the client does not need to know what object to create, but only needs to know the parameters. The factory class is responsible for logical judgment to create a specific object, and the specific product object contains the specific implementation logic. The abstract product class provides the same interface for the specific product object class.
reference resources:
August 21, 2018 18:36:51