First, describe
The creator pattern uses a single creator class to create objects and build relationships between objects to simplify the complexity of client calls. In contrast to the abstract method pattern, the creator pattern adds a separate creator class for assembling the relationship between objects and objects, which is responsible for assembling the objects, so that the responsibilities of each class are more clearly divided.
The creator pattern separates the construction of a complex object from its representation, allowing the same build process to create different representations and shielding the client from the construction details of the object, which consists of 5 parts: The Assembly class, the abstract creator class, the concrete creator class of the abstract creator class, the abstract product class, The specific product class of the abstract product class is realized. Here you can see that there is an assembly class that is more than the abstract method pattern.
Ii. advantages and disadvantages of the creator model
Advantage: In the creator mode, the client is no longer responsible for object creation and assembly, but rather a concrete assembly class to complete this function, the Assembly of responsibility to the Assembly class, the client is responsible for the invocation of the object, so that the responsibilities of each class are more explicit.
Cons: You can create different types of products with the creator pattern, but if you are creating a product that has very large differences, you need to write multiple creator classes to implement it, which undoubtedly adds complexity to the code, and the creator class simply has an advantage in constructing the related part's miscellaneous relationship. If you want to add a part or need to change the original code, add a new module.
Third, the source code
General: A company has a calculation of salary and social security needs, we have several branches, each branch to calculate these two, now we need to create a branch object, using each object to call each company to calculate the two business methods to produce results, I need the following classes: Abstract Factory Class (defines the interface to create objects), Two subsidiaries of the specific factory class (to achieve the abstract factory class to create the object of the method to produce specific objects), abstract salary class (definition of payroll calculation of the standard interface), two subsidiaries of the specific salary class (to achieve the method of calculating salary in the abstract salary category), abstract Social Security class (define Social Security calculation standard interface), Two subsidiaries of the specific social Security class (to achieve the abstract social security of social security methods), assembly class (responsible for object creation and redemption of the direct relationship of assembly), the client calls the class (used to call the Assembly class assembly object and invoke the corresponding implementation class method).
1. Abstract Factory class
Package tong.day5_1.builder;/** * Abstract Factory interface, which defines two abstract methods in this interface, each creating a payroll class object and a Social Security class object, overriding the method by implementing the specific class of the interface, creating the object of your class individually * @author Tong * */public Interface Abstractfactory {//abstract method for creating objects of various classes public abstract Salary createsalary ();p ublic Abstract Insurance createinsurance ();}
2. Specific Factory class
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 methods in the abstract factory and returns the salary object and social security object of the class * @author Tong * */public class Zhejiangfactory Imple ments abstractfactory {@Overridepublic Salary createsalary () {return new zhejiangsalary ();} @Overridepublic Insurance createinsurance () {return new zhejianginsurance ();}}
3, Abstract salary category
Package tong.day5_1.builder;/** * Defines a payroll interface that implements this interface for all company-specific payroll calculations and overrides the method of calculating Payroll computesalary () * @author Tong * */public Interface Salary {public void computesalary ();}
4, the specific salary category
Package tong.day5_1.builder;/** * Guangdong Branch Payroll calculation class implements the salary interface and overrides the method of calculating payroll in the interface Computesalary () * @author Tong * */public Class Guangdongsalary implements Salary {@Overridepublic void Computesalary () {System.out.println ("Guangdong branch payroll Calculation");}}
Package tong.day5_1.builder;/** * Zhejiang Branch the Payroll calculation class implements the salary interface and overrides the method of calculating payroll in the interface Computesalary () * @author Tong * */public Class Zhejiangsalary implements Salary {@Overridepublic void Computesalary () {System.out.println ("Zhejiang branch payroll calculation");}}
5, abstract Social Security category
Package tong.day5_1.builder;/** * Abstract Social Security class * @author Tong * */public interface Insurance {public abstract void Computeinsuran CE ();}
6, the specific Social security category
Package Tong.day5_1.builder;public class Guangdonginsurance implements Insurance {@Overridepublic void Computeinsurance () {System.out.println ("Guangdong branch Social Insurance Calculation");}}
Package Tong.day5_1.builder;public class Zhejianginsurance implements Insurance {@Overridepublic void computeinsurance () {System.out.println ("Zhejiang branch Social Insurance Calculation");}}
6. Assembly Class
Package tong.day5_1.builder;/** * An assembly class is used here to simplify the relationship between client-created objects, initialization objects, and building objects * @author Tong * */public class Builder {private Ab Stractfactory abstractfactory;//determines which company's factory class object is initialized, based on the parameters passed in, and uses polymorphism here to dynamically create the corresponding object based on the parameters passed in. Public Builder (abstractfactory abstractfactory) {this.abstractfactory = abstractfactory;} The concrete implementation method of assembling, calling the method of calculating salary and social security in class public void Moneysum () {Salary Salary = Abstractfactory.createsalary (); Salary.computesalary (); Insurance Insurance = Abstractfactory.createinsurance (); Insurance.computeinsurance ();}}
7. The client calls the class
The package tong.day5_1.builder;/** * Client is simplified here, without having to think about how to assemble and create objects, but just call a dedicated assembly class to assemble the object * @author Tong * */public class Client { public static void Main (string[] args) {//assembly of the Zhejiang branch of the object builder Builder = new Builder (new Zhejiangfactory ()); Builder.moneysum (); System.out.println ("--------------");//Assembly of the Guangdong branch of the object Builder = new Builder (new Guangdongfactory ()); Builder.moneysum ();}}
iv. Results of Operation
Java language Implementation-created design pattern-Creator mode