设计模式(2) Builder模式

(1) 意图:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示

(2) 适用性:
同时满足以下情况的时候可以使用Builder模式
    a. 当创建复杂对象的算法应该独立于该对象的组成部分以及他们的装配方式

    b. 当构造过程必须允许构造的对象有不同的表示

 Builder模式的核心思想

    将一个“复杂对象的构建算法”与它的“部件及组装方式”分离,使得构件算法和组装方式可以独立应对变化;复用同样的构建算法可以创建不同的表示,不同的构建过程可以复用相同的部件组装方式

    抽象的Builder类,为导向者可能要求创建的每一个构件(Part)定义一个操作(接口)。这些操作缺省情况下什么都不做。一个ConcreteBuilder类对它所感兴趣的构建重定义这些操作。每个ConcreteBuilder包含了创建和装配一个特定产品的所有代码(注意:ConcreteBuilder只是提供了使用部件装配产品的操作接口,但不提供具体的装配算法,装配算法在导向器[Director]中定义)。这些代码只需要写一次;然后不同的Director可以复用它,以在相同部件集合的基础上构建不同的Product。

例子:

#include<iostream>
#include <string>
#include <iomanip>

using namespace std;

//产品分为Title,Content,Footer 三部分

class Frame {
public:
	Frame() {
		cout << "Frame" << endl;
	}
};
class Title :public Frame {
public:
	Title() {
		cout << "Title" << endl;
	}
};


class Content :public Frame{
public :
	Content() {
		cout << "Content" << endl;
	}
};

class Footer :public Frame {
public:
	Footer() {
		cout << "Footer" << endl;
	}
};

class Page :public Frame {
public:
	int _titleNum;
	int _contentNum;
	int _footerNum;
public:
	Page() {
		_titleNum = _contentNum = _footerNum = 0;
	}
	void AddTitle(Frame* title) {
		_titleNum++;
	}
	void AddContent(Frame* content) {
		_contentNum++;
	}
	void AddFooter(Frame* footer) {
		_footerNum++;
	}
};


/*Build*/

class Build {
protected:
	Build() = default;  //属性为Protect 使之无法实例化
public:
	virtual void BuildTitle() {};
	virtual void BuildContent() {};
	virtual void BuildFooter() {};

	virtual void BuildPage() {};
	virtual Page* GetPage() { return 0; };
			
};

class StanderBuiler :public Build {
private:
	Page* _page;
public:
	StanderBuiler() {
		_page = nullptr;
	}
	virtual void BuildPage() override {
		_page = new Page();
	}
	virtual void BuildTitle() override {
		Title* title = new Title();
		_page->AddTitle(title);
	}
	virtual void BuildContent() override {
		Content* content = new Content();
		_page->AddContent(content);
	}
	virtual void BuildFooter() override {
		Footer* footer = new Footer();
		_page->AddFooter(footer);
	}

	virtual Page* GetPage() {
		return _page;
	}
};


/*
class AnotherBuilder :pulic Build {
	....................
};
*/


/*Directer*/
class Directer {
public:
	Page* CreatePage(Build* build) {
		build->BuildPage();
		build->BuildTitle();
		build->BuildTitle();
		build->BuildContent();
		build->BuildFooter();
		return build->GetPage();
	}
	/*
	Page* CreateAnotherPage(Build* build){
	...................
	}
	*/
};


/*Client*/

void ShowPage(Page* page) {
	cout << endl;
	cout << "TitleNum: " << page->_titleNum << endl;
	cout << "ContentNum: " << page->_contentNum << endl;
	cout << "FooterNum: " << page->_footerNum << endl;
}
int main() {
	Directer* dir = new Directer();
	Page* page= dir->CreatePage(new StanderBuiler());
	ShowPage(page);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值