最近工作之余研究一下设计模式细节方面的东西,很多经典的东西需要一遍遍去读、去理解,设计模式就是这样的程序设计的经典之作。
我一直坚信学习是把书读薄,又把书读厚的过程,最终达到游刃有余。
言归正传,接下来是研究设计模式自己亲手写下的一段代码,看见网上也有很多人发了关于使用简单工厂实现计算的代码,可是都是浅尝辄止,大多存在内存泄露,没有回收资源的情况,本人发此博文,绝非卖弄,只是充实自我生活,升华自我价值所用。
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