Introduce:
The creator mode belongs to the creation pattern, and the core idea is to separate the building algorithm of a complex object from its constituent parts and assembly mode, so that the construction algorithm and the assembly mode can deal with the change independently, then the parts can be reused, the Assembly can adapt to different changes, and the different construction algorithms can get the difference result. The creator mode consists of 5 components: ABSTRCT Builder, concrete builder, Abstract product, concrete product and Director.
We can give an example to build a house. The house is abstract Product, bungalow, Villa, thatched house ... The specific product, the worker is the abstract Builder, the Masons, the carpenter, the craftsman who laid the foundation, the craftsman who paved the floor ... Specific builders, these craftsmen have skills, or several hands, but if they have to build their own house, they do not know how to cover, they can only do their own specialty, but the director know how to build the house (what kind of house how to cover him), because he had to study, The kung fu on paper is just, but you have to let him play the foundation, he can not do, you want to let him move bricks, he said "he will" (programmers are not all to move bricks), the results were peasant workers laughed; some people want to build a house, what kind of demand, to director, he began to command all kinds of workers, Who first to do (of course, first hit the foundation, the old House reconstruction does not need to play the foundation), who continue to do, this is a strategic (here to introduce the strategy model, according to the needs of customers to generate corresponding products).
Advantages and Disadvantages
Advantage: In the creator mode, the client is no longer responsible for product creation and assembly, but instead of the product creation process to the specific creator, the client is only responsible for invoking the object, so that can do their own work, director just how to design the house, how to cover , all kinds of migrant workers as long as skilled their own skills on the line.
Cons: If you can't reuse builder, each builder is called less often, each product is a big difference, it requires a lot of builder, adds complexity to the code, and if I make changes to the existing builder's functionality, it will also affect other product.
The following demo I only wrote a builder, the specific environment can be self-grasp, in the actual project I am using the creator model, but also introduced the policy model.
Specific products, can do abstract
PackageBuilderpattern; Public classThe hostel {PrivateString Foundation; PrivateString window; PrivateString door; PrivateString Floor; PrivateString roof; PublicString getfoundation () {returnFoundation; } Public voidsetfoundation (String Foundation) { This. foundation =Foundation; } PublicString GetWindow () {returnwindow; } Public voidSetWindow (String window) { This. window =window; } PublicString Getdoor () {returndoor; } Public voidSetdoor (String door) { This. Door =door; } PublicString Getfloor () {returnFloor ; } Public voidSetfloor (String floor) { This. Floor =Floor ; } PublicString Getroof () {returnroof; } Public voidsetroof (String roof) { This. Roof =roof; }}
Abstract Creator
Package Builderpattern; Public Interface Ibuilder { void buildfoundation (); void Buildwindow (); void Builddoor (); void Buildfloor (); void buildroof (); The Getroom ();}
Specifically created by
PackageBuilderpattern; Public classBuilderImplementsIbuilder {PrivateA . PublicBuilder () { This. Hostel =New(); } @Override Public voidbuildfoundation () {System.out.println ("Build Foundation ..."); } @Override Public voidBuildwindow () {System.out.println ("Build window ..."); } @Override Public voidBuilddoor () {System.out.println ("Build door ..."); } @Override Public voidBuildfloor () {System.out.println ("Build floor ..."); } @Override Public voidBuildroof () {System.out.println ("Build roof ..."); } @Override PublicGetroom () {return This. Hostel; }}
Director
PackageBuilderpattern; Public classDirector {PrivateIbuilder Builder; PublicDirector () {} PublicDirector (Ibuilder builder) { This. Builder =Builder; } Public voidOrderbuilder () {if(NULL!= This. Builder) { //First build Foundation;builder.buildfoundation (); //Second Build floorBuilder.buildfloor (); //Third build door and windowBuilder.builddoor (); Builder.buildwindow (); //Fourth Build roofBuilder.buildroof (); } }}
Call
PackageBuilderpattern; Public classMain { Public Static voidExecbuilderpattern () {System.out.println ("Builder Pattern:"); //Builder implements all build method, while they don't know how to build a.Builder Builder =NewBuilder (); //Director know how to build a-a-class, but him doesn ' t know.Director Director =NewDirector (builder); Director.orderbuilder (); the=Builder.getroom (); System.out.println ("End---------------------------"); }}
Design pattern-Behavioral mode-creator mode