Design pattern (builder)

Source: Internet
Author: User

The builder pattern is used to create relatively complex, composed objects. It includes several types of roles:

    • Product: The object to be created, in this case the table
    • Builder: Create an abstraction of an object method, in this case Tablebuilder
    • ConcreteBuilder: The implementation of a specific creation, in this case Tablebuilderimpl
    • Director: Package Builder interface

The class diagram relationships are as follows:

The code is as follows:

    • Table
 Packagecom.example; Public classTable {PrivateString Head; PrivateString content; PrivateString footer;  PublicString GetHead () {returnHead; }     Public voidSethead (String head) { This. Head =Head; }     PublicString getcontent () {returncontent; }     Public voidsetcontent (String content) { This. Content =content; }     PublicString Getfooter () {returnfooter; }     Public voidsetfooter (String footer) { This. Footer =footer; }         PublicString getTable () {return  This. Head + "_" + This. Content + "_" + This. Footer; }}
    • Tablebuilder
 Public Interface Tablebuilder {    public  tablebuilder settablehead (String head);      Public tablebuilder settablecontent (String content);      Public tablebuilder settablefooter (String footer);      Public Table Builder ();}
    • Tablebuilderimpl
 Packagecom.example; Public classTablebuilderimplImplementsTablebuilder {Privatetable table; Private Static FinalString TYPE = "TYPE1";  PublicTablebuilderimpl () {table=NewTable (); } @Override PublicTablebuilder Settablehead (String head) {table.sethead (TYPE+head); return  This; } @Override Publictablebuilder settablecontent (String content) {table.setcontent (TYPE+content); return  This; } @Override Publictablebuilder settablefooter (String footer) {table.setfooter (TYPE+footer); return  This; } @Override PublicTable Builder () {returntable; }}
    • Director
 Public class Director {    private  tablebuilder builder;          Public Director (Tablebuilder builder) {        this. Builder = builder;    }           Public Table Builder () {        return builder.settablehead ("Head")                . Settablecontent ( "Content")                . Settablefooter ("Footer")                . Builder ();    }}
    • APP
 Public class App {    publicstaticvoid  main (string[] args) {        new Director (new  Tablebuilderimpl ());         = Direct. Builder ();        System.out.println (Table.gettable ());}    }

Design pattern (builder)

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.