c 指针实现面向对象

指针是C的灵魂,正是指针使得C存在了这么多年,而且将长期存在下去。事实上,我自己不用C语言写程序已经有一年了,工作中接触到的只有java,python和javascript.最近用C完成了一下类似于OO中的封装(即"类")的概念,顺便把指针复习了下,感觉有必要记一下。

    本文中的例子有这样两个概念:任务(Task),执行器(Executor)。任务有名称(taskName),并且可以执行(execute)。 而执行器与具体任务所执行的内容无关,只是回调(callback)任务的执行方法,这样我们的执行器就可以做的比较通用。而任务接口只需要实现一个execute方法即可,这样我们的任务就可以是多种多样的,可以通过统一的接口set给执行器执行。这是面向对象中基本的思想,也是比较常用的抽象方式。下面我们具体看下例子。

    可以想象,main函数大概是这个样子:

 int main(int argc, char** argv) {

    Task *t1 = TaskConstruction("Task1", run);//此处的run是一个函数指针
    Executor *exe = ExecutorConstruction();
    exe->setTask(t1);
    exe->begin();
    exe->cancel();

    Task *t2 = TaskConstruction("Task2", run2);//此处的run2也是一个函数指针,用于构造一个Task.
    exe->setTask(t2);
    exe->begin();
    exe->cancel();
    
    return (EXIT_SUCCESS);
}

    运行结果为:

 task : [Task1] is ready to run
[a = 1.200000, b = 2.300000]
[(a + b) * (a - b) = -3.850000]
cancel is invoked here
task : [Task2] is ready to run
another type of execute,just print out some information
cancel is invoked here

 

好了,下面详细看看实现:

    定义接口

    首先,定义Task和Executor两个实体的接口:

    Task接口,注意其中的_this字段,这个指针在后边有很重要的作用,用于hold整个Task的实例。然后是一个taskName的字符串,和一个函数指针,这个指针在初始化(构造)Task时传入。这个execute()函数比较有意思,它不在内部使用,而是让执行器回调执行的。

 #ifndef _ITASK_H
#define    _ITASK_H

typedef struct Task{
    struct Task *_this;
    char *taskName;
    void (*execute)();
}Task;

void execute();
#endif    /* _ITASK_H */

    执行器接口比Task接口复杂一些,其中包含_this指针,包含一个对Task的引用,然后是对外的接口begin(), cancel()。对接口的使用者来说,他们只需要调用接口实例上的setTask(),将任务传递给执行器,然后在适当时期调用begin(),等待任务正常结束或者调用cancel()将其取消掉。

 #include "ITask.h"

#ifndef _IEXECUTOR_H
#define    _IEXECUTOR_H

typedef struct Executor{
    struct Executor *_this;
    Task *task;
    char *(*setTask)(Task* task);
    void (*begin)();
    void (*cancel)();
}Executor;

char *setTask(Task *task);
void begin();
void cancel();

#endif /* _IEXECUTOR_H */

    实现接口

 #include <stdlib.h>
#include "ITask.h"

Task *task = NULL;

void execute();

/*
 * The construction of Task object.
 * name : the name of the task
 * execute : execute method of the task
 * 
 */
Task *TaskConstruction(char *name, void (*execute)()){
    task = (Task*)malloc(sizeof(strlen(name))+sizeof(execute));
    task->taskName = name;
    task->execute = execute;
    task->_this = task;
    
    return (Task*)task;//返回一个自身的指针,通过内部的_this指针,两者即可实现封装
}

/*
 * Destruction of task, not used current time.
 *
 */
void TaskDestruction(){
    task->taskName = NULL;
    task->execute = NULL;
    task->_this = NULL;
    task = NULL;
}

/*
 * private method, should register to executor
 *
 */
void execute(){
    task->_this->execute();//调用_this上的execute()方法
}

 

 

执行器的实现一样,稍微复杂一点,构造的时候,将函数指针在内部设置好,当外部调用时动态的执行需要执行的函数,这句话可能有些绕口,这么看:在构造Executor的时候,executor->begin = begin; 这条语句是将下面void begin()的实现注册到结构体中,但是要执行什么还是不确切的,当setTask以后,回调函数的地址已经明确:(executor->_this->task = task;),此时调用begin()即可正确的调用到注册的Task上。

 #include <stdlib.h>
#include "IExecutor.h"

Executor *executor = NULL;

Executor *ExecutorConstruction(){
    executor = (Executor*)malloc(sizeof(Executor));
    executor->begin = begin;
    executor->cancel = cancel;
    executor->setTask = setTask;

    executor->_this = executor;

    return (Executor*)executor;
}

void ExecutorDestruction(){
    executor->begin = NULL;
    executor->cancel = NULL;
    executor->setTask = NULL;
    executor = NULL;
}

char *setTask(Task *task){
    executor->_this->task = task;
}

void begin(){
    printf("task : [%s] is ready to runn",executor->_this->task->taskName);
    executor->_this->task->execute();
}

void cancel(){//这个函数没有实现,只是做了一个占位符,以后如果有多线程,可以用来停止主动线程。
    printf("cancel is invoked heren");
}

    其实,两个实现的代码都不算复杂,如果对C的指针理解的稍好,基本就没什么问题了。

    在C中使用OO

    为了试验,我们不妨设计两个不同的Task,一个Task是计算两个数的某四则混合运算,另一个仅仅是用来打印一点信息。然后我们可以看到,他们使用完全相同的接口来执行:

 #include <stdio.h>

void run(){//计算(a+b)*(a-b)
    float a, b, r;
    a = 1.2;
    b = 2.3;
    r = 0.0;
    printf("[a = %f, b = %f]n", a, b);
    printf("[(a + b) * (a - b) = %f]n",((a+b)*(a-b)));
}

void run2(){//打印一句话,事实上,这些函数可以做任何事,比如I/O,网络,图片处理,音乐播放等等。
    printf("another type of execute,");
    printf("just print out some informationn");
}

    然后,在Main中奖他们注册给Task,代码如下所示:

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

#include "ITask.h"
#include "IExecutor.h"

extern void run();
extern void run2();

int main(int argc, char** argv) {
//代码的风格上,应该可以看出和OO的风格及其类似。
    Task *t1 = TaskConstruction("Task1", run);//new Task("Task 1", run);
    Executor *exe = ExecutorConstruction();// new Executor();
    exe->setTask(t1);
    exe->begin();
    exe->cancel();
 
    Task *t2 = TaskConstruction("Task2", run2);
    exe->setTask(t2);
    exe->begin();
    exe->cancel();
    
    return (EXIT_SUCCESS);
}

    程序的输出结果上文中已经可以看到了,这里就不贴了。

    当然,本文的主要目的不是想说什么“C也可以实现面向对象”之类的幼稚观点,只要谁没有严重的自虐倾向,相信不会有谁真的会用C来做OO的开发。只是想表达一下,指针在C中的重要性和指针的一点高级用法。其实现在的OO语言,基本还是以面向过程的表达式来表达面向对象而已。并没有什么神奇之处,OO主要是思想上的抽象,可以说是语言无关的(language independent)。

 

转载http://c.chinaitlab.com/c/basic/200905/785072_3.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章