"Go" IOC and Factory mode combine to simplify Factory mode

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/mypm/archive/2011/02/09/1950130.html

Directory:

1: Application Scenario

2: Traditional Factory mode

3: The disadvantages of the traditional factory model in 1 application scenarios

4:IOC and Factory mode connection using the scenario described in implementation 1

1: Application Scenario

In MYPM's workflow business, there are three types of business-to-work workflows: Documents, projects, and tasks; These three business pairs are different when it comes to working on a workflow, and the workflow engine calls the various processors based on different business pairs (documents, projects, and Tasks) .

2: Traditional Factory mode

(1) Simple Factory mode

Usage: The client uses simplefactory to produce a specific product (referenced by abstractproduct), which, of course, requires the client to pass arguments, but the judgment logic is in simplefactory (such as a switch statement).

Extension: If you need to add a specific product E, you have to include the Concreteproducte class and modify the Simplefactory.

Modify: If you need to modify the specific product A, you can only modify the Concreteproducta class.

(2) Factory method mode

Usage: The client chooses the factory according to the desired product (instantiates a specific factory A, referenced with Abstractfactory), and uses the factory to manufacture the product (referenced by abstractproduct).

Extension: If you need to add a specific product E, you will be added Concreteproducte class and Concretefactorye.

Modify: Directly modify the corresponding product.

(3) Abstract method mode

Usage: The client chooses the factory according to the desired product (instantiates a specific factory A, referenced with Abstractfactory), and uses the factory to manufacture the product (referenced by abstractproduct). In contrast, the benefit of the abstract factory model is the ease of conversion of the product's series (e.g. from A-series to B-series).

Expansion: If you need to add products 3 A and 3B, you will have to modify Factorya and Factoryb in addition to adding 3 classes. If you need to increase the product line C, in addition to the corresponding class, but also to increase the FACTORYC class.

Modify: Modify the corresponding class directly.

3: The disadvantages of the traditional factory model in 1 application scenarios

Application Scenarios

In MYPM's workflow business, there are three types of business-to-work workflows: Documents, projects, and tasks; These three business pairs are different when it comes to working on a workflow, and the workflow engine calls the various processors based on different business pairs (documents, projects, and Tasks) .

In the scenario described in 1, there are many if else or switch statements implemented in the code using a simple factory pattern, and classes are added when implemented in a factory or abstract method pattern. And look at the following IOC and Factory mode connection using three factory modes (simple Factory mode, factory method mode, abstract method mode) combined into simple Factory mode use and no if else or switch statement in code

4:IOC and Factory mode connected use Industry implementation 1 described in the scenario

The idea is actually simple :

Holding a map in the factory (via IOC injection), key is the parameter to be passed in the simple factory, and value is the implementation of the processor interface Workflowtaskhandler. Remove the specific "product", the processor in the scene

The following methods are called in the workflow engine

The workflow task type to be processed is passed in the Gethanerbytask method

Workflowtaskhandler handler = Taskhandlerfactory.gethanerbytask (Dto.gettask (). Gettasktype ());

In the Gethanerbytask method, you do not need the else if judgment because the value of Dto.gettask (). Gettasktype () must be a key in the map because of this convention:
Map key 0,1,2, key itself as the value of the business-to-image type, if you add a business pair, and its type value is 5, the map then puts a key to 5 value for the bean that implements the processor interface.

Handlewftask interfaces defined in the processor interface Workflowtaskhandler

Handler.handlewftask (DTO);

See below for specific implementations

4.1 Look at the IOC configuration first, as shown in Taskhandlerfactory holding Workflowhanderhold,workflowhanderhold is a map

Three processors are injected into the workflowhanderhold and can continue to inject any class that implements the Workflowtaskhandler interface, if necessary

4.2 Factory class, as shown in the next two diagrams, the implementation of Taskhandlerfactoryimpl, the code is less

"Go" IOC and Factory mode combine to simplify Factory mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.