How programmers respond to the north-canton-high room rate illustration builder mode

Source: Internet
Author: User

First, preface

The recent northward wide house price rub up to rise, if you do not buy a house house price is high and you a little hair relationship is not, but most people still face to buy a house.

North Canton It entrepreneurial proportion is still quite high, so many programmers face expensive as the house, there is no bitter to say.

No matter how the price rises, no matter how high prices, we write procedures to laugh about life, to love work, love the People, love socialism, always uphold the leadership of the party!

It's a bit far from the house, and we're going to return to today's focus-the builder model.

Ii. Basic Concepts

Builder pattern: Separates the construction of a complex object from its representation so that the same build process can create different representations. Builder mode can also be called generator mode.

The builder pattern consists of four roles: product role, abstract builder role, concrete builder role, conductor role

Automotive Example:

Well understood, like the car example in the book, the car is a complex object that includes a variety of components such as wheels, steering wheel, and transmitters. Different cars have different configurations, and for us, we need only one car, no need to know which parts are made up.

It can be designed and described by the builder mode, which separates the parts from the assembly process and creates a complex object step-by-step.

The user simply needs to specify the type of the complex object to be able to get the object without having to know the specifics of the construction inside.

But if you have a car in Shanghai, you have to have a license plate in Shanghai, want to have Shanghai's license plate must have at least 80,000 yuan, with this money you may not be able to get the license plate (this is how many rich people pain)! You may not be able to go to work by bike before you get to the company with a Shanghai license plate. Ha ha. It's pulling away again.

KFC Package Example:

The builder mode can be used to describe how KFC creates a package: The package is a complex object, it usually contains staple foods (such as hamburgers, chicken rolls, etc.) and beverages (such as fruit juice, cola, etc.) and other components, different packages have different components, and KFC waiter can according to customer requirements, the step by stage assembly of these components , construct a complete package and return it to the customer.

If you don't understand, I'll give you an example of how a programmer can deal with the north-Canton-High house price.

Third, code example

The sample code is as follows:

 //Product role: House         Public classHouse {//The house is made up of several roomsilist<string> _rooms =Newlist<string>(); //Add a room             Public voidAddroom (stringGuest) {_rooms.            ADD (guest); }            //back to all the rooms             Public voidGetrooms () {Console.WriteLine ("House Type:"); foreach(varTheinch_rooms)                {Console.WriteLine (guest); }            }        }        //Abstract Builder: Building a house         Public Abstract classBuildhouse {//The House must satisfy the basic: eat, drink, pull, scatter, sleep//Kitchen             Public Abstract voidBuildcitchen (); //Toilet             Public Abstract voidBuildbathroom (); //Bedroom             Public Abstract voidBuildbedroom (); //Living room             Public Abstract voidBuildlingingroom (); //back to the house             Public AbstractHouse Gethouse (); }        //Concrete Builders: Rural self-built 450 ping 4 Room 2 Hall 2 Wei 1 kitchen 2 floor villa cost 200,000         Public classCountyhouse:buildhouse {Private ReadOnlyHouse _house =NewHouse (); //Kitchen             Public Override voidBuildcitchen () {_house. Addroom ("50 flat-size oversized kitchen"); }            //Toilet             Public Override voidBuildbathroom () {_house. Addroom ("50 Flat Oversized toilet 1"); _house. Addroom ("50 Flat Oversized toilet 2"); }            //Bedroom             Public Override voidBuildbedroom () {_house. Addroom ("50 flat-size bedroom 1"); _house. Addroom ("50 flat-size bedroom 2"); _house. Addroom ("50 flat-size bedroom 3"); _house. Addroom ("50 flat-size bedroom 4"); }            //Living room             Public Override voidBuildlingingroom () {_house. Addroom ("50 Flat large living room 1"); _house. Addroom ("50 Flat large living room 2"); }            //back to the house             Public OverrideHouse Gethouse () {return_house; }        }        //Concrete Builders: North Canton deep Buy 60 ping 1 Room 1 Hall 1 kitchen 1 Wei average 40,001 flat also to 2.4 million         Public classCityhouse:buildhouse {Private ReadOnlyHouse _house =NewHouse (); //Kitchen             Public Override voidBuildcitchen () {_house. Addroom ("10 Flat Kitchen"); }            //Toilet             Public Override voidBuildbathroom () {_house. Addroom ("10 Flat Toilet"); }            //Bedroom             Public Override voidBuildbedroom () {_house. Addroom ("20 Flat Bedroom"); }            //Living room             Public Override voidBuildlingingroom () {_house. Addroom ("20 flat living room"); }            //back to the house             Public OverrideHouse Gethouse () {return_house; }        }        //Conductor role: Directing house building process         Public classDirector { Public voidBuilder (Buildhouse house) {House.                Buildcitchen (); House.                Buildbathroom (); House.                Buildbedroom (); House.            Buildlingingroom (); }        }

Client calling Code:

        Static voidMain (string[] args) {            //instantiation of the commanding personDirector director=NewDirector (); //instantiation of rural building villasCountyhouse countyhouse=NewCountyhouse (); Director.            Builder (Countyhouse); House House=Countyhouse.gethouse (); House.            Getrooms (); //Instantiate North Canton Deep purchaseCityhouse Cityhouse =NewCityhouse (); Director.            Builder (Cityhouse); House House1=Cityhouse.gethouse (); House1.            Getrooms ();        Console.readkey (); }

Operating effect:

The house is a complex object, it wants to ensure people's basic life and eat and drink to sleep, generally including bedroom, kitchen, bathroom, living room and other parts of the house, different houses have different components (such as a small family room, three rooms large type), and real estate developers can assemble a variety of types to meet the needs of different people, Then sell the price to the people.

Iv. Summary

1, the client only focus on the required products, do not need the specific internal composition of the product.

2, the client can be based on different specific creators to obtain different products.

3, compared with the abstract factory, the builder mode returns the complete product instance; The abstract factory returns a series of related products

"such as the curd of MSSQL and the curd of MongoDB; home appliances are made with Gerry (washing machine, TV, air conditioner, refrigerator ...). ) or with Haier washing machine, TV, air conditioning, refrigerator ... "

4, abstract Factory mode can be understood as the creation of specific household appliances (washing machine, TV, air conditioning, refrigerator ...). ), builder mode is an example of the creation of an electrical brand (lattice, Haier)

Sample code Download: HTTPS://YUNPAN.CN/CYWPTHPAWTVGD (extract code: 2CAC)

How programmers respond to the north-canton-high room rate illustration builder mode

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.