Creation Mode (III): builder Mode)

Source: Internet
Author: User

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)

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.