首页 > 编程语言 > 详细

两种语言实现设计模式(C++和Java)(八:建造者模式)

时间:2019-05-14 21:30:11      阅读:171      评论:0      收藏:0      [点我收藏+]

参考:http://c.biancheng.net/view/1354.html

建造者(Builder)模式的定义:指将一个复杂对象的构造与它的表示分离,使同样的构建过程可以创建不同的表示,这样的设计模式被称为建造者模式。它是将一个复杂的对象分解为多个简单的对象,然后一步一步构建而成。它将变与不变相分离,即产品的组成部分是不变的,但每一部分是可以灵活选择的。

技术分享图片

建造者模式主要包括以下角色:

1.产品:包含多个组件的复杂对象,各组件由建造者来创建。

2.抽象建造者(builder):一个包含创建产品各个子部件的抽象方法的接口,通常还包含一个返回复杂产品的方法 getResult()。

3.具体建造者ConcreteBuilder,建造者的具体实现,完成复杂产品的各个部件的具体创建方法。

4.指挥者Director:它调用建造者对象中的部件构造与装配方法完成复杂对象的创建,在指挥者中不涉及具体产品的信息。

1.C++代码

  1 #include <iostream>
  2 
  3 using namespace std;
  4 
  5 //产品:车
  6 class Car{
  7 private:
  8     string wheels;
  9     string motor;
 10     string structure;
 11 public:
 12     void setWheels(string _wheels){
 13         this->wheels = _wheels;
 14     }
 15 
 16     void setMotor(string _motor){
 17         this->motor = _motor;
 18     }
 19 
 20     void setStructure(string _structure){
 21         this->structure = _structure;
 22     }
 23 
 24     void print(){
 25         cout<< "Wheel:" << this->wheels << \n
 26             << "Motor:" << this->motor << \n
 27             << "Structure:" << this->structure << \n << endl;
 28     }
 29 };
 30 
 31 //建造者:车厂
 32 class CarFactory{
 33 protected:
 34     Car* product = new Car();
 35 public:
 36     virtual void buildWheels()=0;
 37     virtual void buildMotor()=0;
 38     virtual void buildStructure()=0;
 39     Car* getResult(){
 40         return product;
 41     }
 42 };
 43 
 44 class CarFactoryBmw: public CarFactory
 45 {
 46 public:
 47     void buildWheels(){
 48         product->setWheels("BMW Wheels");
 49     }
 50     void buildMotor(){
 51         product->setMotor("BMW Motor");
 52     }
 53     void buildStructure(){
 54         product->setStructure("BMW Structure");
 55     }
 56 };
 57 
 58 class CarFactoryBenz: public CarFactory
 59 {
 60 public:
 61     void buildWheels(){
 62         product->setWheels("Benz Wheels");
 63     }
 64     void buildMotor(){
 65         product->setMotor("Benz Motor");
 66     }
 67     void buildStructure(){
 68         product->setStructure("Benz Structure");
 69     }
 70 };
 71 
 72 //指挥者:项目经理
 73 class Manager
 74 {
 75 private:
 76     CarFactory* factory;
 77 public:
 78     Manager(CarFactory* _factory) {
 79         factory = _factory;
 80     }
 81 
 82     Car* decorate(){
 83         factory->buildMotor();
 84         factory->buildWheels();
 85         factory->buildStructure();
 86         return factory->getResult();
 87     }
 88 };
 89 
 90 int main()
 91 {
 92     CarFactory* factoryBmw = new CarFactoryBmw();
 93     CarFactory* factoryBenz = new CarFactoryBenz();
 94     Manager *pManagerBmw = new Manager(factoryBmw);
 95     Manager* pManagerBenz = new Manager(factoryBenz);
 96     pManagerBmw->decorate();
 97     Car* carBmw = factoryBmw->getResult();
 98     carBmw->print();
 99     pManagerBenz->decorate();
100     Car* carBenz = factoryBenz->getResult();
101     carBenz->print();
102     return 0;
103 }

输出:

Wheel:BMW Wheels

Motor:BMW Motor

Structure:BMW Structure

 

Wheel:Benz Wheels

Motor:Benz Motor

Structure:Benz Structure

 

 

2.Java代码

  1 public class Car {
  2 
  3     private String wheels;
  4     private String motor;
  5     private String structure;
  6 
  7     public void setWheels(String wheels){
  8         this.wheels = wheels;
  9     }
 10 
 11     public void setMotor(String motor){
 12         this.motor = motor;
 13     }
 14 
 15     public void setStructure(String structure){
 16         this.structure = structure;
 17     }
 18 
 19     public void print(){
 20         System.out.println("Wheels:"+ wheels + ‘\n‘ + "Motor:" + motor + ‘\n‘ + "Structure:" + structure + ‘\n‘);
 21     }
 22 }
 23 
 24 public abstract class CarFactory {
 25 
 26     public CarFactory(){
 27         production = new Car();
 28     }
 29 
 30     protected Car production;
 31 
 32     public abstract void buildWheels();
 33 
 34     public abstract void buildMotor();
 35 
 36     public abstract void buildStructure();
 37 
 38     Car getResult(){
 39         return production;
 40     }
 41 
 42 }
 43 
 44 public class CarFactoryBmw extends CarFactory {
 45 
 46     @Override
 47     public void buildMotor() {
 48         this.production.setMotor("Bmw Motor");
 49     }
 50 
 51     @Override
 52     public void buildWheels() {
 53         this.production.setWheels("Bmw Wheels");
 54     }
 55 
 56     @Override
 57     public void buildStructure() {
 58         this.production.setStructure("Bmw structure");
 59     }
 60 }
 61 
 62 public class CarFactoryBenz extends CarFactory {
 63 
 64     @Override
 65     public void buildMotor() {
 66         this.production.setMotor("Benz Motor");
 67     }
 68 
 69     @Override
 70     public void buildWheels() {
 71         this.production.setWheels("Benz Wheels");
 72     }
 73 
 74     @Override
 75     public void buildStructure() {
 76         this.production.setStructure("Benz structure");
 77     }
 78 }
 79 
 80 public class Manager {
 81 
 82     private CarFactory factory;
 83 
 84     public Manager(CarFactory factory){
 85         this.factory = factory;
 86     }
 87 
 88     public void changeFactory(CarFactory factory){
 89         this.factory = factory;
 90     }
 91 
 92     public Car decorate(){
 93         factory.buildMotor();
 94         factory.buildWheels();
 95         factory.buildStructure();
 96         return factory.getResult();
 97     }
 98 }
 99 
100 public class Main {
101 
102     public static void main(String[] args) {
103         CarFactory factoryBenz = new CarFactoryBenz();
104         CarFactory factoryBmw = new CarFactoryBmw();
105         Manager manager = new Manager(factoryBenz);
106         manager.decorate();
107         factoryBenz.getResult().print();
108         manager.changeFactory(factoryBmw);
109         manager.decorate();
110         factoryBmw.getResult().print();
111     }
112 }

两种语言实现设计模式(C++和Java)(八:建造者模式)

原文:https://www.cnblogs.com/Asp1rant/p/10864478.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!