工廠設計模式(C語言版)
現在有個皮包公司,這個公司只要你給他水果名字,他就能告訴你顏色,形狀,生產地....等信息(沒列出來的自己加);對于客戶 來說,我們也不需要

現在有個皮包公司,這個公司只要你給他水果名字,他就能告訴你顏色,形狀,生產地....等信息(沒列出來的自己加);對于客戶 來說,我們也不需要知道他們怎么回去顏色,生產地信息的(是GPT告訴他的,還是花錢問路人的,咱們不管)。只需要告訴工廠,我要知道某某水果某某特性,你告訴我一下。
對于,工廠設計模式理論不是很理解的,可以參考一下鏈接:https://www.runoob.com/design-pattern/factory-pattern.html
廢話不多說,參照上圖,設計出來的工廠模式,示例中為了擴展性,美觀性,做了一些更改。咱們從圖中,從下往上一步一步實現,真實的開發中,也是這思路:首先明確你要什么,蘋果的種植方法,還是蘋果的顏色,或者是你是要蘋果還是香蕉,有了這個目標才一步一步實現的。
首先建立蘋果
//apple.hn#ifndef __APPLE_Hn#define __APPLE_Hn#include "public.h"napi_methord_st apple_methord;nvoid apple_methord_init(void);n#endifnnn//apple.cn#include "apple.h"n/******************apple**************/nnint apple_get_color(void)n{ntprintf("nttapple_get_colorn");ntreturn E_OK;n}nint apple_get_origin(char * data)n{ntif(NULL == data){nttreturn E_err_ptr;nt}ntntprintf("nttapple_get_origin:[%s]n", data);ntreturn E_OK;n}nint apple_get_shape(void)n{ntprintf("nttapple_get_shapen");ntreturn E_OK;n}nnvoid apple_methord_init(void)n{ntapple_methord.get_color = apple_get_color;nntapple_methord.get_origin = apple_get_origin;nntapple_methord.get_shape = apple_get_shape;n}n/***************************************************/
然后建立香蕉
//banana.hn#ifndef __BANANA_Hn#define __BANANA_Hn#include "public.h"napi_methord_st banana_methord;nvoid banana_methord_init(void);n#endifnnn//banana.cn#include "banana.h"n/****************banana*********************/nnint banana_get_color(void)n{ntprintf("nttbanana_get_colorn");ntreturn E_OK;n}nint banana_get_origin(char * data)n{ntif(NULL == data){nttreturn E_err_ptr;nt}ntntprintf("nttbanana_get_origin:[%s]n", data);ntreturn E_OK;n}nint banana_get_shape(void)n{ntprintf("nttbanana_get_shapen");ntreturn E_OK;n}nvoid banana_methord_init(void)n{ntbanana_methord.get_color = banana_get_color;ntntbanana_methord.get_origin = banana_get_origin;nntbanana_methord.get_shape = banana_get_shape;n}n/***************************************************/n
//剩下的西瓜 ,芒果,請自行添加,都是一樣的額。可以看到,咱們后面如果有新的產品,其實都是一樣的方法添加就可以了。
//public.hn#include"stdio.h"n#ifndef __PUBLIC_Hn#define __PUBLIC_Hnenum ErrCode {ntE_OK,ntE_err,ntE_err_ptr,ntE_MAX,n};nn//具體某個子工廠可以做哪些事情ntypedef struct zjiang_fruit_fun_api_methord_stn{ntint (*get_color)(void);ntint (*get_origin)(char* data);ntint (*get_shape)(void);ntint (*get_production_steps)(void);n}api_methord_st;nn#endifnnnn//main.hn#ifndef __MAIN_Hn#define __MAIN_Hn#include "banana.h"n#include "apple.h"nntypedef enum e_fruiType {ntFRUIT_BANANA=0,ntFRUIT_APPLE=1,ntFRUIT_WATERMELON=2,ntFRUIT_MAX,n}fruiType;nntypedef struct s_fruitFactory {ntint type;ntapi_methord_st* method;ntvoid (*init)(void);n}fruitFactory_t;nnfruitFactory_t zjiangFruit[]={nnt//bananant{ntt/*fruit_type*/FRUIT_BANANA,ntt/*method*/&banana_methord,ntt/*init*/banana_methord_init,nt},nnt//applent{ntt/*fruit_type*/FRUIT_APPLE,ntt/*method*/&apple_methord,ntt/*init*/apple_methord_init,nt},nnt//WATERMELONtttttttttttttttttn};n#endifnnnn//main.cn#include "main.h"nint zjiang_get_spec(fruiType fruiType, void** method)n{ntint nFruitSize = 0;ntif (fruiType >= FRUIT_MAX) {nttreturn E_err;nt}ntif (NULL == method) {nttreturn E_err_ptr;nt}nntnFruitSize = (sizeof(zjiangFruit) / sizeof(fruitFactory_t));tntif (fruiType >= nFruitSize){ntt//array subscripts start at 0nttprintf("%s:%d get fruiType:%d spec fail:%d!n", __FUNCTION__, __LINE__, fruiType, nFruitSize);nttreturn E_err;nt}nntzjiangFruit[fruiType].init();nt(*method) = zjiangFruit[fruiType].method;ntreturn E_OK;n}nn//具體方法nint zjiang_get_fruit_color(fruiType fruit_type)n{ntint ret = E_OK;ntvoid* pmethod = NULL;ntapi_methord_st* api_method = NULL;nntret = zjiang_get_spec(fruit_type, &pmethod);ntif (E_OK != ret) {nttprintf("%s:%d get spec fail!n", __FUNCTION__, __LINE__);nttreturn ret;nt}ntapi_method = (api_methord_st*)pmethod;nntif (NULL != api_method){nttif (NULL != api_method->get_color){ntttapi_method->get_color();ntt}nt}ntelse{nttprintf("%s:%d api_method is nulln", __FUNCTION__, __LINE__);nt}nntreturn E_OK;n}nnint zjiang_get_fruit_origin(fruiType fruit_type, char * data)n{ntint ret = E_OK;ntvoid* pmethod = NULL;ntapi_methord_st* api_method = NULL;nntret = zjiang_get_spec(fruit_type, &pmethod);ntif (E_OK != ret) {nttprintf("%s:%d get spec fail!n", __FUNCTION__, __LINE__);nttreturn ret;nt}ntapi_method = (api_methord_st*)pmethod;nntif (NULL != api_method) {nttif (NULL != api_method->get_origin) {ntttapi_method->get_origin(data);ntt}nt}ntelse {nttprintf("%s:%d api_method is nulln", __FUNCTION__, __LINE__);nt}nntreturn E_OK;n}nnint main()n{ntzjiang_get_fruit_color(FRUIT_WATERMELON);ntzjiang_get_fruit_color(FRUIT_BANANA);ntzjiang_get_fruit_color(FRUIT_APPLE);ntzjiang_get_fruit_origin(FRUIT_BANANA, "hangzhou");ntzjiang_get_fruit_origin(FRUIT_APPLE, "jiangnan");nt//zjiang_get_fruit_color(FRUIT_MAX);ntprintf("hello word!n");ntreturn 0;nn}n
以上代碼親測可用。運行結果如下:

完整代碼:
https://github.com/lordZhouJ/factory_design_mode.git下一節:
哈曼哈龜:單例設計模式(C語言版)上一篇:淘工廠
下一篇:大力建設好鄉村加油站







