Design pattern Start--generator mode

Source: Internet
Author: User

Generator mode

The principle of design pattern: As far as possible in the main program, the code extension is strong, multi-use interface and virtual classes, less use of implementation classes, so that the time to change the program is more convenient.

1, function: the production of a product class requires a number of other class unit sequence generation after the combination, and the production method of these unit is not fixed.

For example, an object can have some important properties, and the object cannot be used as a complete product until they have the proper value. For example, an e-mail message has a sender address, a recipient address, a subject, a content, an appendix, and so on, and the e-mail message cannot be sent until the minimum recipient address has not been assigned a value. In some cases, some properties of an object must be assigned in a certain order to make sense. Another property cannot be assigned to a property until it is assigned a value. These conditions make the construction of nature itself related to complex business logic. At this time, this object is equivalent to a product to be built, and the object of these properties equivalent to the parts of the product, the process of building the product is the process of combining parts. Because the process of assembling parts is complex, the composition of these "parts" is often "externally" to an object called a builder, which is returned to the client by a Product object where all the parts have been built. (Excerpt from http://www.cnblogs.com/lds85930/articles/643143.html)

2. Class Diagram:

3. Realize

(1) Unit definition units

 Public class Unit {    null;      Public Unit (String type)    {        this. Type = type;    }       Public String getInfo ()    {        return  type;    }}
View Code

(2) Ibuider defines the process that makes up the unit and returns the product

 Public Interface Ibuilder {    publicvoid  createone ();      Public void createtwo ();      Public Product getproduct ();}
View Code

(3) Ibuilderone is an implementation class of Ibuilder, which defines a way of unit composition product

 Public classIbuilderoneImplementsibuilder {Product P=NewProduct (); @Override Public voidCreateone () {P.one=NewUnit ("In first one"); } @Override Public voidCreatetwo () {p.two=NewUnit ("in First"); } @Override PublicProduct getproduct () {returnp; }}
View Code

(4) Ibuildertwo is an implementation class of Ibuilder, which defines another way of unit composition product

 Public classIbuildertwoImplementsibuilder {Product P=NewProduct (); @Override Public voidCreateone () {P.one=NewUnit ("in Second One"); } @Override Public voidCreatetwo () {p.two=NewUnit ("In Second,"); } @Override PublicProduct getproduct () {returnp; }}
View Code

(5) The Director defines which ibuilder to use and returns the product

 Public classDirector {Ibuilder builder;  PublicDirector (Ibuilder builder) { This. Builder =Builder; }     PublicProduct getproduct () {builder.createone ();        Builder.createtwo (); returnbuilder.getproduct (); }     Public Static voidMain (string[] args) {Ibuilder builder=NewIbuildertwo (); Director Director=NewDirector (builder); Product Product=director.getproduct ();    Product.showinfo (); }}
View Code

(6) Product class, which defines which originals the product needs to combine

 Public class Product {    Unit one;    Unit A;      Public void Showinfo ()    {        System.out.println (One.getinfo ()+ "-" +Two.getinfo ());}    }
View Code

Design pattern Start--generator 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.