文章目录
- 一、建造者模式基础知识
- 建造者模式的结构
- 建造者模式的应用场景
一、建造者模式基础知识
建造者模式(Builder Pattern)是一种创建型设计模式,它提供了一种优雅的方式来创建复杂对象,同时隐藏其构建过程
。这种模式允许你通过指定复杂对象的类型和内容来构建它们,然后分步骤创建复杂对象的各个部分。
建造者模式的结构
建造者模式通常包含以下角色:
产品(Product):需要构建的复杂对象。
建造者(Builder):提供一个接口,用于创建和表示产品的不同部分。
具体建造者(ConcreteBuilder):实现建造者接口,定义产品对象的各个部分的构建和表示。
指挥者(Director):负责安排已有模块的建造过程,不涉及具体的构建细节。
客户端(Client):使用指挥者和建造者来创建复杂对象。
建造者模式的应用场景
创建复杂对象:当对象的构建过程复杂或者对象本身包含多个成员变量时。
相同的方法,不同的执行顺序:不同的执行顺序可能会导致不同的结果。
分步骤构建:需要分步骤构建对象,每一步都可以独立进行。
隔离复杂对象的构建和表示:客户端不需要知道对象是如何构建的,只需要知道如何使用它。
#pragma once#include <iostream>
#include <string.h>using namespace std;class Phone // 电话类
{
public:Phone();~Phone();void setCamera(string camera);void setBattery(string battery);void setScreen(string screen);void PrintCamera();void PrintBattery();void PrintScreen();private:string strCamera; // 摄像头string strBattery; // 电池string strScreen; // 屏幕
};//----------------------#include "Phone.h"Phone::Phone(){cout << "调用Phone类的构造函数." << endl;
}Phone::~Phone() {cout << "调用Phone类的析构函数." << endl;
}void Phone::setCamera(string camera) {strCamera = camera;
}void Phone::setBattery(string battery) {strBattery = battery;
}void Phone::setScreen(string screen)
{strScreen = screen;
}void Phone::PrintCamera() {cout << "Camera-->" << strCamera.c_str() << endl;
}void Phone::PrintBattery() {cout << "Battery-->" << strBattery.c_str() << endl;
}void Phone::PrintScreen() {cout << "Screen-->" << strScreen.c_str() << endl;
}
#pragma once#include "Phone.h"class Fbuilder // 基类
{
public:Fbuilder();~Fbuilder();protected:Phone* phone; // 电话对象public:virtual void buildCamera() = 0;virtual void buildBattery() = 0;virtual void buildScreen() = 0;virtual Phone* GetPhone();};class IphoneBuilder :public Fbuilder { // 苹果派生类
public:IphoneBuilder();void buildCamera();void buildBattery();void buildScreen();
};class HuaweiBuilder :public Fbuilder { // 华为派生类
public:HuaweiBuilder();void buildCamera();void buildBattery();void buildScreen();
};//------------------------------
#include "Fbuilder.h"Fbuilder::Fbuilder(){}Fbuilder::~Fbuilder() {}Phone* Fbuilder::GetPhone()
{return phone;
}IphoneBuilder::IphoneBuilder() {phone = new Phone;
}
void IphoneBuilder::buildCamera() {phone->setCamera("Iphone Camera Created.");
}
void IphoneBuilder::buildBattery() {phone->setBattery("Iphone Battery Created.");
}
void IphoneBuilder::buildScreen() {phone->setScreen("Iphone Screen Created.");
}HuaweiBuilder::HuaweiBuilder() {phone = new Phone;
}
void HuaweiBuilder::buildCamera() {phone->setCamera("Huawei Camera Created.");
}
void HuaweiBuilder::buildBattery() {phone->setBattery("Huawei Battery Created.");
}
void HuaweiBuilder::buildScreen() {phone->setScreen("Huawei Screen Created.");
}
#pragma once#include "Fbuilder.h"class Director {
public:Director();Director(Fbuilder* builder);~Director();void CreateFunc(Fbuilder* builder);void CreateFunc();private:Fbuilder *dbuilder;
};//---------------------------#include "Director.h"Director::Director() {}// 另一种初始化:Director::Director(Fbuilder* builder):dbuilder(builder)
Director::Director(Fbuilder* builder) {dbuilder = builder;
}Director::~Director() {}void Director::CreateFunc(Fbuilder* builder)
{builder->buildCamera();builder->buildBattery();builder->buildScreen();
}void Director::CreateFunc()
{dbuilder->buildCamera();dbuilder->buildBattery();dbuilder->buildScreen();
}
#include <iostream>
using namespace std;#include "Phone.h"
#include "Director.h"
#include "Fbuilder.h"int main()
{Fbuilder* huawei = new HuaweiBuilder;Fbuilder* iphone = new IphoneBuilder;Director* director = new Director;director->CreateFunc(huawei);director->CreateFunc(iphone);Phone* huaweiphone = huawei->GetPhone();Phone* iphonephone = iphone->GetPhone();huaweiphone->PrintCamera();huaweiphone->PrintScreen();huaweiphone->PrintBattery();cout << endl;iphonephone->PrintCamera();iphonephone->PrintScreen();iphonephone->PrintBattery();return 0;
}