Java design pattern-Builder pattern (Builder)

Source: Internet
Author: User

Builder mode, which separates the construction of a complex object from its representation, this allows you to create different representations for the same construction process. If you need to separate the construction of a complex object from its representation, you can create different tables during the construction process, we need to apply the "Builder mode", also known as "generator mode", to applicability 1. when creating complex objects, algorithms should be independent of the components of the objects and their assembly methods. 2. When the constructor must allow different representations of the object to be constructed. 3. Users create complex objects. The building sequence of these objects is usually stable, but the building of objects is usually subject to complex changes (such as drawing a person ). Advantages of www.2cto.com: separates the Construction Code from the representation code. Because the builder hides how the product is assembled, if you need to change the internal representation of a product, you only need to define a specific builder. If the builder mode is used, you only need to specify the types to be created, the Builder class is the abstract interface ConcreteBuilder class specified for each part of a product object. It is the specific Builder and implements the Builder interface, construct and assemble the Director class of each component: This class constructs a public interface Builder {public void buildHead (); public void buildBody (); public void buildHand (); public void buildFoot (); public Person buildPerson ();} Public class ManBuilder implements Builder {Person person; public ManBuilder () {person = new Man () ;}@ Override public void buildHead () {Person. setHead ("build man's head! ") ;}@ Override public void buildBody () {person. setBody (" build man's body! ") ;}@ Override public void buildHand () {person. setHand (" build man's hand! ") ;}@ Override public void buildFoot () {person. setFoot (" build man's food! ") ;}@ Override public Person buildPerson () {return person ;}} public class Person {private String head; private String hand; private String body; private String foot; public String getHead () {return head;} public void setHead (String head) {this. head = head;} public String getHand () {return hand;} public void setHand (String hand) {this. hand = hand;} 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;} public class PersonDirector {// create public Person ConstructPerson (Builder B) {B. buildFoot (); B. buildHand (); B. buildHead (); B. buildBody (); return B. buildPerson () ;}} public class Man extends Person {} public class BuilderDemo {/*** @ param args the command line arguments */public static void main (String [] args) {// TODO code application logic here PersonDirector pd = new PersonDirector (); Person person = pd. constructPerson (new ManBuilder (); System. out. println (person. getHead (); System. out. println (person. getBody (); System. out. println (person. getHand (); System. out. println (person. getFoot ());}}

Related Article

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.