Builder模式也叫建造者模式或者生成器模式,是由GoF提出的23种设计模式中的一种。Builder模式是一种对象创建模式之一,用来隐藏复合对象的创建过程。他把复合对象的创建过程加以抽象,通过子类继承和重载的方式,动态地创建具有复合属性的对象。
对象的创建:Builder模式是为了对象的创建而设计的模式-创建的是一个复合对象,被创建的对象为一个具有复合属性的复合对象,关注对象创建的各个部分的创建过程,不同工厂(Builder)对产品属性有不同的创建方法。
ConcreteBuilder创建该产品的内部表示并定义它的装配过程,包含定义组成部分的类,包含将这些部件装配成最终产品的接口。
适用情况:
一个对象的构建比较复杂,将一个对象的构建和对象的表示进行分离。
说白了:建造者模式,其实就是相当于一个设计师,指挥建造师造房子,建造师可能是不同的,因为每个建造师建造水平和会造的房子都是不一样的。
Factory模式中:
实现了购买者和生产线的隔离,强调的是结果
Builder模式
这样做,目的是为了实现复杂对象生产线和其部件的解耦。强调的是过程
两者的区别在于以下几种情况:
工厂模式不考虑对象的组装过程,而直接生成一个我想要的对象。
Builder模式先一个个的创建对象的每一个部件,再统一组装成一个对象
工厂模式所解决的问题是,工厂生产产品
而Builder模式解决的是工厂控制产品 生成器组装各个部件的过程,然后从产品生成器中得到产品。
Director.h
#pragma once
#include "IBuilder.h"
class Director
{
public:
Director(); //不带参数构造
Director(IBuilder *builder); //带参数构造
~Director();
void create(IBuilder* builedr);
void create();
private:
IBuilder * m_builder;
};
Ibuilder.h
#pragma once
#include "Phone.h"
class IBuilder
{
public:
IBuilder();
~IBuilder();
protected:
Phone* phone;
public:
virtual void build_camera() = 0;
virtual void build_battery() = 0;
virtual void build_screen() = 0;
virtual Phone* get_phone();
};
class XiaomiBuilder :public IBuilder
{
public:
XiaomiBuilder();
void build_camera();
void build_battery();
void build_screen();
};
class HuaweiBuilder :public IBuilder
{
public:
HuaweiBuilder();
void build_camera();
void build_battery();
void build_screen();
};
Phone.h
#pragma once
#include
#include
using namespace std;
class Phone
{
public:
Phone();
~Phone();
void set_camera(string camera);
void set_screen(string screen);
void set_battery(string battery);
void display_camera();
void display_battery();
void display_screen();
private:
string m_camera;
string m_screen;
string m_battery;
};
Director.cpp
#include "Director.h"
Director::Director()
{
}
Director::Director(IBuilder *builder)
:m_builder(builder)
{
}
Director::~Director()
{
}
void Director::create(IBuilder* builedr)
{
builedr->build_screen();
builedr->build_battery();
builedr->build_camera();
}
void Director::create()
{
m_builder->build_screen();
m_builder->build_battery();
m_builder->build_camera();
}
IBuilder.cpp
#include "IBuilder.h"
IBuilder::IBuilder()
{
}
IBuilder::~IBuilder()
{
}
Phone* IBuilder::get_phone()
{
return phone;
}
//xiaomiBuilder
XiaomiBuilder::XiaomiBuilder()
{
phone = new Phone;
}
void XiaomiBuilder::build_camera()
{
phone->set_camera("xiaomi camera created!");
}
void XiaomiBuilder::build_screen()
{
phone->set_screen("xiaomi screen created!");
}
void XiaomiBuilder::build_battery()
{
phone->set_battery("xiaomi battery created!");
}
//HuaweiBuilder
HuaweiBuilder::HuaweiBuilder()
{
phone = new Phone;
}
void HuaweiBuilder::build_camera()
{
phone->set_camera("huawei camera created!");
}
void HuaweiBuilder::build_screen()
{
phone->set_screen("huawei screen created!");
}
void HuaweiBuilder::build_battery()
{
phone->set_battery("huawei battery created!");
}
main.cpp
#include "Phone.h"
#include "IBuilder.h"
#include "Director.h"
using namespace std;
#ifndef SAFE_DELETE
#define SAFE_DELETE(p){if(p){delete (p);(p)=NULL;}}
#endif
int main()
{
IBuilder* xiaomi = new XiaomiBuilder;
IBuilder* huawei = new HuaweiBuilder;
Director* director = new Director;
director->create(xiaomi);
director->create(huawei);
Phone* xiaomi_phone = xiaomi->get_phone();
Phone* huawei_phone = huawei->get_phone();
xiaomi_phone->display_camera();
xiaomi_phone->display_battery();
xiaomi_phone->display_screen();
huawei_phone->display_camera();
huawei_phone->display_battery();
huawei_phone->display_screen();
SAFE_DELETE(xiaomi);
SAFE_DELETE(xiaomi_phone);
SAFE_DELETE(huawei);
SAFE_DELETE(huawei_phone);
SAFE_DELETE(director);
return 0;
}
Phone.cpp
#include "Phone.h"
Phone::Phone()
{
}
Phone::~Phone()
{
}
void Phone::set_camera(string camera)
{
m_camera = camera;
}
void Phone::set_battery(string battery)
{
m_battery = battery;
}
void Phone::set_screen(string screen)
{
m_screen = screen;
}
void Phone::display_camera()
{
cout << m_camera.c_str() << endl;
}
void Phone::display_battery()
{
cout << m_battery.c_str() << endl;
}
void Phone::display_screen()
{
cout << m_screen.c_str() << endl;
}
参考:
https://blog.csdn.net/andrewgithub/article/details/109088947
https://www.cnblogs.com/HPAHPA/p/10285425.html