建立型模式 生成器 builder

2021-07-28 09:40:02 字數 4408 閱讀 3315

將乙個複雜物件的構建與它的表示分離,使得同樣的構建過程可以建立不同的表示.

main.cc:

/*

design_patterns:"builder"

in the production of mobile phones, for example, now the mobile phone manufacturers will launch high-end, midrange,

low-end configuration version, the basic configuration of most of them, only a few key parts are different, it is very suitable

for the use of builder mode

high-end :835 processor,5.5 inch panel,8g memory.

midrange :820 processor,4.7 inch panel,4g memory.

low-end :810 processor,4.0 inch panel,2g memory.

*/#include

"builder.h"

#include

"high_phone_builder.h"

#include

"normal_phone_builder.h"

#include

"low_phone_builder.h"

#include

"director.h"

#include

"phone.h"

#include

#include

void main()

director:

#ifndef helendp_source_director_h_

#define helendp_source_director_h_

#include "phone.h"

#include "builder.h"

class

director;

#endif

#include "director.h"

#include "builder.h"

director::director()

director::~director()

phone *director::assemblephone()

void director::setbuilder(builder *builder)

builder:

#ifndef helendp_source_builder_h_

#define helendp_source_builder_h_

#include

#include "phone.h"

using

namespace

std;

class builder;

#endif

#include "builder.h"

#include

using

namespace

std;

builder::builder()

builder::~builder()

highphonebuilder:

//high_phone_builder.h

#ifndef helendp_source_high_phone_builder_h_

#define helendp_source_high_phone_builder_h_

#include "builder.h"

#include "phone.h"

class

highphonebuilder:public

builder;

#endif

//high_phone_builder.cc

#include "high_phone_builder.h"

highphonebuilder::highphonebuilder()

highphonebuilder::~highphonebuilder()

void highphonebuilder::assemblestepone()

void highphonebuilder::assemblesteptwo()

void highphonebuilder::assemblestepthree()

phone* highphonebuilder::getphone()

lowphonebuilder:

//low_phone_builder.h

#ifndef helendp_source_low_phone_builder_h_

#define helendp_source_low_phone_builder_h_

#include "builder.h"

#include "phone.h"

class

lowphonebuilder:public

builder;

#endif

//low_phone_builder.cc

#include "low_phone_builder.h"

lowphonebuilder::lowphonebuilder()

lowphonebuilder::~lowphonebuilder()

void lowphonebuilder::assemblestepone()

void lowphonebuilder::assemblesteptwo()

void lowphonebuilder::assemblestepthree()

phone* lowphonebuilder::getphone()

normalphonebuilder:

//normal_phone_builder.h

#ifndef helendp_source_normal_phone_builder_h_

#define helendp_source_normal_phone_builder_h_

#include "builder.h"

#include "phone.h"

class

normalphonebuilder:public

builder;

#endif

//normal_phone_builder.cc

#include "normal_phone_builder.h"

normalphonebuilder::normalphonebuilder()

normalphonebuilder::~normalphonebuilder()

void normalphonebuilder::assemblestepone()

void normalphonebuilder::assemblesteptwo()

void normalphonebuilder::assemblestepthree()

phone* normalphonebuilder::getphone()

phone:

#ifndef helendp_source_phone_h_

#define helendp_source_phone_h_

#include

using

namespace

std;

/*mobile phone includes processor, memory, panel and other basic configuration

*/class phone;

#endif

#include "phone.h"

#include

using

namespace

std;

phone::phone()

phone::~phone()

void phone::assembleprocessor(string processor)

void phone::assemblememory(string memory)

void phone::assemblepanel(string panel)

void phone::showconfig()

**和uml圖(ea)工程檔案,最後會整理打包上傳.

Builder(生成器) 物件建立型模式

將乙個複雜物件的構建與它的表示分離,使得同樣的構建過程可以建立不同的表示。使得生成器可以隱藏這個產品的表示和內部結構。同時也隱藏了該產品是如何裝配的。因為產品是通過抽象介面構造的,在改變該產品的內部表示時所要做的只是定義乙個新的生成器。將構造 和表示 分開,builder模式通過封裝乙個複雜物件的建...

4 Builder 生成器(建立型模式)

動機 motivation 在軟體系統中,有時候面臨著 乙個複雜物件 的建立工作,其通常由各個部分的子物件用一定的演算法構成 由於需求 的變化,這個複雜物件的各個部分經常面臨著劇烈的變 化,但是將它們組合在一起的演算法卻相對穩定。如何應對這種變化?如何提供一種 封裝機制 來隔離出 復 雜物件的各個部...

生成器模式 Builder

把複雜的物件的構建與其表示分離開,以便根據程式的需要在相同的建立過程中建立不同的表示。每個生成器必須有乙個相同的方法名稱。client 建立乙個 director 物件,指定乙個 build 物件,配置 director。當 product 需要生成時,director 通知該builder bui...