Separate the internal representation of a product from the product generation process to generate a product object with different internal representations. The construction mode allows the product's internal appearance to change independently, so that the customer does not have to know the details of the product's internal composition. The construction mode enforces a step-by-step construction process.
Example:
1 Class House {
2 Private String base;
3 Private String wall;
4 Private String roof;
5 Public String getbase (){
6 Return Base;
7 }
8 Public Void Setbase (string base ){
9 This . Base = base;
10 }
11 Public String getwall (){
12 Return Wall;
13 }
14 Public Void Setwall (string wall ){
15 This . Wall = wall;
16 }
17 Public String getroof (){
18 Return Roof;
19 }
20 Public Void Setroof (string roof ){
21 This . Roof = roof;
22 }
23 Public String tostring (){
24 Return This . Base + "" + This . Wall + "" + This . Roof;
25 }
26 }
27
28 Interface Builder {
29 Public Void Bucket parta ();
30 Public Void Buildpartb ();
31 Public Void Buildpartc ();
32 Public House getrusult ();
33 }
34
35 Class Housebuilder Implements Builder {
36 Private House house;
37 Public Housebuilder (){
38 House = New House ();
39 }
40 Public Void Bucket parta (){
41 House. setbase ("Foundation Construction completed ");
42 }
43 Public Void Buildpartb (){
44 House. setwall ("wall construction completed ");
45 }
46 Public Void Buildpartc (){
47 House. setroof ("roof built ");
48 }
49 Public House getrusult (){
50 Return House;
51 }
52 }
53
54 Class Director {
55 Public House construct (builder bulider ){
56 Bulider. Bucket parta ();
57 Bulider. buildpartb ();
58 Bulider. buildpartc ();
59 Return Bulider. getrusult ();
60 }
61 }
62
63 Public Class Test {
64 Public Static Void Main (string [] ARGs ){
65 Director d = New Director ();
66 House = D. Construct (New Housebuilder ());
67 System. Out. println (house );
68 }
69 }
At this time, we can build any house, only the foundation, only the wall, only the roof, any combination ...... (-Is that a house)