設計模式 簡單工廠模式

2021-10-18 16:02:04 字數 2385 閱讀 5001

1.軟體設計模式,稱為設計模式

2.**設計的總結經驗,穩定,拓展性更強。

3.一系列程式設計的思想,**更容易被他人理解,保證**可靠性,程式的重用性

4.有23種,參考資料:設計模式

5.設計模式通常描述了一組相互緊密作用的類與物件

類:使用者自定義的資料型別,也稱類型別(結構體)

物件:類的具象(具體物件),也就是結構體變數

1.工廠模式是最常用的設計模式之一

2.這種型別的設計模式屬於建立型模式,它提供了一種建立物件的(最佳)方式

3.建立物件時不會對客戶端暴露建立邏輯(不在main檔案暴露),並且是通過使用同乙個介面(鍊錶)來指向新建立的物件

物件(變數)以檔案的形式存放在工廠,新增變數只需要新增檔案

每個檔案建立乙個結構體變數,初始化變數,新增到鍊錶功能函式

main檔案,初始化

初始化:做鍊錶

使用:遍歷鍊錶,找物件,實現功能

dag.c

#

include

"animal.h"

void

dogeat()

void

dogbeat()

struct

animal dog =

;struct

animal

*putdoginlink

(struct

animal

*phead)

else

}

cat.c

#

include

"animal.h"

void

cateat()

void

catbeat()

struct

animal cat =

;struct

animal

*putcatinlink

(struct

animal

*phead)

else

}

main.c

#

include

"animal.h"

#include

struct

animal

*findutilbyname

(char

*str,

struct

animal

*phead)

else

tmp = tmp->next;

}return

null;}

}int

main()

;struct

animal

*ptmp;

struct

animal

*phead =

null

; phead =

putcatinlink

(phead)

; phead =

putdoginlink

(phead)

;while(1

)memset

(anname,

'\0'

,sizeof

(anname));

}return0;

}

animal.h

#

include

struct

animal

;struct

animal

*putdoginlink

(struct

animal

*phead)

;struct

animal

*putcatinlink

(struct

animal

*phead)

;

class fruit

public fruit

(string name)

}void

grup()

(string name)

}class peach extends fruit

public peach

(string name)

}class factory

else

if(name ==

"梨")

else}}

public class test

}

師承上官可程式設計 —— 陳立臣

設計模式 工廠模式(簡單工廠)

一 簡單工廠 定義 簡單工廠模式 factory pattern 屬於類的創新型模式,又叫靜態工廠方法模式 static factorymethod pattern 是通過專門定義乙個類來負責建立其他類的例項,被建立的例項通常都具有共同的父類。特點 工廠類直接實現,乙個產品介面,乙個工廠類可以產生多...

設計模式(簡單工廠模式 工廠模式 抽象工廠模式)

當邏輯較為簡單時,可以直接建立對應的類。如下 include using namespace std class class banana class pear intmain 通過此 可以發現,使用者直接與客戶接觸,違背了dip 依賴倒轉 原則,過於麻煩,所以引出簡單工廠模式。include us...

設計模式 簡單工廠設計模式

請用任意一種物件導向語言實現計算器控制台程式,要求輸入兩個數和運算符號,得到結果。operation運算類 public class operation set public double numberb set public virtual double getresult 加減乘除類 using...