Table of Contents
Builder Mode
1. What is Builder Mode?
2. Specific implementation of builder mode
Structural diagram
Four roles in builder mode:
java code implementation
3. Advantages and disadvantages of builder mode
Advantages:
Disadvantages:
Note:
Home Java javaTutorial What is Java Builder Pattern? How to achieve? (with code)

What is Java Builder Pattern? How to achieve? (with code)

Sep 20, 2018 pm 02:31 PM
java

This article brings you what is the Java builder pattern? How to achieve? (Attached is the code), which has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Builder Mode

1. What is Builder Mode?

 Builder Pattern uses multiple simple objects to build a complex object step by step.
A Builder class will construct the final object step by step. This Builder class is independent of other objects.
The builder pattern is mainly used in software systems. Sometimes we are faced with the creation of "a complex object", which is usually composed of sub-objects of each part using a certain algorithm; Due to changes in requirements, this complex object The individual parts of an object often face drastic changes, but the algorithms that put them together are relatively stable.

2. Specific implementation of builder mode

Structural diagram

What is Java Builder Pattern? How to achieve? (with code)

Four roles in builder mode:
  1. Builder: Provides an abstract interface to standardize the construction of each component of the product object. This interface specifies which parts of the complex object are to be created, and does not involve the creation of specific object components.

  2. ConcreteBuilder: Implements the Builder interface to concretize the creation of each part of a complex object for different business logic. After the building process is complete, provide examples of the product.

  3. Director: Call specific builders to create various parts of complex objects. The director does not involve specific product information, but is only responsible for ensuring that all parts of the object are completely created or in a certain order. create.

  4. Product: The complex object to be created.

java code implementation

1. Create human entity class

package com.designpattern.builderPattern;

/**
 * 对象 人
 *
 * @author zhongtao on 2018/9/17
 */
public class Human {

    private String head;
    private String body;
    private String hand;
    private String foot;

    public String getHead() {
        return head;
    }

    public void setHead(String head) {
        this.head = head;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getHand() {
        return hand;
    }

    public void setHand(String hand) {
        this.hand = hand;
    }

    public String getFoot() {
        return foot;
    }

    public void setFoot(String foot) {
        this.foot = foot;
    }
}
Copy after login

2. Create human Builder interface

package com.designpattern.builderPattern;

/**
 * 造人接口 规定造人的规范 需要头、身体、手、脚
 *
 * @author zhongtao on 2018/9/17
 */
public interface BuilderHuman {

    void buildHead();

    void buildBody();

    void buildHand();

    void buildFoot();

    /**
     * 返回创建的对象
     */
    Human createHuman();

}
Copy after login

3 , ConcreteBuilder creates different types of people

tallPerson

package com.designpattern.builderPattern;

/**
 * 高个子的人
 *
 * @author zhongtao on 2018/9/17
 */
public class TallPersonBuilder implements BuilderHuman {

    Human human;

    public TallPersonBuilder() {
        human = new Human();
    }

    @Override
    public void buildHead() {
        human.setHead("普通的头脑");
    }

    @Override
    public void buildBody() {
        human.setBody("壮实,高大的身体");
    }

    @Override
    public void buildHand() {
        human.setHand("长手");
    }

    @Override
    public void buildFoot() {
        human.setFoot("长脚");
    }

    @Override
    public Human createHuman() {
        return human;
    }
}
Copy after login

smartHuman

package com.designpattern.builderPattern;

/**
 * 聪明的人
 *
 * @author zhongtao on 2018/9/17
 */
public class SmartHumanBuilder implements BuilderHuman {

    Human human;

    public SmartHumanBuilder() {
        human = new Human();
    }

    @Override
    public void buildHead() {
        human.setHead("高智商的头脑");
    }

    @Override
    public void buildBody() {
        human.setBody("健康的身体");
    }

    @Override
    public void buildHand() {
        human.setHand("普通的手");
    }

    @Override
    public void buildFoot() {
        human.setFoot("普通的脚");
    }

    @Override
    public Human createHuman() {
        return human;
    }
}
Copy after login

4. The core of the Director builder pattern calls specific builders to create different people

package com.designpattern.builderPattern;

/**
 * 管理造人的顺序 BuilderHuman不同,则创建的人不同
 * @author zhongtao on 2018/9/17
 */
public class HumanDirector {

    public Human createHumanByDirector(BuilderHuman builderHuman){
        builderHuman.buildHead();
        builderHuman.buildBody();
        builderHuman.buildHand();
        builderHuman.buildFoot();

        return builderHuman.createHuman();
    }
}
Copy after login

5. Builder mode test

package com.designpattern.builderPattern;

import org.junit.Test;

/**
 * 测试
 *
 * @author zhongtao on 2018/9/17
 */
public class BuilderPatternTest {

    /**
     * 测试建造者模式
     */
    @Test
    public void test() {
        HumanDirector humanDirector = new HumanDirector();
        //创建高个子的人
        Human humanByDirector = humanDirector.createHumanByDirector(new TallPersonBuilder());
        System.out.println(humanByDirector.getHead());
        System.out.println(humanByDirector.getBody());
        System.out.println(humanByDirector.getHand());
        System.out.println(humanByDirector.getFoot());

        System.out.println("------简单的分割线------");
        //创建聪明的人
        Human smartHuman = humanDirector.createHumanByDirector(new SmartHumanBuilder());
        System.out.println(smartHuman.getHead());
        System.out.println(smartHuman.getBody());
        System.out.println(smartHuman.getHand());
        System.out.println(smartHuman.getFoot());
    }
}
Copy after login

3. Advantages and disadvantages of builder mode

Advantages:
  1. Builder is independent and easy to expand .

  2. Easy to control detailed risks.

Disadvantages:
  1. Products must have something in common and the scope is limited.

  2. If the internal changes are complex, there will be many construction classes.

Note:

Difference from factory mode, builder mode pays more attention to the order of parts assembly.

The above is the detailed content of What is Java Builder Pattern? How to achieve? (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

See all articles