工厂方法模式C语言实现

【说明】工厂方法模式的C语言实现,参考http://blog.csdn.net/sx_wpc/article/details/7645062一文的代码。

【代码清单】

typedef.h

#ifndef __TYPEDEF_H__
#define __TYPEDEF_H__

#include <stdio.h>
#include <stdlib.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef enum _Ret
{
	RET_OK,
	RET_FAIL
}Ret;

#define return_if_fail(p)\
	if(!(p)){\
	printf("%s:%d Warning:"#p"Failed\n",__func__,__LINE__);\
	return;}
#define return_val_if_fail(p, ret)\
	if(!(p)){\
	printf("%s:%d Warning:"#p"Failed\n",__func__,__LINE__);\
	return (ret);}
#define SAFE_FREE(p) if(p != NULL){free(p); p = NULL;}

#ifdef __cplusplus
}
#endif

#endif


factory.h

#ifndef __FACTORY_H__
#define __FACTORY_H__

#include "typedef.h"
#include "product.h"

#ifdef __cplusplus
extern "C" {
#endif

struct _Factory;
typedef struct _Factory Factory;

typedef Operation* (*CreateOperFunc)();
typedef void (*FactoryDestroyFunc)(Factory *thiz);

struct _Factory
{
	CreateOperFunc create_oper;
	FactoryDestroyFunc destroy;
};

static inline Operation* factory_create_oper(Factory *thiz)
{
	return_val_if_fail(thiz != NULL, NULL);
	
	if(thiz->create_oper != NULL)
	{
		return thiz->create_oper();
	}
}

static inline void factory_destroy(Factory *thiz)
{
	return_if_fail(thiz != NULL);
	if(thiz->destroy != NULL)
	{
		thiz->destroy(thiz);
	}
}

#ifdef __cplusplus
}
#endif

#endif


factorya.h

#ifndef __FACTORYA_H__
#define __FACTORYA_H__

#include "product.h"

struct _AddFactory;
typedef struct _AddFactory AddFactory;

typedef Operation* (*CreateAddOperFunc)();
typedef void (*AddFactoryDestroyFunc)(AddFactory *thiz);

struct _AddFactory
{
	CreateAddOperFunc create_oper;
	AddFactoryDestroyFunc destroy;
};

AddFactory *AddFactoryCreate(void);
Operation* add_factory_create();
void add_factory_destroy(AddFactory *thiz);

#endif


factorya.c

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "factorya.h"
#include "producta.h"

Operation* add_factory_create()
{
	Operation *thiz = malloc(sizeof(Operation));

	if(thiz != NULL)
	{

		thiz = (Operation *)oper_add_create();
	}

	return thiz;
}

void add_factory_destroy(AddFactory *thiz)
{
	if(thiz	!= NULL)
	{
		SAFE_FREE(thiz);
	}

	return ;
}

AddFactory *AddFactoryCreate(void)
{
	AddFactory *thiz = malloc(sizeof(AddFactory));

	if(thiz != NULL)
	{
		thiz->create_oper = add_factory_create;
		thiz->destroy = add_factory_destroy;
	}
}


factoryb.h

#ifndef __FACTORYB_H__
#define __FACTORYB_H__

#include "product.h"

struct _MinusFactory;
typedef struct _MinusFactory MinusFactory;

typedef Operation* (*CreateMinusOperFunc)();
typedef void (*MinusFactoryDestroyFunc)(MinusFactory *thiz);

struct _MinusFactory
{
	CreateMinusOperFunc create_oper;
	MinusFactoryDestroyFunc destroy;
};

MinusFactory *MinusFactoryCreate(void);
Operation* minus_factory_create();
void minus_factory_destroy(MinusFactory *thiz);

#endif


factoryb.c

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "factoryb.h"
#include "productb.h"

Operation* minus_factory_create()
{
	Operation *thiz = malloc(sizeof(Operation));

	if(thiz != NULL)
	{
		thiz = (Operation *)oper_minus_create();
	}

	return thiz;
}

void minus_factory_destroy(MinusFactory *thiz)
{
	if(thiz	!= NULL)
	{
		SAFE_FREE(thiz);
	}

	return ;
}

MinusFactory *MinusFactoryCreate(void)
{
	MinusFactory *thiz = malloc(sizeof(MinusFactory));

	if(thiz != NULL)
	{
		thiz->create_oper = minus_factory_create;
		thiz->destroy = minus_factory_destroy;
	}
}


product.h

#ifndef __PRODUCT_H__
#define __PRODUCT_H__

#include <assert.h>
#include "typedef.h"

#ifdef __cplusplus
extern "C" {
#endif

struct _Operation;
typedef struct _Operation Operation;

struct _Result;
typedef struct _Result Result;

typedef Result* (*ResultFunc)(int a, int b);
typedef void (*DestroyFunc)(Operation* thiz);

struct _Result
{
	int val;
};

struct _Operation
{
	ResultFunc result;
	DestroyFunc destroy;
};

static inline Result* oper_result(Operation *thiz, int a, int b)
{
	return_val_if_fail(thiz != NULL, NULL);

	if(thiz->result != NULL)
	{
		return thiz->result(a, b);
	}
}

static inline void oper_destroy(Operation *thiz)
{
	if(thiz != NULL && thiz->destroy != NULL)
	{
		thiz->destroy(thiz);
	}

	return ;
}

#ifdef __cplusplus
}
#endif

#endif


producta.h

#ifndef __PRODUCTA_H__
#define __PRODUCTA_H__

#include "product.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct _Add Add;

typedef Result* (*AddResultFunc)(int a, int b);
typedef void (*AddDestroyFunc)(Add* thiz);

struct _Add
{
	AddResultFunc add_result;
	AddDestroyFunc add_destroy;
};

Add *oper_add_create(void);

#ifdef __cplusplus
}
#endif

#endif


producta.c

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "producta.h"
#include "typedef.h"

static Result *oper_add_result(int a, int b)
{
	Result *result = malloc(sizeof(Result));
	if(result != NULL)
	{
		result->val = a + b;
	}
	return result;
}

static void oper_add_destroy(Add *thiz)
{
	if(thiz != NULL)
	{
		SAFE_FREE(thiz);
	}

	return ;
}

Add *oper_add_create(void)
{
	Add *thiz = malloc(sizeof(Add));

	if(thiz != NULL)
	{
		thiz->add_result = oper_add_result;
		thiz->add_destroy = oper_add_destroy;
	}

	return thiz;
}


productb.h

#ifndef __PRODUCTB_H__
#define __PRODUCTB_H__

#include "product.h"

#ifdef __cplusplus
extern "C" {
#endif

struct _Minus;
typedef struct _Minus Minus;

typedef Result* (*MinusResultFunc)(int a, int b);
typedef void (*MinusDestroyFunc)(Minus* thiz);

struct _Minus
{
	MinusResultFunc minus_result;
	MinusDestroyFunc minus_destroy;
};

Minus *oper_minus_create(void);

#ifdef __cplusplus
}
#endif

#endif


productb.c

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "productb.h"
#include "typedef.h"

static Result *oper_minus_result(int a, int b)
{
	Result *result = malloc(sizeof(Result));
	if(result != NULL)
	{
		result->val = a - b;
	}
	return result;
}

static void oper_minus_destroy(Minus *thiz)
{
	if(thiz != NULL)
	{
		SAFE_FREE(thiz);
	}
}

Minus *oper_minus_create(void)
{
	Minus *thiz = malloc(sizeof(Minus));

	if(thiz != NULL)
	{
		thiz->minus_result = oper_minus_result;
		thiz->minus_destroy = oper_minus_destroy;
	}

	return thiz;
}


test.c

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "factory.h"
#include "factorya.h"
#include "factoryb.h"
#include "product.h"

int main(int argc, char **argv)
{
	Factory *factory = (Factory*)AddFactoryCreate();
	Operation *oper = factory_create_oper(factory);
	Result* result = oper_result(oper, 3, 5);  
	printf("%d\n", result->val);
	oper_destroy(oper);
	factory_destroy(factory);
	return 0;
}


Makefile

exe:=test
temp := $(wildcard test *~) 

all:test.c factory.h factorya.c factorya.h factoryb.c factoryb.h product.h producta.c producta.h productb.c productb.h typedef.h
	gcc -g $^ -o $(exe)

clean:
	rm $(temp)


【源码下载】

 

 

*欢迎纠错,共同进步!

 

转载请标明出处,仅供学习交流,勿用于商业目的

Copyright @ http://blog.csdn.net/tandesir

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值