Java-based creation design mode-creator Mode

Source: Internet
Author: User

Java-based creation design mode-creator Mode

I. Description

The Creator mode uses a creator class to create objects and build relationships between objects to simplify the complexity of client calls. Compared with the abstract method mode, the Creator mode adds a separate creator class for assembling the relationship between objects and objects, which is responsible for object assembly, in this way, you can clearly divide the responsibilities of each category.

The Creator mode separates the construction of a complex object from its representation, so that different representations can be created during the same construction process, and blocks the construction details of the object on the client, this mode consists of five parts: Assembly class and abstract creator class. It implements the specific creator class of the abstract creator class, abstracts the product class, and implements the specific product class of the abstract product class. It can be seen that there is an assembly class more than the abstract method mode.

 

Ii. Advantages and disadvantages of the Creator Mode

Advantage: In the Creator mode, the client is no longer responsible for object creation and assembly. Instead, a specific Assembly class is used to complete this function and the Assembly responsibility is assigned to the Assembly class, the client is only responsible for calling objects, thus clarifying the responsibilities of each class.

Disadvantage: different types of products can be created using the Creator mode. However, if the product to be created is very different, you need to write multiple creator classes for implementation, which undoubtedly increases the complexity of the Code, in addition, the Creator class has an advantage in constructing the impurity relationship between related components. To add a component, you still need to change the original code and add a new module.

 

Iii. Source Code

Summary: a company needs to calculate salary and social security. We have multiple branches and each branch needs to calculate these two items. Now we need to create branch objects, using each object to call the calculation methods of these two businesses of each company, I need the following classes: Abstract Factory classes (defining interfaces for object creation ), the specific factory classes of the two subsidiaries (implementing the methods for creating objects in the abstract factory class to generate specific objects) and abstract the salary class (defining the standard interface for salary calculation ), the specific salary categories of the two subsidiaries (the method for calculating the salary in the abstract salary class) and the abstract social security class (the standard interface for defining the Social Security calculation ), the specific social security classes of the two subsidiaries (the methods used to calculate social security in Abstract Social Security classes) and Assembly classes (the assembly of the direct relationship between creation and fulfillment of objects ), client call class (used to call the Assembly class assembly object and call the corresponding implementation class method ).

1. Abstract Factory

Package tong. day5_1.builder;/*** Abstract Factory interface, which defines two Abstract METHODS: Salary object and Social Security object, override this method by the specific class implementing this interface, and create the object * @ author tong **/public interface AbstractFactory {// abstract method, respectively, used to create objects of various types: public abstract Salary createSalary (); public abstract Insurance createInsurance ();}
2. Specific Factory
package tong.day5_1.builder;public class GuangdongFactory implements AbstractFactory {@Overridepublic Salary createSalary() {return new GuangdongSalary();}@Overridepublic Insurance createInsurance() {return new GuangdongInsurance();}}
Package tong. day5_1.builder;/*** the specific factory class implements all the methods in the abstract factory, and return the Salary object and Social Security object * @ author tong */public class ZhejiangFactory implements AbstractFactory {@ Overridepublic Salary createSalary () {return new ZhejiangSalary ();} @ Overridepublic Insurance createInsurance () {return new ZhejiangInsurance ();}}

3. Abstract salary class

Package tong. day5_1.builder;/*** defines a salary interface, which is implemented for all companies' specific salary calculation, and overwrites the salary calculation method computeSalary () * @ author tong **/public interface Salary {public void computeSalary ();}
4. Specific salary

 

Package tong. day5_1.builder;/*** the Salary calculation class of Guangdong Branch implements the Salary interface and overwrites the Salary calculation method computeSalary () in this interface () * @ author tong **/public class GuangdongSalary implements Salary {@ Overridepublic void computeSalary () {System. out. println ("Guangdong branch salary calculation ");}}
Package tong. day5_1.builder;/*** the Salary calculation class of Zhejiang Branch implements the Salary interface and overwrites the Salary calculation method computeSalary () in this interface () * @ author tong **/public class ZhejiangSalary implements Salary {@ Overridepublic void computeSalary () {System. out. println ("Zhejiang Branch salary calculation ");}}
5. Abstract Social Security
Package tong. day5_1.builder;/*** abstract Social Security class * @ author tong **/public interface Insurance {public abstract void computeInsurance ();}
6. Specific types of social security

 

Package tong. day5_1.builder; public class GuangdongInsurance implements Insurance {@ Overridepublic void computeInsurance () {System. out. println ("Guangdong branch Social Insurance computing ");}}
Package tong. day5_1.builder; public class ZhejiangInsurance implements Insurance {@ Overridepublic void computeInsurance () {System. out. println ("Zhejiang Branch Social Insurance computing ");}}
6. Assembly

 

Package tong. day5_1.builder;/*** an assembly class is used here to simplify the creation, initialization, and construction of objects on the client * @ author tong **/public class Builder {private AbstractFactory abstractFactory AbstractFactory; // determine which company's factory class object to initialize based on the input parameters. polymorphism is used here. The corresponding public Builder (AbstractFactory abstractFactory) {this is dynamically created based on the input parameters. abstractFactory = abstractFactory;} // The specific implementation method of assembly. Call the method for calculating Salary and social security in the class public void moneySum () {salary Salary = abstractFactory. createSalary (); salary. computeSalary (); Insurance insurance = abstractFactory. createInsurance (); insurance. computeInsurance ();}}
7. Client call class

 

Package tong. day5_1.builder;/*** the client is simplified here, so you don't have to worry about how to assemble and create objects, instead, you only need to call a special assembly class to assemble the object * @ author tong */public class Client {public static void main (String [] args) {// assemble the object Builder builder = new Builder (new ZhejiangFactory (); builder. moneySum (); System. out. println ("--------------"); // assemble the object builder of Guangdong branch = new Builder (new GuangdongFactory (); builder. moneySum ();}}

Iv. Running results

 


 

 

 

 

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.