Java design pattern: Creator mode

Source: Internet
Author: User

1. What is Creator mode

The creator mode (builder) separates the construction of a complex object from his presentation, allowing the same build process to create different representations. For example, to build a house as an example, the real life of the house looks like a variety of, and each house in the pattern, size and so on are different, but the process of building a house is basically the same. The creator pattern is to create a process that remains the same, but the details of each step in the process can be changed to create different products.

There are several roles in the creator mode

(1), Builder

The abstract builder class, which prescribes the various components of the product, declares the abstract method of acquiring the finished product.

(2), ConcreteBuilder

Concrete builder classes, depending on the creation of different objects, the creation of the process, the completion of construction can obtain specific products.

(3), Director

The command class, invoking different creators to build individual components of a specific product, to ensure that individual parts of the product are created by a fixed process.

(3), Product

To create the product, there are different parts to make up.

2, the same use of "Big talk Design Model" in the example of making villains with Java implementation.

In the role of the game need to create characters, characters including Head, hands, body, feet and other information, but also divided into fat characters and thin characters. The code is as follows

(1), Design product class people

 class   people{ private  list<string> parts = new  Arraylist<string>();  public  void   Addpart (String part) { this  .parts.ad    D (part);  public  void   Show () {SYSTEM.OUT.PRINTLN ( product information .....  for  (int  i = 0; i < parts.size ();        I++
View Code

(2), design abstract builder class Ibuilder

Interface ibuilder{    publicvoid  buildhead ();      Public void buildbody ();      Public void Buildhand ();      Public void Buildfeet ();      Public people Getbuildpeople ();}
View Code

(3), Design Concrete builder class Fatpeoplebuilder and Thinpeoplebuilder

1 classFatpeoplebuilderImplementsIbuilder2 {3     PrivatePeople fatpeople =Newpeople ();4 5 @Override6      Public voidBuildhead ()7     {8         //TODO auto-generated Method Stub9Fatpeople.addpart ("The Fat Man's head");Ten     } One  A @Override -      Public voidbuildbody () -     { the         //TODO auto-generated Method Stub -Fatpeople.addpart ("The body of a fat man"); -     } -  + @Override -      Public voidBuildhand () +     { A         //TODO auto-generated Method Stub atFatpeople.addpart ("The Fat Man's hand"); -     } -  - @Override -      Public voidBuildfeet () -     { in         //TODO auto-generated Method Stub -Fatpeople.addpart ("Fat Man's Feet"); to     } +  - @Override the      PublicPeople getbuildpeople () *     { $         //TODO auto-generated Method StubPanax Notoginseng         returnfatpeople; -     } the } +  A classThinpeoplebuilderImplementsIbuilder the { +  -     PrivatePeople thinpeople =Newpeople (); $  $ @Override -      Public voidBuildhead () -     { the         //TODO auto-generated Method Stub -Thinpeople.addpart ("The head of the Thin Man");Wuyi     } the  - @Override Wu      Public voidbuildbody () -     { About         //TODO auto-generated Method Stub $Thinpeople.addpart ("The body of the Thin Man"); -     } -  - @Override A      Public voidBuildhand () +     { the         //TODO auto-generated Method Stub -Thinpeople.addpart ("The Thin Man's hand"); $     } the  the @Override the      Public voidBuildfeet () the     { -         //TODO auto-generated Method Stub inThinpeople.addpart ("Thin Man's Feet"); the     } the  About @Override the      PublicPeople getbuildpeople () the     { the         //TODO auto-generated Method Stub +         returnthinpeople; -     } the Bayi}
View Code

(4), Design scheduling class director

class director{    publicvoid  build (Ibuilder builder)    {        Builder.buildbody ();        Builder.buildfeet ();        Builder.buildhand ();        Builder.buildhead ();    }}
View Code

(5), test

 Public classbuildertest{ Public Static voidMain (String args[]) {Director Director=NewDirector (); Ibuilder Builder=NewFatpeoplebuilder (); //Create a Fat manDirector.build (builder);        Builder.getbuildpeople (). Show (); //Create thin peopleBuilder =NewThinpeoplebuilder ();        Director.build (builder);    Builder.getbuildpeople (). Show (); }}
View Code

The benefit of the creator pattern is that the client does not have to relate to the specific composition details of the product object, and the concrete builders are independent and easy to expand.

  

  

Java design pattern: Creator 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.