C 語言 標準工廠模式

// main.h

#ifndef MAIN_H
#define MAIN_H

// parent
typedef struct _Human Human;
struct _Human
{
	void (*GetColor)(void);
	void (*Talk)(void);
	void *DeriveObj;     //派生類的地址.
};


// black
typedef struct _BlackHuman BlackHuman;
struct _BlackHuman
{
	Human *Human;
	void (*GetColorForBlack)(void);
	void (*TalkForBlack)(void);
	void (*DeleteOfBlack)(BlackHuman* human);
};

// white
typedef struct _WhiteHuman WhiteHuman;
struct _WhiteHuman
{
	Human *Human;
	void (*GetColorForWhite)(void);
	void (*TalkForWhite)(void);
	void (*DeleteOfWhite)(WhiteHuman* human);
};

typedef struct _AbstractFactory AbstractFactory;
struct _AbstractFactory{
	Human *(*CreateHuman)(char* pString);
	void *DeriveObj;  //派生類的地址.
};


typedef struct _HumanFactory HumanFactory;

struct _HumanFactory{
	AbstractFactory *AbHumanFactory;
	Human* (*CreateHuman)(char* pString);
	void (*DeleteHumanFactory)(HumanFactory *);
};

#endif


// main.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "main.h"
#ifndef VLD_H
#include <vld.h>
#endif


void GetColorForBlack(void)
{
	printf("human color is black\r\n");	
}

void GetColorForWhite(void)
{
	printf("human color is white\r\n");
}



void TalkForBlack(void)
{
	printf("human talk english\r\n");
}

void TalkForWhite(void)
{
	printf("human talk japan\r\n");
}



void DeleteOfBlack(BlackHuman* human)
{
	if(NULL != human)
	{
		if(NULL != human->Human)
		{
			free(human->Human);
		}
		free(human);
	}
	return ;
}

void DeleteOfWhite(WhiteHuman* human)
{
	if(NULL != human)
	{
		if(NULL != human->Human)
		{
			free(human->Human);
		}
		free(human);
	}
	return ;
}




// 若是是靜態處理, 是不是地址上有所區別,
Human *CreateBlackHuman(void)
{
	BlackHuman *blackhuman = (BlackHuman *)malloc(sizeof(BlackHuman));
	if(NULL == blackhuman)
	{
		return NULL;	
	}else{
		Human *human = (Human*)malloc(sizeof(Human));
		if(NULL == human)
		{
			free(blackhuman);
			return NULL;
		}else{
			//保存派生類的地址,
			human->DeriveObj = blackhuman;
			blackhuman->Human = human;
			blackhuman->GetColorForBlack=GetColorForBlack;
			blackhuman->TalkForBlack= TalkForBlack;
			blackhuman->DeleteOfBlack=DeleteOfBlack;
			blackhuman->Human->GetColor=blackhuman->GetColorForBlack;
			blackhuman->Human->Talk=blackhuman->TalkForBlack;
			return human;
		}
	}
}

Human* CreateWhiteHuman(void)
{
	WhiteHuman *whitehuman = (WhiteHuman *)malloc(sizeof(WhiteHuman));
	if(NULL == whitehuman)
	{
		return NULL;	
	}else{
		Human *human = (Human*)malloc(sizeof(Human));
		if(NULL == human)
		{
			free(whitehuman);
			return NULL;
		}else{
			//保存派生類的地址,
			human->DeriveObj = whitehuman;
			whitehuman->Human = human;
			whitehuman->GetColorForWhite=GetColorForWhite;
			whitehuman->TalkForWhite= TalkForWhite;
			whitehuman->DeleteOfWhite=DeleteOfWhite;
			whitehuman->Human->GetColor=whitehuman->GetColorForWhite;
			whitehuman->Human->Talk=whitehuman->TalkForWhite;
			//返回父類的地址.
			return human;
		}
	}
}




// 指向父類的地址.
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)
	{
		if(pHumanFactory->AbHumanFactory != NULL)
		{
			free(pHumanFactory->AbHumanFactory);
		}
		free(pHumanFactory);
	}
	return ;
}

// 工廠實例
AbstractFactory *CreateHumanFactory(void)
{
	HumanFactory *pHumanFactory = (HumanFactory *)malloc(sizeof(HumanFactory));
	if(NULL == pHumanFactory)
	{
		return NULL;
	}else{
		AbstractFactory *pAbstractFactory= (AbstractFactory *)malloc(sizeof(AbstractFactory));

		if(NULL == pAbstractFactory)
		{
			free(pHumanFactory);
			return NULL;
		}else{
			//保存子類的地址.
			pAbstractFactory->DeriveObj=pHumanFactory;
			pHumanFactory->AbHumanFactory = pAbstractFactory;
			pHumanFactory->CreateHuman= CreateHuman;
			pHumanFactory->DeleteHumanFactory=DeleteHumanFactory;
			pHumanFactory->AbHumanFactory->CreateHuman = pHumanFactory->CreateHuman;
			// 返回父類的地址.
			return pAbstractFactory;
		}
	}
}

int main()
{
	//強制轉化把內容忽略了.
	AbstractFactory *p= CreateHumanFactory();
	Human *pBlack=NULL;
	Human *pWhite=NULL;
	HumanFactory *pHumanFactory=NULL;
	BlackHuman *pBlackHuman= NULL;
	WhiteHuman *pWhiteHuman=NULL;
	pBlack = p->CreateHuman("Black");
	pBlack->GetColor();
	pBlack->Talk();

	pWhite = p->CreateHuman("White");
	pWhite->GetColor();
	pWhite->Talk();

	//
	//BlackHuman *blackhuman=NULL;
	//blackhuman = CreateBlackHuman();

	//if(NULL == blackhuman)
	//{
	//	printf("create blackhuman faile\r\n");
	//	return 0;
	//}

	//blackhuman->GetColorForBlack();
	//blackhuman->TalkForBlack();
	//blackhuman->DeleteOfBlack(blackhuman);

	// convert
	pHumanFactory = (HumanFactory*)p->DeriveObj;
	pHumanFactory->DeleteHumanFactory(pHumanFactory);

	// convert
	pBlackHuman =(BlackHuman*)pBlack->DeriveObj;
	pBlackHuman->DeleteOfBlack(pBlackHuman);

	pWhiteHuman =(WhiteHuman*)pWhite->DeriveObj;
	pWhiteHuman->DeleteOfWhite(pWhiteHuman);

	return 0;
}
相關文章
相關標籤/搜索