Design Mode (5)-builder Mode

Source: Internet
Author: User

Builder mode (builder) can separate parts from their assembly process and create a complex object step by step.
You only need to specify the type of the complex object to obtain the object, without having to know the specific structure details.

Under what circumstances will the builder mode be used?
In my personal understanding, when we create an object, we need logic.
For example, if we want to create a simple pojo object, we can simply create a new object without any logic.
When you want to create a complex object, its attributes are logically related.
The value of an attribute value depends on other attributes. Therefore, the creation of this object is not simply new.
This logic must be clearly written before creation.
The construction mode makes it unnecessary for customers to know too much internal details of the product. It encapsulates the construction and representation of complex objects in
In a specific construction role, the mentor is responsible for coordinating the builder role to obtain a specific product instance.

Its Model:

Abstract Product

Package Org. test. design. build;/*** film, books, and other works * @ author lushuaiyin **/public abstract class product {Public String author; // The author's public int funds = 0; // Cost Public String workname; // work name Public String worktype; // work type Public String content; // Other Information Public String getauthor () {return author ;} public void setauthor (string author) {This. author = author;} public int getfunds () {return funds;} public void setfunds (INT funds) {This. funds = funds;} Public String getworkname () {return workname;} public void setworkname (string workname) {This. workname = workname;} Public String getworktype () {return worktype;} public void setworktype (string worktype) {This. worktype = worktype;} Public String getcontent () {return content;} public void setcontent (string content) {This. content = content;} Public String printstring () {return "product [author =" + author + ", funds =" + funds + ", workname =" + workname + ", worktype = "+ worktype +", content = "+ content +"] ";}}

Movie book

Package Org. test. design. build; public class movie extends product {private string actor; // starring private string Director; // directing public string getactor () {return actor;} public void setactor (string actor) {This. actor = actor;} Public String getdirector () {return Director;} public void setdirector (string Director) {This. director = Director ;}}

package org.test.design.build;public class Book extends Product {private int price=0;public int getPrice() {return price;}public void setPrice(int price) {this.price = price;}}

Abstract Builder

Package Org. test. design. build;/*** logical description of Object Construction * creation steps for files such as movies and books * @ author lushuaiyin **/public interface builder {void recruiting (); // recruit void writescript (); // write the script, book void raisefunds (); // raise funds void propagate (); // publicize void released (); // Publishing, releasing, and releasing product buildproduct ();}

Specific builder moviebuilder bookbuilder

Package Org. test. design. build;/*** specific implementation of movie object construction logic * @ author lushuaiyin **/public class moviebuilder implements builder {public product movie = new movie (); // use the transformation object here/* if the object we create only cares about behavior characteristics, you can use the parent class or interface to declare the object. In this way, other private attributes of the specific object class will not be involved during creation. If we care about these attributes when creating them. Then, the subclass NEW is used to export the object. Movie movie = new movie (); */Public void recruiting () {movie. setauthor ("James Cameron");} public void writescript () {movie. setworkname ("Avatar");} public void raisefunds () {movie. setfunds (200000);} public void propagate () {movie. setcontent ("the first 3D sci-fi masterpiece! ");} Public void released () {movie. setworktype (" 3D movie ");} public product buildproduct () {return movie ;}}

Package Org. test. design. build;/*** specific implementation of book object construction logic * @ author lushuaiyin **/public class bookbuilder implements builder {public product book = New Book (); // here is the transformation object using public void recruiting () {book. setauthor ("J. k. write a novel ");} public void writescript () {book. setworkname ("Harry Potter and the magic stone");} public void raisefunds () {book. setfunds (10000);} public void propagate () {book. setcontent ("the best-selling magic novel! ");} Public void released () {book. setworktype (" novel ");} public product buildproduct () {return book ;}}

Conductor Director

Package Org. test. design. build;/*** conductor role * @ author lushuaiyin **/public class ctor {public product buw.product (Builder) {builder. raisefunds (); // raise funds builder. recruiting (); // recruitment builder. propagate (); // publicize builder. released (); // release, release return builder. buildproduct ();}}

Test:

Package Org. test. design. build; public class testmain {/*** @ Param ARGs */public static void main (string [] ARGs) {Director ctor = new director (); // conductor role // generation of a movie moviebuilder = new moviebuilder (); product movie = Director. bucket product (moviebuilder); system. out. println (movie. printstring (); // the generation of a novel bookbuilder = new bookbuilder (); product book = Director. bucket product (bookbuilder); sys TEM. out. println (book. printstring () ;}}/* print product [author = James Cameron, funds = 200000, workname = NULL, worktype = 3D movie, content = first 3D sci-fi masterpiece!] Product [author = J. K. Based on novels, funds = 10000, workname = NULL, worktype = novels, content = the best-selling magic novels!] */

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.