建造者模式,它是一种对象构建模式,它提供了一种构建对象的最佳方式。这种模式适用于当对象的构建过程需要涉及到多个部分,并且这些部分在构造过程中可以逐步完善。
建造者模式将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。这种模式将对象的创建过程抽象化,通过将对象的创建与它的表示分离,使得同样的构建过程可以创建不同的表示。
建造者模式主要涉及四个角色:
1、产品角色(Product)是一个具体的产品对象,它包含了产品对象的各个部分。
2、抽象建造者(Builder)为创建一个产品对象的各个部件指定抽象接口,它定义了产品对象的各个部分,并提供了一种构建和装配各个部件的方法。
3、具体建造者(ConcreteBuilder)实现抽象建造者接口,构造和装配产品的各个部件,定义并明确它所创建的表示,并提供一个返回这个产品的接口。
4、指挥者(Director)构建一个使用Builder接口的对象。
使用建造者模式可以隔离客户与对象的生产过程,同时负责控制产品对象的生产过程。这种模式使得用户只需要通过指定复杂对象的类型和内容就可以构建它们,而不需要了解内部具体的构建细节。
总的来说,建造者模式是一种灵活且可维护的模式,它使得对象构建过程更加清晰和易于理解。
建造者模式的使用场景:
1、相同的方法,不同的执行顺序,产生不同的事件结果时,可以采用建造者模式。
2、多个部件或零件,都可以装配到一个对象中,但是产生的运行结果又不相同时,则可以使用建造者模式。
3、产品类非常复杂,或者产品类中调用顺序不同产生了不同的效果时,可以采用建造者模式。
4、在对象创建过程中会使用到系统中的一些其他对象,这些对象在产品对象的创建过程中不易得到时,也可以采用建造者模式。
建造者模式的创建步骤:
1、创建一个指挥者类,该类负责调用建造者类来构建复杂对象。
2、创建一个抽象建造者类,该类定义了复杂对象的各个部件,以及如何构建这些部件。
3、创建一个具体建造者类,该类实现了抽象建造者类中定义的方法,以构建并返回复杂对象。
4、在客户端代码中,使用指挥者类来创建具体建造者类的实例,然后通过指挥者类来调用具体建造者类的方法,以构建复杂对象。
以上是建造者模式的基本创建步骤,您可以根据实际情况进行调整和扩展。
建造者模式的优点,主要包括:
1、将一个复杂对象的建造过程与其表示过程分离,使得同样的构建过程可以创建不同的表示。
2、客户端不必知道产品对象的内部组成,使得构建过程更加灵活。
3、将复杂对象的创建过程分解在不同的方法中,使得创建过程更加清晰,更方便使用程序来控制创建过程。
4、各个具体建造者相互独立,有利于系统的解耦。
5、指挥者类针对抽象建造者编程,增加新的具体建造者无须修改原有类库的代码,系统扩展方便,符合“开闭原则”。
建造者模式的缺点,主要包括:
1、产品的组成部分必须相同,这限制了其使用范围。
2、如果产品的内部变化复杂,可能会导致需要定义很多具体建造者类来实现这种变化,导致系统变得很庞大,增加系统的理解难度和运行成本。

下面是一个使用C#实现建造者模式的示例:
- // 产品类
- public class Car
- {
- public string Name { get; set; }
- public int Seats { get; set; }
- public string Transmission { get; set; }
- public string Engine { get; set; }
-
- public override string ToString()
- {
- return $"{Name} ({Seats} seats, {Transmission}, {Engine})";
- }
- }
-
- // 抽象建造者类
- public abstract class CarBuilder
- {
- public abstract void SetBody();
- public abstract void SetWindows();
- public abstract void SetDoors();
- public abstract Car GetCar();
- }
-
- // 具体建造者类1
- public class CarBuilderA : CarBuilder
- {
- private Car _car = new Car();
-
- public override void SetBody()
- {
- _car.Name = "CarA";
- _car.Seats = 4;
- _car.Transmission = "Automatic";
- _car.Engine = "Petrol";
- }
-
- public override void SetWindows()
- {
- // Add windows to the car body
- }
-
- public override void SetDoors()
- {
- // Add doors to the car body
- }
-
- public override Car GetCar()
- {
- return _car;
- }
- }
-
- // 具体建造者类2
- public class CarBuilderB : CarBuilder
- {
- private Car _car = new Car();
-
- public override void SetBody()
- {
- _car.Name = "CarB";
- _car.Seats = 2;
- _car.Transmission = "Manual";
- _car.Engine = "Electric";
- }
-
- public override void SetWindows()
- {
- // Add windows to the car body
- }
-
- public override void SetDoors()
- {
- // Add doors to the car body
- }
-
- public override Car GetCar()
- {
- return _car;
- }
- }
建造者模式通常通过以下方式实现:
- // 产品类
- public class Car {
- private String name;
- private int seats;
- private String transmission;
- private String engine;
-
- public Car(String name, int seats, String transmission, String engine) {
- this.name = name;
- this.seats = seats;
- this.transmission = transmission;
- this.engine = engine;
- }
-
- public String getName() {
- return name;
- }
-
- public int getSeats() {
- return seats;
- }
-
- public String getTransmission() {
- return transmission;
- }
-
- public String getEngine() {
- return engine;
- }
- }
-
- // 抽象建造者类
- public abstract class CarBuilder {
- public abstract void setBody();
- public abstract void setWindows();
- public abstract void setDoors();
- public abstract Car getCar();
- }
-
- // 具体建造者类1
- public class CarBuilderA extends CarBuilder {
- private Car car = new Car("CarA", 4, "Automatic", "Petrol");
-
- @Override
- public void setBody() {
- // Add body to the car
- }
-
- @Override
- public void setWindows() {
- // Add windows to the car body
- }
-
- @Override
- public void setDoors() {
- // Add doors to the car body
- }
-
- @Override
- public Car getCar() {
- return car;
- }
- }
-
- // 具体建造者类2
- public class CarBuilderB extends CarBuilder {
- private Car car = new Car("CarB", 2, "Manual", "Electric");
-
- @Override
- public void setBody() {
- // Add body to the car
- }
-
- @Override
- public void setWindows() {
- // Add windows to the car body
- }
-
- @Override
- public void setDoors() {
- // Add doors to the car body
- }
-
- @Override
- public Car getCar() {
- return car;
- }
- }
在JavaScript中,建造者实现方式如下:
- // 产品类
- function Car(name, seats, transmission, engine) {
- this.name = name;
- this.seats = seats;
- this.transmission = transmission;
- this.engine = engine;
- }
-
- // 抽象建造者类
- function CarBuilder() {}
-
- // 具体建造者类1
- function CarBuilderA() {
- this.car = new Car();
- }
-
- CarBuilderA.prototype.setBody = function(body) {
- // Add body to the car
- this.car.name = body;
- };
-
- CarBuilderA.prototype.setWindows = function(windows) {
- // Add windows to the car body
- this.car.seats = windows;
- };
-
- CarBuilderA.prototype.setDoors = function(doors) {
- // Add doors to the car body
- this.car.transmission = doors;
- };
-
- CarBuilderA.prototype.getCar = function() {
- return this.car;
- };
-
- // 具体建造者类2
- function CarBuilderB() {
- this.car = new Car();
- }
-
- CarBuilderB.prototype.setBody = function(body) {
- // Add body to the car
- this.car.name = body;
- };
-
- CarBuilderB.prototype.setWindows = function(windows) {
- // Add windows to the car body
- this.car.seats = windows;
- };
-
- CarBuilderB.prototype.setDoors = function(doors) {
- // Add doors to the car body
- this.car.transmission = doors;
- };
-
- CarBuilderB.prototype.getCar = function() {
- return this.car;
- };
以下是在C++中实现建造者模式:
- #include
- #include
-
- // 产品类
- class Car {
- public:
- void setBody(const std::string& body) {
- m_body = body;
- }
-
- void setWindows(const std::string& windows) {
- m_windows = windows;
- }
-
- void setDoors(const std::string& doors) {
- m_doors = doors;
- }
-
- void printCar() const {
- std::cout << "Car: " << m_body << ", Windows: " << m_windows << ", Doors: " << m_doors << std::endl;
- }
-
- private:
- std::string m_body;
- std::string m_windows;
- std::string m_doors;
- };
-
- // 抽象建造者类
- class CarBuilder {
- public:
- virtual ~CarBuilder() {}
-
- virtual void setBody() = 0;
- virtual void setWindows() = 0;
- virtual void setDoors() = 0;
- virtual Car* getCar() = 0;
- };
-
- // 具体建造者类1
- class CarBuilderA : public CarBuilder {
- public:
- void setBody() {
- m_car->setBody("CarA Body");
- }
-
- void setWindows() {
- m_car->setWindows("CarA Windows");
- }
-
- void setDoors() {
- m_car->setDoors("CarA Doors");
- }
-
- Car* getCar() {
- return m_car;
- }
-
- private:
- Car* m_car;
- };
-
- // 具体建造者类2
- class CarBuilderB : public CarBuilder {
- public:
- void setBody() {
- m_car->setBody("CarB Body");
- }
-
- void setWindows() {
- m_car->setWindows("CarB Windows");
- }
-
- void setDoors() {
- m_car->setDoors("CarB Doors");
- }
-
- Car* getCar() {
- return m_car;
- }
-
- private:
- Car* m_car;
- };
-
- // 客户端代码
- int main() {
- // 使用CarBuilderA构建一辆汽车并输出信息
- CarBuilderA builderA;
- builderA.setBody();
- builderA.setWindows();
- builderA.setDoors();
- Car* carA = builderA.getCar();
- carA->printCar();
- }
在Python中,建造者模式通常使用类来构建对象。下面是一个使用Python实现建造者模式的示例:
- class Car:
- def __init__(self):
- self.body = ""
- self.windows = ""
- self.doors = ""
-
- def set_body(self, body):
- self.body = body
-
- def set_windows(self, windows):
- self.windows = windows
-
- def set_doors(self, doors):
- self.doors = doors
-
- def print_car(self):
- print("Car: {}, Windows: {}, Doors: {}".format(self.body, self.windows, self.doors))
-
-
- class CarBuilder:
- def __init__(self):
- self.car = Car()
-
- def set_body(self):
- self.car.set_body("Car Body")
-
- def set_windows(self):
- self.car.set_windows("Car Windows")
-
- def set_doors(self):
- self.car.set_doors("Car Doors")
-
- def get_car(self):
- return self.car
-
-
- # 客户端代码
- if __name__ == "__main__":
- builder = CarBuilder()
- builder.set_body()
- builder.set_windows()
- builder.set_doors()
- car = builder.get_car()
- car.print_car()
在Go语言中,建造者模式通常使用结构体和方法来实现。下面是一个使用Go实现建造者模式的示例:
- package main
-
- import "fmt"
-
- // Product接口定义产品的行为
- type Product interface {
- Use()
- }
-
- // ConcreteProduct是Product接口的具体实现
- type ConcreteProduct struct {
- // 产品的属性
- }
-
- // Use是Product接口的具体实现方法
- func (p *ConcreteProduct) Use() {
- fmt.Println("使用产品")
- }
-
- // Builder接口定义构建者的行为
- type Builder interface {
- SetPartA()
- SetPartB()
- SetPartC()
- GetProduct() Product
- }
-
- // ConcreteBuilder是Builder接口的具体实现
- type ConcreteBuilder struct {
- product *ConcreteProduct
- }
-
- // SetPartA是Builder接口的具体实现方法
- func (b *ConcreteBuilder) SetPartA() {
- // 构建产品的部分A
- }
-
- // SetPartB是Builder接口的具体实现方法
- func (b *ConcreteBuilder) SetPartB() {
- // 构建产品的部分B
- }
-
- // SetPartC是Builder接口的具体实现方法
- func (b *ConcreteBuilder) SetPartC() {
- // 构建产品的部分C
- }
-
- // GetProduct是Builder接口的具体实现方法,返回最终的产品
- func (b *ConcreteBuilder) GetProduct() Product {
- return b.product
- }
-
- func main() {
- // 创建具体的构建者对象
- builder := &ConcreteBuilder{}
-
- // 使用构建者构建产品对象,并使用产品对象的方法进行操作。
- builder.SetPartA()
- }
下面是一个使用PHP实现建造者模式的示例:
-
-
- // 产品接口
- interface Product {
- public function useProduct();
- }
-
- // 具体产品实现
- class ConcreteProduct implements Product {
- public function useProduct() {
- echo "使用产品";
- }
- }
-
- // 建造者接口
- interface Builder {
- public function setPartA();
- public function setPartB();
- public function setPartC();
- public function getProduct();
- }
-
- // 具体建造者实现
- class ConcreteBuilder implements Builder {
- private $product;
-
- public function setPartA() {
- // 构建产品的部分A,并进行适当的设置(比如组装或配置)工作。
- // 具体细节对用户来说是不可见的。用户只是调用builder的方法进行设置,而不需要关心具体是如何实现的。这就是封装。
- // 封装是面向对象编程的三大特性之一,它提供了隐藏对象内部状态并仅通过对象提供的方法来访问的能力。封装可以防止用户直接访问对象内部状态,从而防止修改对象内部状态导致的问题。封装还可以隐藏对象的实现细节,从而提高了代码的安全性和可维护性。
- }
-
- public function setPartB() {
- // 构建产品的部分B,并进行适当的设置(比如组装或配置)工作。
- }
-
- public function setPartC() {
- // 构建产品的部分C,并进行适当的设置(比如组装或配置)工作。
- }
-
- public function getProduct() {
- return $this->product; // 返回最终的产品对象。
- }
- }
-
- // 使用建造者模式构建对象并使用其方法进行操作。
- $builder = new ConcreteBuilder();
- $builder->setPartA();
《完结》