Create pattern (vi) Builder mode

Source: Internet
Author: User

Before I introduce this design pattern, let me say two points of knowledge. Is-a, Has-a.

Is-a refers to a class that is one of the other classes and belongs to a relationship.

Has-a refers to a responsibility that represents a role.

A summary of a sentence

Create a complex object that is composed of the various parts of the molecular object that are combined by certain steps. This is the builder pattern.

Plot

Xiao Ming and the goddess in the process of communication, always feel slow progress. So Xiaoming decided to use a candlelight dinner surprise, to impress the goddess.

But Xiao Ming was alone, and he came out of the day to offer the goddess. So he's going to hire someone to prepare for a candlelight dinner.

The plot looks very simple, but let's analyze how to construct the scene.

First, the necessary characters are xiaoming and the hired one. If you use builder mode. Xiao Ming can be regarded as the conductor, and then hired by small a (from a name, convenient) is the builder.

Then we want to define what the specific job is. and a candle-lit dinner with something.

Finally, we are commanded by Ming Li and employed by the people who work.

Then we'll use the builder model to construct the plot.

Task one: Create a candlelight Dinner

First create candlelight dinner this class, altogether two methods, ready and start eating.

 Public classSupper {IList<string> list =Newlist<string>();  Public voidADD (stringname) {list.        ADD (name); }         Public voidGetsupper () {foreach(stringNameinchlist) {Console.WriteLine (name+". Finished"); } Console.WriteLine ("dinner is served."); }    }

Prepare the steps with a list to store, start eating will first cycle the list, and then to enjoy.

Task two: Create a builder small a

First, the task of the builders is abstracted into classes. Then the small a inherits this abstract class, to realize the concrete operation.

 Public Abstract classHire {//Candles         Public Abstract voidcandle (); //Steak         Public Abstract voidsteak (); //Champagne         Public Abstract voidChampagne (); //Candlelight Dinner         Public AbstractSupper getsupper (); }     Public classSomea:hire {Supper Su=NewSupper ();  Public Override voidCandle () {Su.add ("Buy Candles"); }         Public Override voidChampagne () {Su.add ("Making Steak"); }         Public Override voidsteak () {Su.add ("take out the champagne ."); }         Public OverrideSupper Getsupper () {returnSu; }    }

A message is added to the candlelight dinner every step of the way. The code is simple, nothing to say.

Task three: Create Xiao Ming's classmates

Xiao Ming students have two methods, the first conductor small a prepare dinner. The second with the goddess after dinner.

 Public class someming    {        publicvoid  Read (Hire hi)        {            hi. Candle ();            Hi. Champagne ();            Hi. Steak ();        }          Public void Go (Hire hi)        {            = hi. Getsupper ();            Sup. Getsupper ();        }    }

Very simple, nothing to say.

Task four: Xiangmingyo Goddess Dinner

Xiao Ming classmate about goddess, and then call small a let him prepare dinner. Then go for a candlelight dinner.

Static voidMain (string[] args) {Console.WriteLine ("Xiangmingyo out goddess"); Console.WriteLine ("Xiao Ming began to let small a prepare dinner"); Someming m=Newsomeming (); Hire Hi=NewSomea ();            M.read (HI); Console.WriteLine ("Dinner's Ready .");            M.go (HI);        Console.ReadLine (); }

Execution results

Summarize

Builder mode, creating a candle-lit dinner this complex object is divided into several small responsibilities, which are stitched together into a complex object. And the conductors and builders also learn to decouple.

If I add builders, just implement abstract classes. The conductor and the existing builder do not have to make any changes.

Common scenarios: you need to build a batch of products with the same process but very complex construction processes.

Select key: whether the product construction process is the same.

Finally, a few words.

At this point, the creation of 6 design mode mode, the introduction is complete. Whether you understand it or not, I understand it anyway.

Create design patterns, which are the patterns used to create objects. And the process of instantiation is abstracted.

All created design patterns have two things in common:

They all encapsulate the system using those specific classes.

Second, they hide how instances of these classes are created and composed.

Design patterns can be used in combination, such as in an abstract factory, with a singleton pattern. It's all possible.

Next, we introduce 7 design patterns of the structural type. Our hero Xiao Ming classmate, in the end with the goddess what will happen? We went down to see

by~

Create pattern (vi) 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.