(iii) Generator pattern-code implementation

Source: Internet
Author: User

Concept: The complex function of an entity is separated from the entity, and this complex function is diverse.

For example, clothing factories (entities) have the function of making clothes, each garment has a different manufacturing process, thus separating the complex and variable functions of making clothes.

Usage scenario: One of the functions of an entity is complex and not only one

Benefits: Extended functionality does not affect solids, solids are more cohesive, and low coupling with variable parts

Instance:

Production process Interface

 Package Note.com.builder;  Public Interface Builder {    public  String Build (Factory Factory);    }

Production 1 Process

 Package Note.com.builder;  Public class Implements builder{    public  String Build (Factory Factory) {        = factory.getone1 () + FACTORY.GETTWO2 () +factory.getthree1 () +factory.getfour1 ();         return clothes;}    }

Production 2 Process

 Package Note.com.builder;  Public class Implements builder{    public  String Build (Factory Factory) {        = factory.getone2 () + FACTORY.GETTWO1 () +factory.getfour1 ();         return clothes;}    }

Clothing Factory Entity

 PackageNote.com.builder; Public classFactory {PrivateString one1 = "Textile Cloth"; PrivateString one2 = "Nylon"; PrivateString two1 = "cropping"; PrivateString TWO2 = "shaded"; PrivateString three1 = "plus collar"; PrivateString four1 = "stitch";  Publicstring Build (Builder builder) {string clothes= Builder.build ( This); System.out.println ("Production process:" +clothes); returnclothes; }             PublicString getOne1 () {returnone1; }     Public voidsetOne1 (String one1) { This. one1 =one1; }     PublicString GetOne2 () {returnOne2; }     Public voidsetOne2 (String one2) { This. One2 =One2; }     PublicString getTwo1 () {returntwo1; }     Public voidsetTwo1 (String two1) { This. TWO1 =two1; }     PublicString GetTwo2 () {returnTWO2; }     Public voidSetTwo2 (String two2) { This. TWO2 =TWO2; }     PublicString getThree1 () {returnthree1; }     Public voidsetThree1 (String three1) { This. three1 =three1; }     PublicString GetFour1 () {returnFour1; }     Public voidSetFour1 (String four1) { This. Four1 =Four1; }        }

Test class

 Package Note.com.builder;  Public class buildertest {    publicstaticvoid  main (string[] args) {          New  Factory ();         New Builder1 ();         New Builder2 ();        Factory.build (builder1);        Factory.build (Builder2);    }}

Results

Production process: textile fabric color plus collar stitching
Production process: nylon cutting and stitching

As a result, the production process of the plant is separated from the factory, and every time the production of new clothes is added, the builder's implementation is needed.

(iii) Generator pattern-code 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.