C語言實現《設計模式之禪》之建造者模式

2021-06-20 05:01:28 字數 4458 閱讀 5145

#include 

#include

#include

/*建造者模式示例,詳細說明及類圖參見設計模式之禪 p106*/

/*行為順序陣列*/

#define sequence_size 4

#define sequence_stringlen 15

static

unsigned

char

aucsequence

[sequence_size

][sequence_stringlen]=

; /*aucsequence 資料內容形如:

aucsequence[4][15] = */

/*抽象產品類 (抽象車模)*/

typedef

struct

_carmodel

carmodel

; struct

_carmodel ;

/*抽象車模的run方法實現*/

void

run(

carmodel

*pcarmodel)

elseif(

0==strcmp

(ptmp

,"stop"))

elseif(

0==strcmp

(ptmp

,"alarm"))

elseif(

0==strcmp

(ptmp

,"engineboom"))

else

ptmp

+=sequence_stringlen; }

}/*抽象車模的setsequence方法實現*/

void

setsequence

(unsigned

char

*pparam1

,unsigned

char

*pparam2

, unsigned

char

*pparam3

,unsigned

char

*pparam4)

void

deletecarmodel

(carmodel

*pcarmodel)

} carmodel

*createcarmodel

(void)

memset

(pcarmodel,0

,sizeof

(carmodel

));

pcarmodel

->

psequence

=aucsequence

; pcarmodel

->

deletecarmodel

=deletecarmodel

; pcarmodel

->

run=

run;

pcarmodel

->

setsequence

=setsequence;

return

pcarmodel; }

/*具體產品類 (繼承抽象產品類)*/

/*benz車模*/

typedef

struct

_benzmodel

benzmodel

; struct

_benzmodel ;

void

benzalarm

(void)

void

benzengineboom

(void)

void

benzstart

(void)

void

benzstop

(void)

benzmodel

*createbenzmodel

(void)

pbenzmodel

->

carmodel

.alarm

=benzalarm

; pbenzmodel

->

carmodel

.engineboom

=benzengineboom

; pbenzmodel

->

carmodel

.start

=benzstart

; pbenzmodel

->

carmodel

.stop

=benzstop;

return

pbenzmodel; }

/*bwm車模*/

/*與 benz車模類似,**略*/

/*抽象的組裝者*/

typedef

struct

_carbuilder

carbuilder

; struct

_carbuilder ;

/*具體組裝者*/

/*benz組裝者*/

typedef

struct

_benzbuilder

benzbuilder

; struct

_benzbuilder ;

void

deletebenzbuiler

(benzbuilder

*pbenzbuilder)

} carmodel

*getbenzmodel

(carbuilder

*pcarbuilder)

benzbuilder

*createbenzbuilder

(void)

memset

(pbenzbuilder,0

,sizeof

(benzbuilder

)); if(

null

==pbenzmodel)

pbenzbuilder

->

pbenzmodel

=pbenzmodel

; pbenzbuilder

->

deletebenzbuiler

=deletebenzbuiler

; pbenzbuilder

->

carbuilder

.setsequence

=setsequence

; pbenzbuilder

->

carbuilder

.getcarmodel

=getbenzmodel;

return

pbenzbuilder; }

/*導演類*/

typedef

struct

_director

director

; struct

_director ;

carmodel

*getabenzmodel

(director

*pdirector)

carmodel

*getbbenzmodel

(director

*pdirector)

void

deletedirector

(director

*pdirector)

} director

*createdirector

(void)

memset

(pdirector,0

,sizeof

(director

));

pdirector

->

pbenzbuilder

=createbenzbuilder

(); if

(null

==pdirector

->

pbenzbuilder)

pdirector

->

getabenzmodel

=getabenzmodel

; pdirector

->

getbbenzmodel

=getbbenzmodel

; pdirector

->

deletedirector

=deletedirector;

return

pdirector; }

/*場景*/

void

main()

/*建造n輛a類benz*/

for(i

=0;i

<10;

i++)

/*建造n輛b類benz*/

for(i

=0;i

<10;

i++)

pdirector

->

deletedirector

(pdirector);

}

C語言實現《設計模式之禪》之模板模式

include include include c語言實現設計模式之禪 模板模式 類圖詳見p95 define false 0 define true 1 typedef int boolean 抽象模板類 typedef struct hummermodel hummermodel struct ...

設計模式之建造者模式(C 實現)

建造者模式 將乙個複雜物件的構建與它的表示分離,使得同樣的構建過程可以建立不同的表示。這是建造者模式的標準表達,不過看著讓人迷惑,什麼叫構建和表示的分離?乙個物件使用建構函式構造之後不就固定了,只有通過它方法來改變它的屬性嗎?而且還要同樣的構建過程搞出不同的表示,怎麼可能呢?多寫幾個建構函式?其實多...

C 設計模式之 建造者模式

將乙個複雜物件的構建與它的表示分離,使得同樣的構建過程可以建立不同的表示 它主要用於建立一些複雜物件,這些物件內部構建間的建造順序通常是穩定的,但物件內部的構建通常面臨著複雜的變化。它使得建造 與表示 分離,由於建造者隱藏了該產品是如何組裝的,所以若需要改變乙個產品的內部表示,只需要再定義乙個具體的...