Fundamentals 6 min read

Understanding the Builder Design Pattern with Java Examples

The Builder pattern separates object construction from its representation, enabling the same construction process to produce varied complex objects, and is illustrated through a Java house-building example that defines abstract and concrete builders, a product class, a director, and client code.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Understanding the Builder Design Pattern with Java Examples

Builder pattern is a creational design pattern that separates the construction of a complex object from its representation, allowing the same construction process to create different representations.

The pattern involves four main roles: Builder (abstract builder), Concrete Builder, Product, and Director.

In the example, a house is used as the product with attributes such as foundation, floor, wall, and roof. The abstract builder defines methods to build each part, and concrete builders (e.g., VillaHouseBuilder and CommonHouseBuilder) implement these steps with specific behavior.

package com.sample.patterns.builder;
// 房屋,具有地基、地板等属性
public class House {
    private String foundation;
    private String floor;
    private String wall;
    private String roof;
    // 省略getter和setter
}
package com.sample.patterns.builder;
// 抽象建造者,定义了产品创建的步骤
public interface HouseBuilder {
    void buildFoundation();
    void buildRoof();
    void buildWall();
    void buildFloor();
    House getResult();
}
package com.sample.patterns.builder;
// 别墅的修建方法
public class VillaHouseBuilder implements HouseBuilder {
    private House house = new House();
    @Override
    public void buildFoundation() {
        System.out.println("打造别墅的地基");
    }
    @Override
    public void buildRoof() {
        System.out.println("修建别墅的屋顶");
    }
    @Override
    public void buildWall() {
        System.out.println("修建别墅的墙壁");
    }
    @Override
    public void buildFloor() {
        System.out.println("铺设别墅的地板");
    }
    @Override
    public House getResult() {
        return house;
    }
}
package com.sample.patterns.builder;
// 房屋建造的指挥者
public class HouseDirector {
    public House construct(HouseBuilder houseBuilder) {
        houseBuilder.buildFoundation();
        houseBuilder.buildFloor();
        houseBuilder.buildWall();
        houseBuilder.buildRoof();
        return houseBuilder.getResult();
    }
}
package com.sample.patterns.builder;
// 客户端
public class Client {
    public static void main(String[] args) {
        HouseDirector houseDirector = new HouseDirector();
        HouseBuilder villaBuilder = new VillaHouseBuilder();
        House villa = houseDirector.construct(villaBuilder);
        HouseBuilder commonBuilder = new CommonHouseBuilder();
        House common = houseDirector.construct(commonBuilder);
    }
}

The Director controls the construction sequence, invoking the builder methods in a fixed order and returning the finished product.

This pattern is widely used in JDK source code, Spring framework, and other libraries such as StringBuilder and BeanDefinitionBuilder, and is useful when a class has many constructor parameters that need flexible configuration.

Javasoftware architectureObject Creationdesign patternBuilder Pattern
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.