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