The Cock silk has the money only to spend, that must be some domineering house!
Intent: To separate the construction of a complex object from its representation. Enables the same build process to create different representations.
Applicability:
1, the House has a different number of parts. Capable of independent installation
2, hope to build a number of different appearances of the house
Let's look at a class diagram first.
1.Builder
Specifies an abstract interface for each part that creates a product object.
2.ConcreteBuilder
Implement the builder interface to construct and assemble the individual parts of the product.
Define and understand the representation that it creates.
Provides an interface for retrieving products.
3.Director
Constructs an object that uses the builder interface.
4.Product
Represents a complex object that is constructed. ConcreteBuilder creates an internal representation of the product and defines its assembly process.
Includes defining the classes that make up parts. This includes assembling these parts into the interface of the product at last.
Now the house is to be built. There's a favorite home.
Package creatingtype.builder;/* * Houses */public class House { //flooring private String floor; Wall private String wall; Roof private String housetop; Public String Getfloor () { return floor; } public void Setfloor (String floor) { This.floor = floor; } Public String Getwall () { return wall; } public void Setwall (String wall) { this.wall = wall; } Public String Gethousetop () { return housetop; } public void Sethousetop (String housetop) { this.housetop = housetop; }}
Schedule Project Teams (different project teams to build different houses)
Package creatingtype.builder;/* * Project Team */public interface Housebuilder { //building floor public void Makefloor (); Construction wall public void Makewall (); Construction of the roof public void Makehousetop (); Public house Gethouse ();}
Get a manager.
Package Creatingtype.builder;public class Housedirector {public void Makehouse (Housebuilder builder) { //build Floor Builder.makefloor (); Construction of Wall Builder.makewall (); Build roof builder.makehousetop (); }}
The person in charge has. Suppose you want to build a villa. Determine Villa project team
Package creatingtype.builder;/* * Villa project Team */public class Bieshubuilder implements housebuilder {Home House = new House (); public void Makefloor () { House.setfloor ("Villa-to-floor"); } public void Makehousetop () { house.sethousetop ("Villa-to-Roof"); } public void Makewall () { house.setwall ("Villa-to-wall"); } Public house Gethouse () { return house; }}
The person in charge has, suppose you want to build a mansion, determine the mansion project team
Package creatingtype.builder;/* * Mansion Project Team */public class Haozhaibuilder implements housebuilder {House House = new House (); Public house Gethouse () { return house; } public void Makefloor () { House.setfloor ("mansion-to-floor"); } public void Makehousetop () { house.sethousetop ("mansion-to-Roof"); } public void Makewall () { House.setwall ("mansion-to-Wall");} }
It's all right. You can start building the home you want.
Package creatingtype.builder;//Builder mode public class MainClass { // builder mode is also called builder mode or generator mode, // is one of the 23 design patterns presented by GOF. Builder mode is one of the object-creation patterns that is used to // hide the creation of compound objects, which abstracts the creation //process of compound objects . objects that have composite properties are created in a dynamic//state by means of subclass inheritance and overloading . Public static void Main (string[] args) { //project team Housebuilder builder = new Haozhaibuilder (); Designer Housedirector Director = new Housedirector (); Director.makehouse (builder); House House = Builder.gethouse (); System.out.println (House.getfloor ()); System.out.println (House.getwall ()); System.out.println (House.gethousetop ());} }
Just so simple, the mansion is good, you can also build a villa Oh!
Builder mode (dedicated for cock wire)