The 11th chapter of the design pattern-builder mode (Java implementation)

Source: Internet
Author: User

The 11th chapter of the design pattern-builder mode (Java implementation)

  "I was drunk in that restaurant 、、、" "What's the matter?" "The Order of serving, the first dessert, then a cold drink, then the dish or something, no words dead." "That's a bit of a order, too." However, it is very rare, normal if it is Chinese food, are cold dishes before the hot dish, then the soup, and finally some dessert or something. Western food Now, first has the first dish, uses for the appetizer, then is the soup (feels strange to say), then then is the auxiliary dish, the main course, the vegetable class, the dessert, the drink again. Then France is the Blablabla 、、、 "(the author has been dragged away by the crowd.) "Let me finish, there are few countries left ~ AH ~ ~"). Cough, return to the right turn. You asked me who I was? And look at the following self-introduction.

Self-Introduction to the builder model

Since you sincerely ask, then I will be merciful to tell you, in order to prevent the software industry is destroyed, in order to maintain the software industry peace, the Builder (PS: In the Chinese version of the Gof translated into the generator mode, of course, in order to my Enlightenment book "Big Talk Design mode" salute, in this follow the translation, Founder ~), I am shuttle in the Galaxy design mode, White hole, white tomorrow waiting for me, is Jiangzi, aim ~ My English definition is: separate the construction of a complex object from its representation so T He same construction process can create different representations. My Chinese definition is: separate the construction of a complex object from its representation so that the same build process can create different representations. The class diagram is as follows:

  

  The basic situation is like this. Each class diagram is given in detail on the functional responsibility diagram.

Self-analysis of builders ' patterns

It's time for self-dissection. Oh no, it is self-analysis, in view of the previous few said they have no shortcomings of the tragic fate of the model, I will not say no shortcomings, but first of all, the advantages:

    • Separate the construction code from the presentation code.
    • More granular control over the construction process is possible.
    • Can change the internal representation of a product. This is mainly because the Builder object provides the command with an abstract interface to construct the product. This interface allows the generator to hide the representation and internal structure of the product.

Implementation of the construction model

  Just took the fish elder brother to drag away, in order to apologize, sent as a foodie of fish brother a pile of eating chat table Sorry (a fish: Attis, who miss me again). Well, let's go over the order of the dishes. The first is Foodbuilder, which is used to assemble the order of serving.

The first is the abstract class of serving order:

1  Public Abstract classfoodseq{2     //order of execution for each basic method3     Privatearraylist<string> seq =NewArraylist<>();4 5     //Serving6     protected Abstract voidFood ();7     //Upper Soup8     protected Abstract voidsoup ();9     //on WineTen     protected Abstract voidwine (); One  A     Final  Public voideat () { -         //Loop, which one to eat first -          for(inti = 0; i< This. Seq.size (); i++ ) { theString s = This. Seq.get (i); -             if(S.equalsignorecase ("Food")) { -                  This. Food (); -             } +             Else if(S.equalsignorecase ("Soup")) { -                  This. Soup (); +             } A             Else if(S.equalsignorecase ("Wine")) { at                  This. wine (); -             } -         } -     } -  -     //Get Order Value in     Final  Public voidSetsequence (arraylist<string>seq) { -          This. Seq =seq; to     } +}
View Code

  Secondly, the order code of Chinese dishes:

1  Public classChseqextendsfoodseq{2     //Serving3     protected Abstract voidFood () {4System.out.println ("Cui hua, shang pickled cabbage, oh no, serving ~");5     }6     //Upper Soup7     protected Abstract voidsoup () {8System.out.println ("Little Two, on the soup.");9     }Ten     //on Wine One     protected Abstract voidwine () { ASYSTEM.OUT.PRINTLN ("The Boss, a bottle of Tang and song Yuan Ming and Qing years of aging"); -     } -}
View Code

The following is an abstract class of Foodbuilder:

 1  public  abstract  class   foodbuilder{ //  build a model, give a serving order  3  public  abstract  void  Setsequence (Arraylist<string> sequence);  4   After Setup, you can get the order chart of serving directly  5  public  abstract   Foodseq Getfoodseq ();  6 } 

  Here is the Builder class implementation class for Chinese cuisine:

1  Public classChbuilderextendsfoodbuilder{2     PrivateChseq ch =Newchseq ();3      Publicchseq getchseq () {4         return  This. Ch;5     }6      Public voidSetsequence (arraylist<string>sequence) {7          This. Ch.setsequence (sequence);8     }9}

The next is our most important commander, he is in the Vivo, the top commander in the battle, the commander of the fighter, oh mouth slippery, the combat officer:

1  Public classdirector{2     Privatearraylist<string> seq =NewArrayList ();3     PrivateChbuilder ch =NewChbuilder ();4 5     //order A, soup first, after serving, last wine6      Publicchseq getaseq () {7         //Clear Cache8          This. Seq.clear ();9         //Order aTen          This. Seq.add ("Soup"); One          This. Seq.add ("Food"); A          This. Seq.add ("Wine"); -          This. Ch.setsequence ( This. seq); -         return(CHSEQ) This. Ch.getchseq (); the     } -  -         //Order B, serve first, then the wine, and finally the soup . -      Publicchseq getaseq () { +         //Clear Cache -          This. Seq.clear (); +         //Order a A          This. Seq.add ("Food"); at          This. Seq.add ("Wine"); -          This. Seq.add ("Soup"); -          This. Ch.setsequence ( This. seq); -         return(CHSEQ) This. Ch.getchseq (); -     } -}
View Code

  Well, the concrete realization of this is over.

Application Scenarios for builders ' patterns

    • There are the same methods but the execution order is different, resulting in different event structures.
    • Multiple parts or parts can be assembled into an object.
    • Product classes are very complex or have different performance in order of invocation in a product class.

  Predict funeral how, and listen to tell, hungry, eat to go ~

PS: This blog welcome forwarding, but please specify the blog address and author ~

Blog Address: http://www.cnblogs.com/voidy/

Blog: http://voidy.gitcafe.com

<. ))) ≦

The 11th chapter of the design pattern-builder mode (Java implementation)

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.