写版本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的更多相关文章

  1. C语言实现OOP 版本3 :简化代码

    我倒是不追求代码和C++相似,但是应该追求简洁的代码,下面是一个新的尝试 shape.h #ifndef SHAPE_H #define SHAPE_H typedef struct shape_t ...

  2. 一个UUID生成算法的C语言实现 --- WIN32版本 .

    一个UUID生成算法的C语言实现——WIN32版本   cheungmine 2007-9-16   根据定义,UUID(Universally Unique IDentifier,也称GUID)在时 ...

  3. C# 语言规范_版本5.0 (第2章 词法结构)

    1. 词法结构 1.1 程序 C# 程序 (program) 由一个或多个源文件 (source file) 组成,源文件的正式名称是编译单元 (compilation unit)(第 9.1 节). ...

  4. 几种不同程序语言的HMM版本

    几种不同程序语言的HMM版本 “纸上得来终觉浅,绝知此事要躬行”,在继续翻译<HMM学习最佳范例>之前,这里先补充几个不同程序语言实现的HMM版本,主要参考了维基百科.读者有兴趣的话可以研 ...

  5. 一个UUID生成算法的C语言实现——WIN32版本

    源: 一个UUID生成算法的C语言实现——WIN32版本

  6. C语言的OOP实践(OOC)

    OOC 面向对象 C 语言编程实践 - 文章 - 伯乐在线http://blog.jobbole.com/105105/ ---硬着头皮看完了,但是感觉还是抽象有不理解的地方,感觉用C实现OOP好难啊 ...

  7. C# 语言规范_版本5.0 (第10章 类)

    1. 类 类是一种数据结构,它可以包含数据成员(常量和字段).函数成员(方法.属性.事件.索引器.运算符.实例构造函数.静态构造函数和析构函数)以及嵌套类型.类类型支持继承,继承是一种机制,它使派生类 ...

  8. C# 语言规范_版本5.0 (第8章 语句)

    1. 语句 C# 提供各种语句.使用过 C 和 C++ 编程的开发人员熟悉其中大多数语句. statement: labeled-statement declaration-statement emb ...

  9. C# 语言规范_版本5.0 (第7章 表达式)

    1. 表达式 表达式是一个运算符和操作数的序列.本章定义语法.操作数和运算符的计算顺序以及表达式的含义. 1.1 表达式的分类 一个表达式可归类为下列类别之一: 值.每个值都有关联的类型. 变量.每个 ...

随机推荐

  1. SharedPreferences数据、openFileOutput文件、SQLite数据库文件存储位置

    在模拟器中: SharedPreferences将XML文件保存在/data/data/<package name>/shared_prefs目录下, openFileOutput方法将文 ...

  2. 分享注册AltiumDesignerLive官网账号注册方法教程

    在Altium中国注册Live账号时,Altium会要求填写一些个人信息来申请账号提交账号后,等很长一段时间都没有得到回复.于是选择另一种方法注册打开Altium在俄罗斯的网址 http://ru.a ...

  3. WPF中使用文件浏览对话框的几种方式

    原文:WPF中使用文件浏览对话框的几种方式 WPF本身并没有为我们提供文件浏览的控件, 也不能直接使用Forms中的控件,而文件浏览对话框又是我们最常用的控件之一. 下面是我实现的方式 方式1: 使用 ...

  4. BZOJ1671: [Usaco2005 Dec]Knights of Ni

    1671: [Usaco2005 Dec]Knights of Ni Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 175  Solved: 107[Su ...

  5. VS调试异常代码 异常:HRESULT: 0x80070057 (E_INVALIDARG) 的处理

    碰到这个异常的原因很偶然: 现象:Solution在ReBuild过程中断电了,来电恢复了,重析编译整个Solution不报错,但在浏览页面时始终无法正常浏览,而在design的视图中,每个aspx的 ...

  6. C#的隐式和显示类型转换

    原文地址:http://blog.csdn.net/yysyangyangyangshan/article/details/7494577 关于隐式转换和显示转换,每种语言都有的,C#中当然也不例外. ...

  7. (转)iOS7界面设计规范(13) - UI基础 - 与iOS的系统整合

    突然就到了周日傍晚.你永远不会知道自己的生活在接下来的一周当中能够发生多少变化:各种不可预知性所带来的更多是快感还是焦虑与不安,冷暖自知.相比之下,白天工作当中那些需求列表与排期文档就显得那么可爱了, ...

  8. itoa的源代码实现

    由于通过socket传递数据的时候,仅仅能够通过字符串类型,可是,当我们要传递的数据是整型的是,应该怎么办呢?本来我想着使用for循环,可是,总感觉太麻烦了,后来别人告诉我能够使用itoa,以下是it ...

  9. 使用C++11实现无锁stack(lock-free stack)

    前几篇文章,我们讨论了如何使用mutex保护数据及使用使用condition variable在多线程中进行同步.然而,使用mutex将会导致一下问题: 等待互斥锁会消耗宝贵的时间 — 有时候是很多时 ...

  10. Android与JS混编(js调用android相机扫描二维码)

    参考demo http://www.cnblogs.com/mythou/p/3280023.html        项目源码: https://github.com/weifengzz/Androi ...