C语言实现设计模式之简单工厂模式(无内存泄露)

最近工作之余研究一下设计模式细节方面的东西,很多经典的东西需要一遍遍去读、去理解,设计模式就是这样的程序设计的经典之作。

我一直坚信学习是把书读薄,又把书读厚的过程,最终达到游刃有余。

言归正传,接下来是研究设计模式自己亲手写下的一段代码,看见网上也有很多人发了关于使用简单工厂实现计算的代码,可是都是浅尝辄止,大多存在内存泄露,没有回收资源的情况,本人发此博文,绝非卖弄,只是充实自我生活,升华自我价值所用。

 

Operation.h  //运算类,在C++这叫抽象类

#define operation_free free

 

/*运算类抽象*/

typedef int oper_t;

struct operation

{

oper_t (* result)(oper_t a, oper_t b);  /*虚接口,派生类会根据自己需求进行实现*/

void (*destructor)(struct operation *p_oper); /*当一个类被用来作为基类的时候,才把析构函数写成虚函数,来调用派生类的析构.*/

};

 

/*

为什么基类的析构函数是虚函数?

在实现多态时,当用基类操作派生类,在析构时防止只析构基类

而不析构派生类的状况发生。

*/

struct operation * create_operation(char oper_name);

 

#endif

 

Operation.c  运算类,在C++这叫抽象类

 

#include <stdio.h>

#include <stdlib.h>

#include "operation.h"

#include "operation_add.h"

#include "operation_dec.h"

#include "operation_mul.h"

 

 

 

struct operation * create_operation(char oper_name)

{

struct operation * p_oper = NULL;

struct operation_add * p_add;

struct operation_dec * p_dec;

struct operation_mul * p_mul;

switch(oper_name)

{

case '+':

p_add = add_structor();

p_oper = &p_add->oper;

break;

case '-':

p_dec = dec_structor();

p_oper = &p_dec->oper;

break;

case '*':

p_mul = mul_structor();

p_oper = &p_mul->oper;

break;

default:

p_oper = NULL;

break;

}

 

return p_oper;

}

 

Factory.h    这就是工厂类了。

#ifndef __FACTORY__H__

#define __FACTORY__H__

struct factory

{

void (* factory_destructor)(struct factory *p_fac);

struct operation * (* create_operation)(char oper_name);

};

struct factory * factory_structor(void);

 

#endif

 

Factory.c  这就是工厂类了。

#include <stdlib.h>

#include "operation.h"

 

struct factory

{

void (* factory_destructor)(struct factory *p_fac);

struct operation * (* create_operation)(char oper_name);

};

void factory_destructor(struct factory *p_fac)

{

operation_free(p_fac);

}

 

struct factory * factory_structor(void)

{

struct factory * p_fac = operation_malloc(sizeof(struct factory));

if(NULL == p_fac)

{

return NULL;

}

p_fac->factory_destructor = factory_destructor;

p_fac->create_operation = create_operation;

 

return p_fac;

}

 

Operation_add.h   加法的封装

#ifndef __OPERATION__ADD_H__

#define __OPERATION__ADD_H__

/*加法类抽象*/

struct operation_add

{

struct operation oper;  /*继承运算类*/

void (*destructor)(struct operation_add *p_add);  /*加法操作析构函数*/

oper_t (* result)(oper_t a, oper_t b);  /*加法操作结果func*/

};

 

struct operation_add * add_structor(void);

 

#endif

 

Operation_add.c  加法的封装

 

#include <stdlib.h>

 

#include "operation.h"

#include "operation_add.h"

 

oper_t add_result(oper_t a, oper_t b)

{

return a + b;

}

void add_destructor(struct operation_add *p_add)

{

operation_free(p_add);

}

void operation_add_destructor(struct operation *p_oper)

{

struct operation_add *p_add = container_of(p_oper, struct operation_add, oper);

p_add->destructor(p_add);

}

 

struct operation_add * add_structor(void)

{

struct operation_add * p_add = operation_malloc(sizeof(struct operation_add));

if(NULL == p_add)

{

return NULL;

}

p_add->destructor = add_destructor;

p_add->result = add_result;

p_add->oper.result = p_add->result;

p_add->oper.destructor = operation_add_destructor;

return p_add;

}

 

Operation_dec.h

#ifndef __OPERATION__DEC_H__

#define __OPERATION__DEC_H__

struct operation_dec

{

struct operation  oper;

void (*dec_destructor)(struct operation_dec * p_dec);

oper_t (*result)(oper_t a, oper_t b);

};

 

 

struct operation_dec * dec_structor(void);

 

#endif

 

Operation_dec.c

#include <stdlib.h>

#include"operation.h"

#include"operation_dec.h"

 

oper_t dec_result(oper_t a, oper_t b)

{

return a - b;

}

 

void dec_destructor(struct operation_dec * p_dec)

{

operation_free(p_dec);

}

void operation_dec_destructor(struct operation * p_oper)

{

struct operation_dec * p_dec = container_of(p_oper, struct operation_dec, oper);

p_dec->dec_destructor(p_dec);

}

 

struct operation_dec * dec_structor(void)

{

struct operation_dec * p_dec = operation_malloc(sizeof(struct operation_dec));

if(p_dec == NULL)

{

return NULL;

}

p_dec->dec_destructor = dec_destructor;

p_dec->result = dec_result;

p_dec->oper.result = p_dec->result;

p_dec->oper.destructor = operation_dec_destructor;

return p_dec;

}

 

代码太多,乘法和除法等就不一一列举,勤劳的人自然会自我实现,聪明的人自然知道如何实现。

 

下面是main函数和Makefile

 

Main函数   需要几个参数,如 ./my_calc 1 + 1

 

#include <stdio.h>

#include <stdlib.h>

#include "operation.h"

 

#ifdef FACTORY

#include "factory.h"

 

int main(int argc, char **argv)

{

int result;

int ret = 0;

int a = 0, b = 0;

char oper = '+';

 

printf("%s   %s  %s\n",argv[1], argv[2], argv[3]);

if(argc == 4)

{

a = atoi(argv[1]);

b = atoi(argv[3]);

oper = argv[2][0];

}

 

/*创建简单工厂对象

    简单工厂模式用一个单独的类来做创造实例的过程

*/

struct factory * p_fac = factory_structor();

if(NULL == p_fac)

{

return -1;

}

/*创造出实例*/

struct operation * p_oper = p_fac->create_operation(oper);

if(NULL == p_oper)

{

ret = -1;

goto label;

}

result = p_oper->result(a,  b);

printf("%d %c %d  = %d\r\n", a, oper,b, result);

/*析构实例*/

p_oper->destructor(p_oper);

label:

/*析构工厂*/

p_fac->factory_destructor(p_fac);

return ret;

}

#endif 

 

 

 

Makefile  写的有点粗陋,这都不是重点。

app:my_calc

COMPILER=

CC=$(COMPILER)gcc

CFLAGS=-g -c -D FACTORY 

LDFLAGS=

OBJ:

my_calc:main.o operation.o factory.o operation_add.o operation_dec.o operation_mul.o

$(CC) $^ -o $@ $(LDFLAGS)

operation.o:operation.c

$(CC) $^ $(CFLAGS)

main.o:main.c

$(CC) $^ $(CFLAGS)

factory.o:factory.c

$(CC) $^ $(CFLAGS)

operation_add.o:operation_add.c

$(CC) $^ $(CFLAGS)

operation_dec.o:operation_dec.c

$(CC) $^ $(CFLAGS)

operation_mul.o:operation_mul.c

$(CC) $^ $(CFLAGS)

clean:

rm  *.o my_calc

.PHONY:clean app

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
工厂设计模式是一种创建型设计模式,它提供了一种创建对象的方式,使得对象的创建过程与具体使用代码分离,从而提高代码的可维护性和可扩展性。 在C语言中,可以使用结构体来实现工厂设计模式。首先定义一个工厂结构体,用于创建对象。这个结构体包含一个函数指针,指向一个用于创建对象的函数。然后定义一个接口结构体,用于表示所有被创建的对象共同具有的属性和方法。最后定义具体的对象结构体,它继承自接口结构体,并实现了接口结构体中的方法。 下面是一个简单的工厂设计模式的C语言实现示例: ``` #include <stdio.h> /* 定义接口结构体 */ struct Shape { void (*draw)(struct Shape *); }; /* 定义具体的对象结构体 */ struct Circle { struct Shape shape; }; /* Circle的draw方法实现 */ void Circle_draw(struct Shape *shape) { printf("Drawing a circle\n"); } /* 定义工厂结构体 */ struct ShapeFactory { struct Shape *(*create)(void); }; /* Circle的创建方法实现 */ struct Shape *Circle_create(void) { struct Circle *circle = malloc(sizeof(struct Circle)); circle->shape.draw = Circle_draw; return (struct Shape *)circle; } /* 工厂结构体中创建函数指向具体的创建方法 */ struct ShapeFactory shapeFactory = { .create = Circle_create }; int main() { /* 通过工厂结构体创建对象 */ struct Shape *shape = shapeFactory.create(); /* 调用对象的方法 */ shape->draw(shape); return 0; } ``` 在这个示例中,我们使用了一个工厂结构体ShapeFactory来创建具体的对象,这里我们只实现了一个Circle的对象,但是你可以通过新增一个函数指针来创建其他类型的对象。另外,我们使用了接口结构体Shape来表示所有对象的公共属性和方法,这样可以方便的扩展其他对象的实现

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值