C语言实现OOP 版本2
写版本2的原因,还是发现在不同的具体图形模块里发现了重复的release代码,这是坏味道,所以还是决定消除这些重复代码,DRY!
shape.h
#ifndef SHAPE_H
#define SHAPE_H typedef struct shape_t
{
void *shapeData;
void (*area)(void *);
void (*release)(void *);
}Shape; void release(void *shape); #endif
shape.c
#include <stdlib.h>
#include "shape.h" void release(void *shape)
{
free(((Shape*)shape)->shapeData);
free(shape);
}
circle.h
#ifndef CIRCLE_H
#define CIRCLE_H typedef struct
{
double r;
}CircleData; typedef struct
{
void *shapeData;
void (*area)(void *);
void (*release)(void *);
}Circle; Circle *makeCircle(double r); #endif
circle.c
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "shape.h"
#include "circle.h" const double PI = 3.14159; static void area(void *shape)
{
Circle *_circle = (Circle *)shape;
CircleData* data = (CircleData*)_circle->shapeData;
printf("the circle area is %f \n", data->r * data->r * PI);
} /*
static void release(void *shape)
{
Circle *_circle = (Circle *)shape;
CircleData* data = (CircleData*)_circle->shapeData;
free(data);
free(_circle);
}
*/ Circle *makeCircle(double r)
{
CircleData* circleData = (CircleData*)malloc(sizeof(CircleData));
Circle* circle = (Circle*)malloc(sizeof(Circle));
assert(circleData != NULL);
assert(circle != NULL);
assert(r > ); circleData->r = r;
circle->shapeData = circleData;
circle->area = &area;
circle->release = &release; return circle;
}
square.h
#ifndef SQUARE_H
#define SQUARE_H typedef struct
{
double x;
double y;
}SquareData; typedef struct
{
void *shapeData;
void (*area)(void *);
void (*release)(void *);
}Square; Square *makeSquare(double x, double y); #endif
square.c
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "shape.h"
#include "square.h" static void area(void *shape)
{
Square *square = (Square *)shape;
SquareData* data = (SquareData*)square->shapeData;
printf("the square area is %f \n", data->x * data->y);
} /*
static void release(void *shape)
{
Square *square = (Square *)shape;
SquareData* data = (SquareData*)square->shapeData;
free(data);
free(square);
}
*/ Square *makeSquare(double x, double y)
{
SquareData* squareData = (SquareData*)malloc(sizeof(SquareData));
Square* square = (Square*)malloc(sizeof(Square));
assert(squareData != NULL);
assert(square != NULL);
assert(x > && y > ); squareData->x = x;
squareData->y = y;
square->shapeData = squareData;
square->area = &area;
square->release = &release; return square;
}
main.c 发现没有,尽管内部进行了调整,这些的代码丝毫没变!重构就应该这样,内部的调整不太涉及到client代码,除非真的决定修改接口,修改接口内部应该优先于调整接口
#include <stdio.h>
#include "shape.h"
#include "circle.h"
#include "square.h" void printShapeArea(Shape **shape,int length)
{
int i=;
for(i=;i<length;i++)
{
shape[i]->area(shape[i]);
shape[i]->release(shape[i]);
}
} int main()
{
Shape *p[] = {(Shape*)makeCircle(3.2),(Shape*)makeCircle(3.2),(Shape*)makeSquare(3.1,)};
printShapeArea(p,);
return ;
}
C语言实现OOP 版本2的更多相关文章
- C语言实现OOP 版本3 :简化代码
我倒是不追求代码和C++相似,但是应该追求简洁的代码,下面是一个新的尝试 shape.h #ifndef SHAPE_H #define SHAPE_H typedef struct shape_t ...
- 一个UUID生成算法的C语言实现 --- WIN32版本 .
一个UUID生成算法的C语言实现——WIN32版本 cheungmine 2007-9-16 根据定义,UUID(Universally Unique IDentifier,也称GUID)在时 ...
- C# 语言规范_版本5.0 (第2章 词法结构)
1. 词法结构 1.1 程序 C# 程序 (program) 由一个或多个源文件 (source file) 组成,源文件的正式名称是编译单元 (compilation unit)(第 9.1 节). ...
- 几种不同程序语言的HMM版本
几种不同程序语言的HMM版本 “纸上得来终觉浅,绝知此事要躬行”,在继续翻译<HMM学习最佳范例>之前,这里先补充几个不同程序语言实现的HMM版本,主要参考了维基百科.读者有兴趣的话可以研 ...
- 一个UUID生成算法的C语言实现——WIN32版本
源: 一个UUID生成算法的C语言实现——WIN32版本
- C语言的OOP实践(OOC)
OOC 面向对象 C 语言编程实践 - 文章 - 伯乐在线http://blog.jobbole.com/105105/ ---硬着头皮看完了,但是感觉还是抽象有不理解的地方,感觉用C实现OOP好难啊 ...
- C# 语言规范_版本5.0 (第10章 类)
1. 类 类是一种数据结构,它可以包含数据成员(常量和字段).函数成员(方法.属性.事件.索引器.运算符.实例构造函数.静态构造函数和析构函数)以及嵌套类型.类类型支持继承,继承是一种机制,它使派生类 ...
- C# 语言规范_版本5.0 (第8章 语句)
1. 语句 C# 提供各种语句.使用过 C 和 C++ 编程的开发人员熟悉其中大多数语句. statement: labeled-statement declaration-statement emb ...
- C# 语言规范_版本5.0 (第7章 表达式)
1. 表达式 表达式是一个运算符和操作数的序列.本章定义语法.操作数和运算符的计算顺序以及表达式的含义. 1.1 表达式的分类 一个表达式可归类为下列类别之一: 值.每个值都有关联的类型. 变量.每个 ...
随机推荐
- C# 代码转换到Python
下载地址:http://pan.baidu.com/s/1dDAZJxv SharpDevelop 3.1 now supports converting C# and VB.NET code to ...
- 奇葩的SQL*Net more data from client等待,导致批处理巨慢
<pre name="code" class="sql"><pre name="code" class="sql ...
- IT人大学生活之“做点正经事”
最近一直主抓部门的人事招聘工作:很多到手的简历,特别是毕业一年之内的同学的简历上面都会写到:在xxx餐饮公司实习,获得了与人交流的经验:在学生会组织了哪些文体活动:在大四参加了一些与软件开发不相关的一 ...
- list 操作
animals = ["aardvark", "badger", "duck", "emu", "fennec ...
- 基于PCA和SVM的人脸识别系统-error修改
------------------------------------------------- Undefined function or variable 'W'. Error in class ...
- 利用dedecms给近三天(或当天)发布的文章显示红色日期或加上new字或new小图片
1)红色日期 <br>[field:pubdate runphp='yes'] <br>$a="<font color=red>".strfti ...
- [2011山东省第二届ACM大学生程序设计竞赛]——Identifiers
Identifiers Time Limit: 1000MS Memory limit: 65536K 题目:http://acm.sdut.edu.cn/sdutoj/problem.php?act ...
- 选择Android还是IOS开发?
选择Android还是IOS? 随着移动互联网的如日中天,涌现了越来越多的开发者.IOS优秀的用户体验,Android极高的用户群,这对于开发者来说陷入了选择困难的境地,尤其是新入门的开发者,精力有限 ...
- C#Equal的使用
代码如下: public partial class FramMain : Form { public FramMain() { InitializeComponent(); } private vo ...
- CentOS CVS安装使用
CentOS CVS安装使用 一.CVS简介 CVS(Concurrent Versions System)版本控制系统:是一种GNU软件包,CVS是一个C/S系统,主要用于在多人开发环境下的源码 ...