设计模式 之美 -- 建造者模式
生活随笔
收集整理的這篇文章主要介紹了
设计模式 之美 -- 建造者模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1. 解決問題
- 2. 應用場景
- 3. 實現
- C語言實現
- C++實現
- 4. 缺點
1. 解決問題
描述如下場景:
- 類的數據成員很多(8個以上),當我們進行初始化的時候放在初始化列表中,影響類的可讀性,同時校驗初始化參數列表是否有效的邏輯也無處放置
- 假如初始化列表中的參數有一定的依賴關系,比如數據成員max 一定比 數據成員min 小才行,這樣的依賴關系設置邏輯無處安放(需要在類實例化的時候就要完成依賴關系的創建)
- 類的對象是不可變的,也就是對象在創建好之后就不能再修改其內部的屬性值。此時,我們不能對外暴露類的set成員函數。
針對以上三種關于 類實例化過程中遇到 的問題,我們可以使用建造者模式 解決。
2. 應用場景
- 一種類的設計較為復雜,需要通過設置不同的可選錯參數,定制化地創建不同的對象
建造者模式和工廠模式的區別:
工廠模式:創建擁有相關性的不同對象(繼承同一父類或者接口的子類),由給定的參數來決定創建哪一種類的對象
建造者模式:創建一種類型的復雜對象,通過設置不同的可選參數,“定制化”地創建不同的對象
舉例:
顧客走進餐館點餐,我們使用工廠模式 根據用戶的不同選擇,制作不同的食物:漢堡,沙拉,披薩等。
對于披薩來說,用戶可以有不同的配料選擇:奶酪,西紅柿,起司。此時我們通過建造者模式根據用戶選擇的不同配料制作披薩。
3. 實現
C語言實現
實現功能:
建造者模式實現簡單的電腦組裝,通過配置不同的config來組裝出不同用戶要求的電腦(穩定性高、性能高)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>typedef struct PERSONAL_COMPUTER {char cpu[20];char memory[20];char harddisk[20];
}Personal_Computer;void assemble_intel_cpu(Personal_Computer* personal_computer)
{strncpy(personal_computer->cpu, "inter",sizeof("inter"));return;
}
void assemble_amd_cpu(Personal_Computer* personal_computer)
{strncpy(personal_computer->cpu, "amd", sizeof("amd"));return;
}
void assemble_samsung_memory(Personal_Computer* personal_computer)
{strncpy(personal_computer->memory,"samsung", sizeof("samsung"));return;
}
void assemble_kingston_memory(Personal_Computer* personal_computer)
{strncpy(personal_computer->memory, "kingston", sizeof("kingston"));return;
}
void assemble_hitachi_harddisk(Personal_Computer* personal_computer)
{strncpy(personal_computer->harddisk, "hitachi", sizeof("hitachi"));return;
}
void assemble_digital_harddisk(Personal_Computer* personal_computer)
{strncpy(personal_computer->harddisk, "digital", sizeof("digital"));return;
}struct PERSONAL_COMPUTER* builder_fast_config()
{struct PERSONAL_COMPUTER *Personal_Computer = NULL;Personal_Computer = (struct PERSONAL_COMPUTER*)malloc(sizeof(struct PERSONAL_COMPUTER));if (Personal_Computer == NULL) {return NULL;}assemble_intel_cpu(Personal_Computer);assemble_hitachi_harddisk(Personal_Computer);assemble_kingston_memory(Personal_Computer);return Personal_Computer;
}struct PERSONAL_COMPUTER* builder_safty_config()
{struct PERSONAL_COMPUTER* personal_computer = NULL;personal_computer = (struct PERSONAL_COMPUTER*)malloc(sizeof(struct PERSONAL_COMPUTER));if (personal_computer == NULL) {return NULL;}assemble_amd_cpu(personal_computer);assemble_digital_harddisk(personal_computer);assemble_samsung_memory(personal_computer);return personal_computer;
}int main()
{struct PERSONAL_COMPUTER* my_personal_computer = NULL;printf("safy style is :\n");my_personal_computer = builder_safty_config();printf("cpu: %s\n", my_personal_computer->cpu);printf("memory: %s\n", my_personal_computer->memory);printf("harddisk: %s\n", my_personal_computer->harddisk);printf("fast style is :\n");my_personal_computer = builder_fast_config();printf("cpu: %s\n", my_personal_computer->cpu);printf("memory: %s\n", my_personal_computer->memory);printf("harddisk: %s\n", my_personal_computer->harddisk);if (my_personal_computer != NULL) {free(my_personal_computer);}return 0;
}
輸出如下:
safy style is :
cpu: amd
memory: samsung
harddisk: digital
fast style is :
cpu: inter
memory: kingston
harddisk: hitachi
C++實現
實現功能:
建造者負責根據product 類的初始化成員列表,構造不同需求的product對象,同時滿足product類的成員有效性的校驗。
#include <iostream>
#include <string>using namespace std;class Product{
private:string name;int max;int min;int total;/*構造函數聲明為private,可以保證該類實例化無法再外部進行,只能由類內部自己進行*/Product(Builder builder) { (*this).name = builder.name;(*this).max = builder.max;(*this).min = builder.min;(*this).total = builder.total;}public:string getName() {return (*this).name;}int getMax() {return (*this).max;}int getMin() {return (*this).min;}int getTotal() {return (*this).total;}/*builder類設計成了Product的內部類,這里也可以設計Builer類為獨立的非內部類*/static class Builder {private:const static int DEFAULT_MAX = 8;const static int DEFAULT_TOTAL = 8;const static int DEFAULT_MIN = 0;string name;int max = DEFAULT_MAX;int total = DEFAULT_TOTAL;int min = DEFAULT_MIN;public:/*這里進行Product類初始化成員的校驗邏輯,包括必填校驗項、依賴關系校驗、約束條件等*/Product build(){if(name==""){throw "name is null!";}if(max > total) {throw "max bigger than total!"}if(max < min) {throw "max less than min!"}return new Product((*this));}/*設置名字*/Builder setName(string name) {if(name == "") {throw "name is null, illegal!";}(*this).name = name;return (*this);}/*設置最大值*/Builder setMax(int max) {if(max < 0) {throw "max is less than zero, illegal!";}(*this).max = max;return (*this);} /*設置總值*/Builder setTotal(int name) {if(total <= 0) {throw "total is less than zero, illegal!";}(*this).total = total;return (*this);}/*設置最小值*/Builder setMin(int min) {if(min < 0) {throw "min is less than zero, illegal!";}(*this).min = min;return (*this);}};
};int main(){Product product = new Product.Builder().setName("productA").setMax(16).setTotal(2).setMin(0).build();cout << "After product construct:"<< "name:" << product.getName() << endl<< "total:" << product.getTotal() << endl<< "max:" << product.getMax() << endl<< "min:" << product.getMin() << endl;return 0;
}
4. 缺點
-
工廠模式:創建擁有相關性的不同對象(繼承同一父類或者接口的子類),由給定的參數來決定創建哪一種類的對象
-
建造者模式:創建一種類型的復雜對象,通過設置不同的可選參數,“定制化”地創建不同的對象。
綜上,建造者模式僅僅是為了適配不同需求的對象,并不負責之后對象的處理;工廠模式則主要是針對接口開發,通過創建擁有相關性的不同對象來進行不同的操作。
總結
以上是生活随笔為你收集整理的设计模式 之美 -- 建造者模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 编程模式 之美 -- 抽象工厂模式
- 下一篇: 有了女性不孕要如何解决