Design Model-factory model learning

Source: Internet
Author: User
Tags export class

Design Model-factory model learning
1. Concepts

 

Factory mode definition: instantiate an object and use the factory method instead of the new operation. Achieve decoupling (avoid hard coding coupling ). The Factory method mode is the class creation mode, also known as the Virtual Constructor mode or the Polymorphic Factory mode.

 

The factory mode is our most commonly used model. The famous Jive forum has used the factory mode extensively. The factory mode can be seen everywhere in Java program systems. Because the factory mode is equivalent to creating the new instance object, we often need to generate instance objects according to Class, for example, A a = new A () Factory mode is also used to create instance objects, so in the future, there will be multiple eyes. Can you consider using the factory model? Although doing so may do more work, it will bring more scalability and as few modifications as possible to your system.

 

Common java ee Framework: Spring, Spring container is a huge factory.


2. Purpose

 

Decoupling improves program scalability.


3. Examples

 

 

For example, if there are two jobs, AWork and BWork, there may be more in the future. If we use a simple factory method at this time, we will design a WorkFactory and implement a static method to control which job is generated through parameters. It seems that a simple factory can complete the task, but if you need to add CWork in the future, you need to add the CWork implementation class and modify the factory method. If there are more, the factory class will be too bloated. Because the simple factory mode has only one factory class, it needs to process all the created logic.

 

At this time, the factory method mode is required to deal with the above requirements. In the factory method mode, the core factory class is no longer responsible for the creation of all objects, but the specific creation work is handed over to the subclass. This core class changes and becomes an abstract factory role. It is only responsible for providing the interface that must be implemented by the specific factory subclass without touching the details of which class should be instantiated.

 

This further abstract result allows the factory method mode to allow the system to introduce new products without modifying the specific factory role, this feature undoubtedly makes the factory method model more advantageous than the simple factory model. The following is a UML diagram for the above requirements:

 


 

 

It can be seen that the system using the factory method model involves the following roles:

 

Abstract Factory role: this role is at the core of the factory method mode. Any factory class that creates objects in the mode must implement this interface. In actual systems, this role is often implemented using abstract classes.

 

Specific Factory (AWorkFactory, BWorkFactory) Role: assume this role is a specific JAVA class that implements the abstract factory interface. The specific factory role contains the logic closely related to the business and is called by the user to create a work class.

 

Abstract work role: the superclass of the object created in the factory method mode, that is, the common parent class of all work classes or the APIS jointly owned by them. In actual systems, this role is often implemented using abstract classes.

 

AWork and BWork roles: this role implements the interface declared by the abstract (ExportFile) role. Every object created in the factory method mode is an instance of a specific work role.


4. Source Code

 

Abstract work class:

 

public interface IWork {public void doWork();}

Specific work types:

 

 

Public class AWork implements IWork {@ Overridepublic void doWork () {System. out. println (work on !);}}

Public class BWork implements IWork {@ Overridepublic void doWork () {System. out. println (work on B !);}}

 

 

Abstract Factory:

 

public interface IWorkFactory {public IWork createWork();}

Specific Factory:
public class AWorkFactory implements IWorkFactory {@Overridepublic IWork createWork() {return new AWork();}}

public class BWorkFactory implements IWorkFactory {@Overridepublic IWork createWork() {return new BWork();}}

 

Client Angle class source code:

public class Test {public static void main(String[] args) {IWorkFactory aWorkFactory = new AWorkFactory();IWork aWork = aWorkFactory.createWork();aWork.doWork();IWorkFactory bWorkFactory = new BWorkFactory();IWork bWork = bWorkFactory.createWork();bWork.doWork();}}

 

5. Factory mode and simple factory Mode

 

The factory method mode and the simple factory mode have different structures. The core of the factory method mode is an abstract factory class, while the simple factory mode places the core on a specific class.

 

After the factory method model degrades, it can become much like a simple factory model. Imagine that if a system requires only one specific factory class, you may wish to merge the abstract factory class into a specific factory class. Since there is only one specific factory type, you may wish to change the factory method to a static method. At this time, a simple factory mode is obtained.

 

If the system needs to add a new work CWork, all you need is to add this export class and the corresponding factory class to the system. There is no need to modify the client, or modify the abstract factory role or other existing factory roles. For new export types, this system fully supports the "open-close principle ".

 

 

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.