The builder pattern for Java design patterns

Source: Internet
Author: User

This article continues to introduce the builder model of the 23 design pattern series.

Definition: Builder mode: Separates the construction of a complex object from its representation so that the same build process can create different representations.

Practical scope 1, when the algorithm to create complex objects should be independent of the object's components and the way they are assembled. 2, when the construction process must allow the constructed object to have different representations.

Role in this design pattern, there are several roles: 1, Builder: Specify an abstract interface for each part that creates a product object. 2. ConcreteBuilder: Implements the builder interface to construct and assemble the individual parts of the product, defines and clarifies the representations it creates, and provides an interface for retrieving the product. 3. Director: Constructs an object that uses the Builder interface to guide the build process. 4. Product: Represents the complex object being constructed. ConcreteBuilder creates an internal representation of the product and defines its assembly process, which contains the classes that define the constituent parts, including the interfaces that assemble the parts into the final product.

Role builder: [Java] View plaincopyprint? On code to see a snippet derived from my Code slice public interface Personbuilder {void Buildhead ();       void Buildbody ();       void Buildfoot ();  Person Buildperson (); }

Role ConcreteBuilder: [Java] View plaincopyprint? On code to view the chip derivation to my Code slice public class Manbuilder implements Personbuilder {& nbsp      person person;       Public manbuilder () {             person = new Mans ();      }        public void Buildbody () {            person.setbody (" Build a man's body ");      }       public void Buildfoot () {             Person.setfoot ("Build Men's Feet");      }       public void Buildhead () {            Person.sethead ("Build Man's Head");      }       public Person Buildperson ( ) {            return PERSON;&NBsp     } } 

Role ConcreteBuilder: [Java] View plaincopyprint? On code to view the chip derivation to my Code slice public class Womanbuilder implements Personbuilder {& nbsp      person person;       Public womanbuilder () {             person = new Woman ();      }    & nbsp;  public void Buildbody () {            person.setbody (" Build a woman's body ");      }       public void Buildfoot () {             Person.setfoot ("Build a woman's foot");      }       public void Buildhead () {            Person.sethead ("Build a woman's head");      }       public Person Buildperson ( ) {            return person;      } } 

Role director: [Java] View plaincopyprint? On code to view the chip derivation to my Code slice public class Persondirector {     & nbsp Public person Constructperson (Personbuilder pb) {            Pb.buildhead ();            pb.buildbody ();             pb.buildfoot ();             return Pb.buildperson ();      } } 

Role product: [Java] View plaincopyprint? To view code slices to my code slice. public class person {       Private String head;       private string body;       private String foot;& nbsp        public String gethead () {             return head;      }       public void Sethead (String head) {& nbsp           this.head = head;      }       public String getbody () {            return body;      }       public void Setbody (String body) {  & nbsp;         this.body = body;      }       public String getfoot () {            return foot;      }        public void Setfoot (String foot) {            this.foot = foot ;      } }  public class Mans extends person {       public M An () {            System.out.println ("Start building men");       } }  public class Woman extends person {       public Woman () {&nbs P           System.out.println ("Start building women");       } } 

Test: [Java] View plaincopyprint? On code to view a snippet derived from my Code slice public class test{public static void Main (string[] args) {            Persondirector PD = new Persondirector ();            Person Womanperson = Pd.constructperson (New Manbuilder ());       Person Manperson = Pd.constructperson (New Womanbuilder ()); }  }

The builder model can evolve in many forms during use: if there is only one concrete object to be built, the abstract builder and director can be omitted, allowing ConcreteBuilder to play the role of mentor and builder, Even ConcreteBuilder can be put into product. In the "Effective Java" book, the second article mentions "when encountering multiple constructor parameters to consider the builder", in fact, the builder here is the builder mode, but the inside of the four characters are put into the specific product inside.

The above example, if only a man, evolved as follows: [Java] View plaincopyprint? On code to view a snippet derived from my Code slice public class man {       private string head;       private string body;       private string foot;         public String gethead () {             return head;      }       public void Sethead ( String head) {            this.head = head;       }       public String getbody () {             return body;      }       public void Setbody ( String body) {            this.body = body;       }       public String getfoot () {            return foot;      }        public void Setfoot (String foot) {            This.foot = foot;      } } 

[Java] View plaincopyprint? To see code slices on codes derivation to my Code slice public class manbuilder{       man man;  & nbsp;    public Manbuilder () {            man = new Mans ();      }       public void Buildbody () {             Man.setbody ("Building a man's body");      }        public void Buildfoot () {            Man.setfoot ("Build man's Foot");      }       public void Buildhead () {             Man.sethead ("Building a man's head");       }       Public Mans Builderman () {             buildhead ();            buildbody();            buildfoot ();             return man;      } } 

Test: [Java] View plaincopyprint? On code to view a snippet derived from my Code slice public class test{public static void Main (string[] args) {            Manbuilder builder = new Manbuilder (); Mans man = Builder.builderman ();

The builder pattern for Java design patterns

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.