2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
The Builder Pattern in design patterns is a commonly used object creation design pattern, which is mainly used to solve the problem of building complex objects. The following is a detailed introduction to the Builder Pattern:
The Builder pattern separates the construction of a complex object from its representation, allowing the same construction process to create different representations. In other words, it breaks down the construction process of a complex object into a series of simple steps and allows users to create different objects by specifying the order and parameters of these steps.
Class Diagram
In the builder mode, the following roles are usually included:
The builder pattern is applicable to the following scenarios:
In the following example, we will use the Builder pattern to design a house renovation system. We will define aHouse
The class is a complex object that contains multiple decoration components (such as ceiling, paint, floor, floor tiles, etc.). Then, we define aHouseBuilder
Interface, which contains abstract methods for creating these components. Next, we create specific builder classes for each specific decoration style (such as luxurious European style, light luxury pastoral style, modern simplicity). Finally, we will pass aDirector
class to guide the construction process, but it may not be necessary in this example because we can define the complete construction logic directly in the builder class.
However, to demonstrate the concept of the commander role, we will keep aDirector
Class, but it is only for illustration purposes, in fact the construction process can be done directly in the builder class.
- // 房屋类
- public class House {
- private String ceiling; // 吊顶
- private String paint; // 涂料
- private String floor; // 地板
- private String tiles; // 地砖
-
- // 私有构造函数
- private House() {}
-
- // Getter 方法
- public String getCeiling() {
- return ceiling;
- }
-
- public String getPaint() {
- return paint;
- }
-
- public String getFloor() {
- return floor;
- }
-
- public String getTiles() {
- return tiles;
- }
-
- // 建造者接口
- public interface HouseBuilder {
- HouseBuilder buildCeiling(String ceiling);
- HouseBuilder buildPaint(String paint);
- HouseBuilder buildFloor(String floor);
- HouseBuilder buildTiles(String tiles);
- House build();
- }
-
- // 豪华欧式建造者 ,注意是静态内部类
- public static class LuxuryEuropeanBuilder implements HouseBuilder {
- private House house;
-
- public LuxuryEuropeanBuilder() {
- this.house = new House();
- }
-
- @Override
- public HouseBuilder buildCeiling(String ceiling) {
- house.ceiling = "豪华欧式吊顶: " + ceiling;
- return this;
- }
-
- @Override
- public HouseBuilder buildPaint(String paint) {
- house.paint = "豪华欧式涂料: " + paint;
- return this;
- }
-
- @Override
- public HouseBuilder buildFloor(String floor) {
- house.floor = "豪华欧式地板: " + floor;
- return this;
- }
-
- @Override
- public HouseBuilder buildTiles(String tiles) {
- house.tiles = "豪华欧式地砖: " + tiles;
- return this;
- }
-
- @Override
- public House build() {
- return house;
- }
- }
-
- // ... 可以为其他风格创建类似的建造者类
-
- // 指挥者类(可选,这里主要用于展示概念)
- public static class Director {
- private HouseBuilder builder;
-
- public Director(HouseBuilder builder) {
- this.builder = builder;
- }
-
- // 这里可以添加方法来指导建造过程,但在这个例子中,我们直接在建造者中完成了所有工作
- public House constructHouse() {
- // 假设这是由指挥者指导的步骤,但在这里我们直接返回建造者的结果
- return builder
- .buildCeiling("水晶吊灯")
- .buildPaint("金色镶边涂料")
- .buildFloor("大理石地板")
- .buildTiles("马赛克地砖")
- .build();
- }
- }
-
- // 主函数,用于演示
- public static void main(String[] args) {
- HouseBuilder luxuryBuilder = new LuxuryEuropeanBuilder();
- // Director director = new Director(luxuryBuilder); // 如果使用指挥者
-
- House house = luxuryBuilder
- .buildCeiling("水晶吊灯")
- .buildPaint("金色镶边涂料")
- .buildFloor("大理石地板")
- .buildTiles("马赛克地砖")
- .build();
-
- System.out.println("Ceiling: " + house.getCeiling());
- System.out.println("Paint: " + house.getPaint());
- System.out.println("Floor: " + house.getFloor());
- System.out.println("Tiles: " + house.getTiles());
- }
- }
Note that in this example,Director
The class doesn't really add much value, since all the build logic is already encapsulated in theHouseBuilder
But in more complex applications,Director
Classes can be used to encapsulate the order and logic of the build process, especially when the build process needs to span multiple builders.
From the above introduction, we can see that the builder pattern has great advantages in building complex objects. It improves the encapsulation and extensibility of the code by separating the construction process from the representation, while also reducing the coupling between the client and the specific product.
If this article is helpful for your study, please like and collect it.