Builder mode of Design Pattern

Source: Internet
Author: User

The builder mode is also called the generator mode. It is an object creation mode used to hide the process of creating a composite object. It abstracts the process of creating a composite object and uses subclass inheritance or overloading, dynamically create objects with composite attributes.


Application scenarios:

-Object creation: the builder mode is designed for object creation.

-Create a composite object: The created object is a composite object with composite attributes.

-Follow the creation process of each part of an object: different factories (Builder here) have different methods to create product properties.


Composite attribute object (that is, the object to be created ):

public class House {private String floor;private String wall;private String proof;public String getFloor() {return floor;}public void setFloor(String floor) {this.floor = floor;}public String getWall() {return wall;}public void setWall(String wall) {this.wall = wall;}public String getProof() {return proof;}public void setProof(String proof) {this.proof = proof;}}

Specific Creator interface or abstract class:

public interface HouseBuilder {public void makeFloor();public void makeWall();public void makeProof();public House getHouse();}

Specific creator implementation Class 1:

public class ApartmentBuilder implements HouseBuilder {private House house = new House();@Overridepublic House getHouse() {return house;}@Overridepublic void makeFloor() {house.setFloor("Apartment --> Floor");}@Overridepublic void makeWall() {house.setWall("Apartment --> Wall");}@Overridepublic void makeProof() {house.setProof("Apartment --> Proof");}}


Specific creator implementation Class 2:

public class VillaBuilder implements HouseBuilder {private House house = new House();@Overridepublic void makeFloor() {house.setFloor("Villa --> Floor");}@Overridepublic void makeWall() {house.setWall("Villa --> Wall");}@Overridepublic void makeProof() {house.setProof("Villa --> Proof");}@Overridepublic House getHouse() {return house;}}

Designers:

public class HouseDirector {public void makeHouse(HouseBuilder builder) {builder.makeFloor();builder.makeProof();builder.makeWall();}}

Client call class:

public class ClientApp {public static void main(String[] args) {//HouseBuilder builder = new ApartmentBuilder();HouseBuilder builder = new VillaBuilder();HouseDirector director = new HouseDirector();director.makeHouse(builder);House house = builder.getHouse();System.out.println(house.getFloor());System.out.println(house.getProof());System.out.println(house.getWall());}}




Builder mode of Design Pattern

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.