// main.h #ifndef MAIN_H typedef struct _Human Human; struct _Human{ void (*GetColor)(void); void (*Talk)(void); }; typedef struct _BlackHuman BlackHuman; struct _BlackHuman{ Human human; void (*GetColorForBlack)(void); void (*TalkForBlack)(void); void (*DeteteForBlack)(BlackHuman*); }; typedef struct _WhiteHuman WhiteHuman; struct _WhiteHuman{ Human human; void (*GetColorForWhite)(void); void (*TalkForWhite)(void); void (*DeleteForWhite)(WhiteHuman*); }; typedef struct _AbstractFactory AbstractFactory; struct _AbstractFactory{ Human* (*CreateHuam)(char* pstring); }; typedef struct _Factory HumanFactory; struct _Factory{ AbstractFactory AbstractFactory; Human* (*CreateHuman)(char* pstring); void (*DeleteHumanFactory)(HumanFactory *pHumanFactory); }; #endif // main.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include "main.h" #ifndef VLD_H #include <vld.h> #endif void GetColorForBlack(void) { printf("human color is black\r\n"); } void TalkForBlack(void) { printf("human talk is engish\r\n"); } void DeleteBlackHuman(BlackHuman* pBlackHuman) { if(NULL != pBlackHuman) { free(pBlackHuman); } return ; } BlackHuman* CreateBlackHuman(void) { BlackHuman* phuman = (BlackHuman*)malloc(sizeof(BlackHuman)); if(NULL == phuman) { return NULL; } phuman->GetColorForBlack = GetColorForBlack; phuman->TalkForBlack = TalkForBlack; phuman->DeteteForBlack = DeleteBlackHuman; phuman->human.GetColor = phuman->GetColorForBlack; phuman->human.Talk=phuman->TalkForBlack; return phuman; } void GetColorForWhite(void) { printf("human color is white\r\n"); } void TalkForWhite(void) { printf("human talk is japan\r\n"); } void DeleteWhiteHuman(WhiteHuman* pWhiteHuman) { if(NULL != pWhiteHuman) { free(pWhiteHuman); } return ; } WhiteHuman* CreateWhiteHuman(void) { WhiteHuman* phuman= (WhiteHuman*)malloc(sizeof(WhiteHuman)); if(NULL == phuman) { return NULL; } phuman->GetColorForWhite=GetColorForWhite; phuman->TalkForWhite= TalkForWhite; phuman->DeleteForWhite = DeleteWhiteHuman; phuman->human.GetColor= phuman->GetColorForWhite; phuman->human.Talk=phuman->TalkForWhite; return phuman; } Human* CreateHuman(char* pstring) { Human *pHuman=NULL; if(0 == strcmp(pstring,"Black")) { pHuman= (Human*)CreateBlackHuman(); } if(0 == strcmp(pstring,"White")) { pHuman = (Human*)CreateWhiteHuman(); } return pHuman; } void DeleteHumanFactory(HumanFactory* pHumanFactory) { if(NULL != pHumanFactory) { free(pHumanFactory); } return ; } HumanFactory* CreateHumanFactory(void) { HumanFactory* pHumanFactory = (HumanFactory*)malloc(sizeof(HumanFactory)); if(NULL == pHumanFactory) { return NULL; } pHumanFactory->CreateHuman = CreateHuman; pHumanFactory->DeleteHumanFactory=DeleteHumanFactory; pHumanFactory->AbstractFactory.CreateHuam = pHumanFactory->CreateHuman; return pHumanFactory; } int main() { AbstractFactory* pFactory = (AbstractFactory*)CreateHumanFactory(); Human* pBlack=NULL; Human* pWhite=NULL; BlackHuman* pBlackDelete=NULL; WhiteHuman* pWhiteDelete=NULL; HumanFactory* pHumanFactory=NULL; pHumanFactory=(HumanFactory*)pFactory; pBlack = pFactory->CreateHuam("Black"); pBlackDelete = (BlackHuman*)pBlack; pBlack->GetColor(); pBlack->Talk(); pBlackDelete->DeteteForBlack(pBlackDelete); pWhite= pFactory->CreateHuam("White"); pWhiteDelete=(WhiteHuman*)pWhite; pWhite->GetColor(); pWhite->Talk(); pWhiteDelete->DeleteForWhite(pWhiteDelete); // free pHumanFactory->DeleteHumanFactory(pHumanFactory); return 0; }
工廠模式的文章有2篇指針
第一篇: 使用一個派生類的指針, 用來保存子類的地址, 建立子類的時候,老是返回父類的地址code
該篇: 使用結構體指針的轉化,建立子類的時候,返回的是子類的地址, 利用轉化,能夠的到父類的地址,內存
區別 : 1 建立子類返回的地址不一樣string
2 使用的內存開銷不一樣it
3 在建立場景的時候, 都使用到告終構體指針的轉化.io