• 设计模式:建造者模式(C#、JAVA、JavaScript、C++、Python、Go、PHP)


    上一篇《策略模式》                                                       下一篇《适配器模式》

    简介:

    建造者模式,它是一种对象构建模式,它提供了一种构建对象的最佳方式。这种模式适用于当对象的构建过程需要涉及到多个部分,并且这些部分在构造过程中可以逐步完善。
    建造者模式将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。这种模式将对象的创建过程抽象化,通过将对象的创建与它的表示分离,使得同样的构建过程可以创建不同的表示。

    建造者模式主要涉及四个角色:
    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#建造者模式

    下面是一个使用C#实现建造者模式的示例:

    1. // 产品类  
    2. public class Car  
    3. {  
    4.     public string Name { get; set; }  
    5.     public int Seats { get; set; }  
    6.     public string Transmission { get; set; }  
    7.     public string Engine { get; set; }  
    8.   
    9.     public override string ToString()  
    10.     {  
    11.         return $"{Name} ({Seats} seats, {Transmission}, {Engine})";  
    12.     }  
    13. }  
    14.   
    15. // 抽象建造者类  
    16. public abstract class CarBuilder  
    17. {  
    18.     public abstract void SetBody();  
    19.     public abstract void SetWindows();  
    20.     public abstract void SetDoors();  
    21.     public abstract Car GetCar();  
    22. }  
    23.   
    24. // 具体建造者类1  
    25. public class CarBuilderA : CarBuilder  
    26. {  
    27.     private Car _car = new Car();  
    28.   
    29.     public override void SetBody()  
    30.     {  
    31.         _car.Name = "CarA";  
    32.         _car.Seats = 4;  
    33.         _car.Transmission = "Automatic";  
    34.         _car.Engine = "Petrol";  
    35.     }  
    36.   
    37.     public override void SetWindows()  
    38.     {  
    39.         // Add windows to the car body  
    40.     }  
    41.   
    42.     public override void SetDoors()  
    43.     {  
    44.         // Add doors to the car body  
    45.     }  
    46.   
    47.     public override Car GetCar()  
    48.     {  
    49.         return _car;  
    50.     }  
    51. }  
    52.   
    53. // 具体建造者类2  
    54. public class CarBuilderB : CarBuilder  
    55. {  
    56.     private Car _car = new Car();  
    57.   
    58.     public override void SetBody()  
    59.     {  
    60.         _car.Name = "CarB";  
    61.         _car.Seats = 2;  
    62.         _car.Transmission = "Manual";  
    63.         _car.Engine = "Electric";  
    64.     }  
    65.   
    66.     public override void SetWindows()  
    67.     {  
    68.         // Add windows to the car body  
    69.     }  
    70.   
    71.     public override void SetDoors()  
    72.     {  
    73.         // Add doors to the car body  
    74.     }  
    75.   
    76.     public override Car GetCar()  
    77.     {  
    78.         return _car;  
    79.     }  
    80. }

    二、java建造者模式

    建造者模式通常通过以下方式实现:

    1. // 产品类  
    2. public class Car {  
    3.     private String name;  
    4.     private int seats;  
    5.     private String transmission;  
    6.     private String engine;  
    7.   
    8.     public Car(String name, int seats, String transmission, String engine) {  
    9.         this.name = name;  
    10.         this.seats = seats;  
    11.         this.transmission = transmission;  
    12.         this.engine = engine;  
    13.     }  
    14.   
    15.     public String getName() {  
    16.         return name;  
    17.     }  
    18.   
    19.     public int getSeats() {  
    20.         return seats;  
    21.     }  
    22.   
    23.     public String getTransmission() {  
    24.         return transmission;  
    25.     }  
    26.   
    27.     public String getEngine() {  
    28.         return engine;  
    29.     }  
    30. }  
    31.   
    32. // 抽象建造者类  
    33. public abstract class CarBuilder {  
    34.     public abstract void setBody();  
    35.     public abstract void setWindows();  
    36.     public abstract void setDoors();  
    37.     public abstract Car getCar();  
    38. }  
    39.   
    40. // 具体建造者类1  
    41. public class CarBuilderA extends CarBuilder {  
    42.     private Car car = new Car("CarA", 4, "Automatic", "Petrol");  
    43.   
    44.     @Override  
    45.     public void setBody() {  
    46.         // Add body to the car  
    47.     }  
    48.   
    49.     @Override  
    50.     public void setWindows() {  
    51.         // Add windows to the car body  
    52.     }  
    53.   
    54.     @Override  
    55.     public void setDoors() {  
    56.         // Add doors to the car body  
    57.     }  
    58.   
    59.     @Override  
    60.     public Car getCar() {  
    61.         return car;  
    62.     }  
    63. }  
    64.   
    65. // 具体建造者类2  
    66. public class CarBuilderB extends CarBuilder {  
    67.     private Car car = new Car("CarB", 2, "Manual", "Electric");  
    68.   
    69.     @Override  
    70.     public void setBody() {  
    71.         // Add body to the car  
    72.     }  
    73.   
    74.     @Override  
    75.     public void setWindows() {  
    76.         // Add windows to the car body  
    77.     }  
    78.   
    79.     @Override  
    80.     public void setDoors() {  
    81.         // Add doors to the car body  
    82.     }  
    83.   
    84.     @Override  
    85.     public Car getCar() {  
    86.         return car;  
    87.     }  
    88. }

    三、javascript建造者模式

    在JavaScript中,建造者实现方式如下:

    1. // 产品类  
    2. function Car(name, seats, transmission, engine) {  
    3.   this.name = name;  
    4.   this.seats = seats;  
    5.   this.transmission = transmission;  
    6.   this.engine = engine;  
    7. }  
    8.   
    9. // 抽象建造者类  
    10. function CarBuilder() {}  
    11.   
    12. // 具体建造者类1  
    13. function CarBuilderA() {  
    14.   this.car = new Car();  
    15. }  
    16.   
    17. CarBuilderA.prototype.setBody = function(body) {  
    18.   // Add body to the car  
    19.   this.car.name = body;  
    20. };  
    21.   
    22. CarBuilderA.prototype.setWindows = function(windows) {  
    23.   // Add windows to the car body  
    24.   this.car.seats = windows;  
    25. };  
    26.   
    27. CarBuilderA.prototype.setDoors = function(doors) {  
    28.   // Add doors to the car body  
    29.   this.car.transmission = doors;  
    30. };  
    31.   
    32. CarBuilderA.prototype.getCar = function() {  
    33.   return this.car;  
    34. };  
    35.   
    36. // 具体建造者类2  
    37. function CarBuilderB() {  
    38.   this.car = new Car();  
    39. }  
    40.   
    41. CarBuilderB.prototype.setBody = function(body) {  
    42.   // Add body to the car  
    43.   this.car.name = body;  
    44. };  
    45.   
    46. CarBuilderB.prototype.setWindows = function(windows) {  
    47.   // Add windows to the car body  
    48.   this.car.seats = windows;  
    49. };  
    50.   
    51. CarBuilderB.prototype.setDoors = function(doors) {  
    52.   // Add doors to the car body  
    53.   this.car.transmission = doors;  
    54. };  
    55.   
    56. CarBuilderB.prototype.getCar = function() {  
    57.   return this.car;  
    58. };

    四、C++建造者模式

    以下是在C++中实现建造者模式:

    1. #include  
    2. #include  
    3.   
    4. // 产品类  
    5. class Car {  
    6. public:  
    7.     void setBody(const std::string& body) {  
    8.         m_body = body;  
    9.     }  
    10.   
    11.     void setWindows(const std::string& windows) {  
    12.         m_windows = windows;  
    13.     }  
    14.   
    15.     void setDoors(const std::string& doors) {  
    16.         m_doors = doors;  
    17.     }  
    18.   
    19.     void printCar() const {  
    20.         std::cout << "Car: " << m_body << ", Windows: " << m_windows << ", Doors: " << m_doors << std::endl;  
    21.     }  
    22.   
    23. private:  
    24.     std::string m_body;  
    25.     std::string m_windows;  
    26.     std::string m_doors;  
    27. };  
    28.   
    29. // 抽象建造者类  
    30. class CarBuilder {  
    31. public:  
    32.     virtual ~CarBuilder() {}  
    33.   
    34.     virtual void setBody() = 0;  
    35.     virtual void setWindows() = 0;  
    36.     virtual void setDoors() = 0;  
    37.     virtual Car* getCar() = 0;  
    38. };  
    39.   
    40. // 具体建造者类1  
    41. class CarBuilderA : public CarBuilder {  
    42. public:  
    43.     void setBody() {  
    44.         m_car->setBody("CarA Body");  
    45.     }  
    46.   
    47.     void setWindows() {  
    48.         m_car->setWindows("CarA Windows");  
    49.     }  
    50.   
    51.     void setDoors() {  
    52.         m_car->setDoors("CarA Doors");  
    53.     }  
    54.   
    55.     Car* getCar() {  
    56.         return m_car;  
    57.     }  
    58.   
    59. private:  
    60.     Car* m_car;  
    61. };  
    62.   
    63. // 具体建造者类2  
    64. class CarBuilderB : public CarBuilder {  
    65. public:  
    66.     void setBody() {  
    67.         m_car->setBody("CarB Body");  
    68.     }  
    69.   
    70.     void setWindows() {  
    71.         m_car->setWindows("CarB Windows");  
    72.     }  
    73.   
    74.     void setDoors() {  
    75.         m_car->setDoors("CarB Doors");  
    76.     }  
    77.   
    78.     Car* getCar() {  
    79.         return m_car;  
    80.     }  
    81.   
    82. private:  
    83.     Car* m_car;  
    84. };  
    85.   
    86. // 客户端代码  
    87. int main() {  
    88.     // 使用CarBuilderA构建一辆汽车并输出信息  
    89.     CarBuilderA builderA;  
    90.     builderA.setBody();  
    91.     builderA.setWindows();  
    92.     builderA.setDoors();  
    93.     Car* carA = builderA.getCar();  
    94.     carA->printCar();  
    95. }

    五、python建造者模式

    在Python中,建造者模式通常使用类来构建对象。下面是一个使用Python实现建造者模式的示例:

    1. class Car:  
    2.     def __init__(self):  
    3.         self.body = ""  
    4.         self.windows = ""  
    5.         self.doors = ""  
    6.   
    7.     def set_body(self, body):  
    8.         self.body = body  
    9.   
    10.     def set_windows(self, windows):  
    11.         self.windows = windows  
    12.   
    13.     def set_doors(self, doors):  
    14.         self.doors = doors  
    15.   
    16.     def print_car(self):  
    17.         print("Car: {}, Windows: {}, Doors: {}".format(self.body, self.windows, self.doors))  
    18.   
    19.   
    20. class CarBuilder:  
    21.     def __init__(self):  
    22.         self.car = Car()  
    23.   
    24.     def set_body(self):  
    25.         self.car.set_body("Car Body")  
    26.   
    27.     def set_windows(self):  
    28.         self.car.set_windows("Car Windows")  
    29.   
    30.     def set_doors(self):  
    31.         self.car.set_doors("Car Doors")  
    32.   
    33.     def get_car(self):  
    34.         return self.car  
    35.   
    36.   
    37. # 客户端代码  
    38. if __name__ == "__main__":  
    39.     builder = CarBuilder()  
    40.     builder.set_body()  
    41.     builder.set_windows()  
    42.     builder.set_doors()  
    43.     car = builder.get_car()  
    44.     car.print_car()

    六、go建造者模式

    在Go语言中,建造者模式通常使用结构体和方法来实现。下面是一个使用Go实现建造者模式的示例:

    1. package main  
    2.   
    3. import "fmt"  
    4.   
    5. // Product接口定义产品的行为  
    6. type Product interface {  
    7.  Use()  
    8. }  
    9.   
    10. // ConcreteProduct是Product接口的具体实现  
    11. type ConcreteProduct struct {  
    12.  // 产品的属性  
    13. }  
    14.   
    15. // Use是Product接口的具体实现方法  
    16. func (p *ConcreteProduct) Use() {  
    17.  fmt.Println("使用产品")  
    18. }  
    19.   
    20. // Builder接口定义构建者的行为  
    21. type Builder interface {  
    22.  SetPartA()  
    23.  SetPartB()  
    24.  SetPartC()  
    25.  GetProduct() Product  
    26. }  
    27.   
    28. // ConcreteBuilder是Builder接口的具体实现  
    29. type ConcreteBuilder struct {  
    30.  product *ConcreteProduct  
    31. }  
    32.   
    33. // SetPartA是Builder接口的具体实现方法  
    34. func (b *ConcreteBuilder) SetPartA() {  
    35.  // 构建产品的部分A  
    36. }  
    37.   
    38. // SetPartB是Builder接口的具体实现方法  
    39. func (b *ConcreteBuilder) SetPartB() {  
    40.  // 构建产品的部分B  
    41. }  
    42.   
    43. // SetPartC是Builder接口的具体实现方法  
    44. func (b *ConcreteBuilder) SetPartC() {  
    45.  // 构建产品的部分C  
    46. }  
    47.   
    48. // GetProduct是Builder接口的具体实现方法,返回最终的产品  
    49. func (b *ConcreteBuilder) GetProduct() Product {  
    50.  return b.product  
    51. }  
    52.   
    53. func main() {  
    54.  // 创建具体的构建者对象  
    55.  builder := &ConcreteBuilder{}  
    56.    
    57.  // 使用构建者构建产品对象,并使用产品对象的方法进行操作。  
    58.  builder.SetPartA() 
    59. }

    七、PHP建造者模式

    下面是一个使用PHP实现建造者模式的示例:

    1.  
    2.   
    3. // 产品接口  
    4. interface Product {  
    5.     public function useProduct();  
    6. }  
    7.   
    8. // 具体产品实现  
    9. class ConcreteProduct implements Product {  
    10.     public function useProduct() {  
    11.         echo "使用产品";  
    12.     }  
    13. }  
    14.   
    15. // 建造者接口  
    16. interface Builder {  
    17.     public function setPartA();  
    18.     public function setPartB();  
    19.     public function setPartC();  
    20.     public function getProduct();  
    21. }  
    22.   
    23. // 具体建造者实现  
    24. class ConcreteBuilder implements Builder {  
    25.     private $product;  
    26.       
    27.     public function setPartA() {  
    28.         // 构建产品的部分A,并进行适当的设置(比如组装或配置)工作。  
    29.         // 具体细节对用户来说是不可见的。用户只是调用builder的方法进行设置,而不需要关心具体是如何实现的。这就是封装。  
    30.         // 封装是面向对象编程的三大特性之一,它提供了隐藏对象内部状态并仅通过对象提供的方法来访问的能力。封装可以防止用户直接访问对象内部状态,从而防止修改对象内部状态导致的问题。封装还可以隐藏对象的实现细节,从而提高了代码的安全性和可维护性。  
    31.     }  
    32.       
    33.     public function setPartB() {  
    34.         // 构建产品的部分B,并进行适当的设置(比如组装或配置)工作。  
    35.     }  
    36.       
    37.     public function setPartC() {  
    38.         // 构建产品的部分C,并进行适当的设置(比如组装或配置)工作。  
    39.     }  
    40.       
    41.     public function getProduct() {  
    42.         return $this->product; // 返回最终的产品对象。  
    43.     }  
    44. }  
    45.   
    46. // 使用建造者模式构建对象并使用其方法进行操作。  
    47. $builder = new ConcreteBuilder();  
    48. $builder->setPartA(); 

    《完结》

    上一篇《策略模式》                                                        下一篇《适配器模式》

  • 相关阅读:
    Linux设置密码复杂度
    5. 线性回归的从零开始实现
    NVIDIA 7th SkyHackathon(七)Tao 目标检测模型可视化推理与导出
    Zookeeper初步
    14天机器学习DAY1-5|线性回归原理小结
    ElasticSearch7.3学习(十四)----生产环境实时重建索引
    案例部署——GlusterFS分布式文件系统群集
    30秒让你弄懂pdf怎么翻译,还在犹豫什么
    linux安装ffmpeg支持libx264
    蓝桥杯(1):python排序
  • 原文地址:https://blog.csdn.net/yetyrain/article/details/133978452