Java design mode serialization (3)-builder Mode

Source: Internet
Author: User

Java Design Pattern serialization (3) -- builder Pattern

 

Intent:

Separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.

Applicability:

  • When creating complex objects, algorithms should be independent of the components of the objects and their assembly methods.
  • When the constructor must allow different representations of the constructed object.

 

Instance:

1. Define a computer builder Interface

package Builder_Pattern;public interface  ComputerBuilder {    public  void buildDisplayer();       public  void buildMainframe();       public  void buildDataline();   public  Computer  buildComputer();   }

2. define a general computer class

 

package Builder_Pattern;public class Computer{private String displayer;        private String mainframe;        private String dataline;     public String getDisplayer() {return displayer;}public void setDisplayer(String displayer) {this.displayer = displayer;}public String getMainframe() {return mainframe;}public void setMainframe(String mainframe) {this.mainframe = mainframe;}public String getDataline() {return dataline;}public void setDataline(String dataline) {this.dataline = dataline;}}


 

 

3. Define a supervisor to manage the person who builds the computer

 

package Builder_Pattern;public class  ComputerDirector {public  Computer   constructComputer(ComputerBuilder  cb){           cb.buildDisplayer();    cb.buildMainframe();    cb.buildDataline();    return  cb.buildComputer();    }}

4. Define a PC and inherit the general computer class. Or you can define a large computer.

package Builder_Pattern;public class  PC  extends Computer{           } 


 

5. Define a constructor dedicated to building a PC

 

Package builder_pattern; public class pcbuilder implements computerbuilder {private computer; Public pcbuilder () {computer = new PC () ;}@ overridepublic computer buildcomputer () {return computer ;} @ overridepublic void builddataline () {computer. setdataline ("build PC Data Cable") ;}@ overridepublic void builddisplayer () {computer. setdisplayer ("Building a personal computer display") ;}@ overridepublic void buildmainframe () {computer. setmainframe ("Building a PC host ");}}

6. Test this mode.

Package builder_pattern; public class test {public static void main (string [] ARGs) {// instantiate a computer builder computerdirector = new computerdirector (); computer = computerdirector. constructcomputer (New pcbuilder (); system. out. println (computer. getdisplayer (); system. out. println (computer. getmainframe (); system. out. println (computer. getdataline ());}}

7. Thank you for browsing. Thank you for choosing axe.

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.