Preview article: Notes on Java design patterns for beginners--BUILDER pattern

 

The builder (BUILDER) mode is also called the generator (BUILDER) mode. Its definition is as follows: (See GoF's design patterns and Yan Hong's "Java and Patterns")

1. Intent:
Separate the construction of a complex object from its representation, so that the same construction process can create different representations.

 

2. Participants:

• Abstract builder (Builder): Specifies abstract interfaces for each component that creates a product (Prodcut) object.
• Concrete Builder (ConcretBuilder):

    Implement the abstract builder (Builder) interface to construct and assemble the various components of the product.
    Define and clarify the representation it creates.
    Provides an interface for retrieving products.

• Director: Constructs an object using the abstract Builder interface.
• Product(Prodcut):

  Represents a constructed complex object. The Concrete Builder (ConcretBuilder) creates an internal representation of the product and defines its assembly process.
  Contains classes that define the component parts, including the interfaces that assemble those parts into the final product.

 

3. Structure:



 

According to the design pattern of GoF, let’s describe the collaborative relationship between the various roles when using the BUILDER pattern:

1. The client creates a Director object and configures it with a ConcretBuilder object of the abstract Builder it wants.
2. When a certain component of the product (PartOne, PartTwo, ...) needs to be created, the Director (Director) will notify the specific builder (ConcretBuilder) .
3. The specific builder (ConcretBuilder) handles the director's request and adds the parts (PartOne, PartTwo, ...) to the product (Prodcut) .
4. The client retrieves the product (Prodcut) from the concrete builder (ConcretBuilder) .

 

===============Interlude================================

When referring to "Basics of Design Patterns for Reusable Object-Oriented Software" (GOF Design Patterns Chinese version), I cannot understand the second description of collaboration in the Builder pattern:

Once the product part is generated, the director notifies the generator. (Check the Chinese Wikipedia, it is also translated this way)

Compared with the original English work, it is described as follows:

Director notifies the builder whenever a part of the product should be built.

 

So, should it be translated into the following meaning?

The director notifies the generator when a component of the product needs (should) be created .

(In other words, the director notifies the generator once the product part needs to be generated . )

  

This may be a little too serious, but I personally feel that the two Chinese expressions of the former and the former should have different meanings.

=====================================================

 

 

Understand the builder pattern based on real-life examples . Let’s use car production as an example. Example 9

The customer wants to buy a FAW-Volkswagen car.

A car is actually a product family, that is, a set (a series) of auto parts products (product family).

The structure of a car is very complex. It has wheels, steering wheel, seats, engine, color, etc.

 

Why does this example look so familiar?

1. It seems to be very similar to the previous article: Notes on Java Design Patterns for Beginners - Abstract Factory Pattern :

Both of them can create complex objects (product collections or product families).

This is what GoF’s design patterns say:

Abstract Factory is similar to Builder in that it can also create complex objects. The main difference is that the Builder pattern focuses on constructing a complex object step by step. Abstract Factory focuses on multiple series of product objects (simple or complex).

 

In other words, the Builder pattern is concerned with how to assemble the various components (or component products) of complex products;

The Abstract Factory model is concerned with which components (or component products) are assembled.

 

2. It seems to be very similar to another previous note about Java design patterns for beginners - Factory Pattern :

Review the advantages of the factory pattern and compare it with the purpose of the builder pattern :

The factory pattern can be used to separate the creation process and use of objects (that is, decoupling).

The builder pattern separates the construction of a complex object from its representation, allowing the same construction process to create different representations.

Personally I think,

The builder pattern focuses more on a regular or orderly creation (construction) process;

The creation (construction) process of the factory pattern is relatively casual.

  

 

In fact, you can also understand their differences from the two names of factory and builder :

Builders are more like production equipment in a factory

The factory only cares about what product it produces, and it only knows what the product is made of;

The builder needs to consider how to produce the product. He needs to consider the rules and methods of product composition.

 

In other words, to a certain extent, the builder pattern is like refining some of the responsibilities of  the factory role in the factory pattern .

The director guides the production of the product , and the builder implements the specific production.

 

Continuing this example to experience the builder pattern:

 

Abstract builder: 

 

/*
 * 建造者 
 */
public interface CarBuilder {
	
	/**
	 * 建造汽车的框架
	 */
	public void buildeCarSkeleton();
	
	/**
	 * 给汽车装上发动机
	 */
	public void buildeCarEngine();
	
	/**
	 * 给汽车装上轮子
	 */
	public void buildeCarWheels();
	
	/**
	 * 给汽车装上导航设备
	 */
	public void buildeCarNavigation();
	
	/**
	 * 安装车身(包含车门、车窗的安装,涂颜色)
	 */
	public void buildeCarBody();

}

 

Specific builder: (Produces Audi A6)

  

Director: 

Here, the director directs the builder to produce the car in a certain way ( with a navigation system installed )

 

/*
 * 导演者
 */
public class AudiDirector {
	
	CarBuilder builder;

	/**
	 * 为导演者配置一个建造者
	 */
	public AudiDirector(CarBuilder builder){
		this.builder = builder;
		
	}

	/**
	 * 按照一定的方式或规则建造汽车
	 */
	public void contruct(){
		
		builder.buildeCarSkeleton();
		
		builder.buildeCarEngine();
		
		builder.buildeCarNavigation();
		
		builder.buildeCarWheels();
		
		builder.buildeCarBody();
		
	}
}

 

Audi cars (products): 

 

/*
 * 产品
 */
public class AudiCar {
	
	private String carSkeleton;
	private Engine carEngine;
	private String carWheels;
	private String carNavigation = "";
	
	private String carDoor;
	private String carWindscreen;
	private String carColor;
	
	public AudiCar(){	}
	
	/**
	 * 汽车的颜色
	 */
	public void setCarColor(String carColor) {
		this.carColor = carColor;
	}
	/**
	 * 汽车的车门
	 */
	public void setCarDoor(String carDoor) {
		this.carDoor = carDoor;
	}
	/**
	 * 汽车发动机,发动机在这里是一个对象
	 */
	public void setCarEngine(Engine carEngine) {
		this.carEngine = carEngine;
	}
	/**
	 * 汽车的框架
	 */
	public void setCarSkeleton(String carSkeleton) {
		this.carSkeleton = carSkeleton;
	}
	/**
	 * 汽车轮子
	 */
	public void setCarWheels(String carWheels) {
		this.carWheels = carWheels;
	}
	/**
	 * 汽车的挡风玻璃
	 */
	public void setCarWindscreen(String carWindscreen) {
		this.carWindscreen = carWindscreen;
	}
	/**
	 * 汽车导航
	 */
	public void setNavigation(String navigation) {
		this.carNavigation = navigation;
	}
	

	/**
	 * 一个汽车对象的描述
	 */
	public String toString(){
		return carSkeleton+ "," + carEngine.toString() +"," + carWheels + "," +
		carDoor + "," + carWindscreen + "," + carColor + "," + carNavigation;
	}
}

 

A component object of the car:

 

/*
 * 一个部件对象(汽车发动机)
 */
public class Engine {
	
	private String name;
	
	public Engine(String name){
		this.name = name;
	}
	
	public String toString(){
		return this.name;
	}

}

 

Client:

 

/*
 * 客户端
 */
public class Clients {

	public static void main(String[] args) {
		
//		创建一个建造者对象
		AudiCarBuilder builder = new AudiCarBuilder();
		
//		创建一个导演者,并为它配置一个建造者
		AudiDirector director1 = new AudiDirector(builder);
		
//		导演者将通知建造者去创建产品
//		在这个过程中,建造者将会根据导演者的请求,去创建组织并创建产品对象
		director1.contruct();
		
//		从具体建造者中检索产品(返回的是带导航的汽车产品)。
		AudiCar audiCar = builder.retrieveCar();
		
		System.out.println(audiCar);	
	}
}

 

operation result:

汽车框架,奥迪A6发动机,车轮,车门,挡风玻璃,黑色,汽车导航

 

If you want to produce an Audi A6 car without a navigation system, how to achieve it?

Just need to modify the director.

New Director: (instructs builder not to install navigation system )

 

/*
 * 导演者
 */
public class AudiDirector {
	
	CarBuilder builder;

	/**
	 * 为导演者配置一个建造者
	 */
	public AudiDirector(CarBuilder builder){
		this.builder = builder;
		
	}

	/**
	 * 按照一定的方式或规则建造汽车
	 */
	public void contruct(){
		
		builder.buildeCarSkeleton();
		
		builder.buildeCarEngine();
		
//		不安装导航系统
//		builder.buildeCarNavigation();
		
		builder.buildeCarWheels();
		
		builder.buildeCarBody();
		
	}
}

 

Running results: ( without navigation system )

 

  

汽车框架,奥迪A6发动机,车轮,车门,挡风玻璃,黑色,

 

 

 

 If you want to produce an Audi A8 car with a navigation system, how to achieve it?

(Assuming that the only difference between the Audi A6 and the Audi A8 is the engine)

Just modify the builder class.

 

Builder: ( just change own internal implementation - change Audi A6 engine to Audi A8 engine)

 

/*
 * 具体建造者
 */
public class AudiCarBuilder implements CarBuilder {
	
	private AudiCar audiCar = new AudiCar();

	/**
	 * 安装车身(包含车门、车窗的安装,涂颜色)
	 */
	public void buildeCarBody() {
		audiCar.setCarDoor("车门");
		audiCar.setCarWindscreen("挡风玻璃");
		audiCar.setCarColor("黑色");
	}

	/**
	 * 给汽车装上发动机
	 */
	public void buildeCarEngine() {
		Engine audiEngine = new Engine("奥迪A8发动机");
		audiCar.setCarEngine(audiEngine);
	}

	/**
	 * 给汽车装上导航设备
	 */
	public void buildeCarNavigation() {
		audiCar.setNavigation("汽车导航");
	}

	/**
	 * 建造汽车的框架
	 */
	public void buildeCarSkeleton() {
		audiCar.setCarSkeleton("汽车框架");
	}

	/**
	 * 给汽车装上轮子
	 */
	public void buildeCarWheels() {
		audiCar.setCarWheels("车轮");
	}
	
	public AudiCar retrieveCar(){
		return audiCar;
	}

}

 

 

Running results: ( replaced with Audi A8 engine )

汽车框架,奥迪A8发动机,车轮,车门,挡风玻璃,黑色,汽车导航

  

 

From this example we can see the advantages of the builder pattern:

1. The director can specify different production methods (whether to install navigation), and the builder can get different products without making any changes.

2. The director does not need to change the desired production method. The builder only needs to change his internal implementation (what type of engine to choose), and he can also get different products.

  

For more information about the effects of builder mode, please refer to GoF's "Design Pattern".

/*
 * 具体建造者
 */
public class AudiCarBuilder implements CarBuilder {
	
	private AudiCar audiCar = new AudiCar();

	/**
	 * 安装车身(包含车门、车窗的安装,涂颜色)
	 */
	public void buildeCarBody() {
		audiCar.setCarDoor("车门");
		audiCar.setCarWindscreen("挡风玻璃");
		audiCar.setCarColor("黑色");
	}

	/**
	 * 给汽车装上发动机
	 */
	public void buildeCarEngine() {
		Engine audiEngine = new Engine("奥迪A6发动机");
		audiCar.setCarEngine(audiEngine);
	}

	/**
	 * 给汽车装上导航设备
	 */
	public void buildeCarNavigation() {
		audiCar.setNavigation("汽车导航");
	}

	/**
	 * 建造汽车的框架
	 */
	public void buildeCarSkeleton() {
		audiCar.setCarSkeleton("汽车框架");
	}

	/**
	 * 给汽车装上轮子
	 */
	public void buildeCarWheels() {
		audiCar.setCarWheels("车轮");
	}
	
	public AudiCar retrieveCar(){
		return audiCar;
	}

}

 

Guess you like

Origin blog.csdn.net/louis_lee7812/article/details/83772087