設計模式 構造者模式

2021-06-28 21:57:43 字數 1083 閱讀 2989

原始碼 from: 萬方資料 朱進 *****

構造者模式使用場景:

#include 

#define buide_c

#define finished 1

#define unfinished 0

typedef

struct splane plane;

typedef

struct sconcretebuild concrete_build;

/*function poniter*/

typedef

int (*build_engine)(plane*);

typedef

int (*build_airframe)(plane*);

typedef

void (*construct)(concrete_build*);

/*plane struct*/

struct splane;

struct sconcretebuild;

typedef

struct sdirectordirector;

int mbuild_engine(plane *iplane)

int mbuild_airframe(plane *iplane)

/*assemble plane*/

void mconstruct(concrete_build *ibuild)

static director idirector = ,

mbuild_engine,

mbuild_airframe},

mconstruct,

};int main(int argc, char *argv)

director用於組裝部件,以形成成品

concrete_build 用於生產部件

結構體設計思想:

1. 首先定義飛機需要的部件 struct plane

2. 依據飛機的結構,構造各個部件

3. 依據各個部件構造完整的飛機

設計模式(二) 構造者模式彙總

簡單工廠模式結構圖 簡單工廠模式包括這幾個角色 prouduct 抽象產品 所有具體產品的基類,符合依賴反轉原則 concreteproduct 具體產品 factory 產品工廠 對外提供產品的入口 工廠方法模式結構如圖 包括如下角色 product 抽象產品 concreateproduct 具...

構造者模式

構造者模式是指乙個由多個簡單的物件一起構造為乙個複雜的物件,使得在物件的建立時可以由乙個相同的流程創造出多個不同的物件進行使用 以汽車為例 暫時將汽車的組成分為 鋼材 發動機 玻璃和座椅 當我們構建不同品牌的汽車時只需要輸入該汽車的組成部分的相關標識即可完成對該品牌汽車的組裝 同時也也可以獲取多個汽...

構造者Builder模式

根據物件的屬性行為建立例項,通常有幾種方法 1 通過帶有引數的建構函式 同時函式過載 來滿足根據不同屬性 必選屬性,可選屬性 建立物件 2 通過無參建構函式 或者有參建構函式 再配合屬性的getter,setter方法靈活地設定物件的屬性 3 通過builder模式建立物件 分析幾種方式的特點 方式...