Object oriented in c language

来源:互联网 发布:小学生编程软件 编辑:程序博客网 时间:2024/05/22 14:04

How-To: Object Oriented in C

Purpose

I would like to introduce Object Oriented programming and concept adapted to the C language.

There are cases where you need high performance and to have access to every bits of your piece of software.

For example if you develop in a highly demanding environment where robustness, efficiency and speed are key factors, you need to be able to master most of your code. You don’t want to rely on obscure libraries build-up in a teenager garage for example :o).

The problem is that if you grown up using PHP, Java and C++ it is a bit difficult to come back to the good old programs using global functions and global variables. You want to be able to usedecorators, and also some visitor, observer orsingletons patterns.

Well, it is possible!, OK, you will lack some of the magic provided bu Java or C++, but you still can do a pretty good job and get most of the evolution and portability provided by object-oriented programming

Road-map

I propose to start with a basic code structure and use it as main base for showing you every kind of patterns… It is not possible to use all the patterns in C, but most of them actually.

The model I will use is a computer machine (PC) with a Central Unit, a Screen and a Printer. It will be enough to do what we want.

Files

common.h

#ifndef COMMON_H#define COMMON_H#include #include #include #define OK EXIT_SUCCESS#define KO EXIT_FAILUREtypedef enum {COMPUTER,SCREEN,PRINTER,}ComputerParts;#endif

computer.h

/* * Computer.h * *  Created on: 11 oct. 2012 *      Author: Alexandre Melard */#ifndef COMPUTER_H_#define COMPUTER_H_#include "screen.h"#include "printer.h"typedef struct __computer {char* vendor;char* serial;Screen * screen;Printer * printer;int (*start)(struct __computer*);  这里很关键,都是使用的一些函数指针。int (*run)(struct __computer*); int (*stop)(struct __computer*);void (*destroy)(struct __computer *);} Computer;Computer * computer_new(char *, char *, Screen *, Printer *);#endif /* COMPUTER_H_ */

computer.c

/* * computer.c * *  Created on: 11 oct. 2012 *      Author: Alexandre Melard */#include "common.h"#include "computer.h"/** * Destructor for Computer object. * use to clean up the memory * @param this */static void _destroy(Computer* this) {if (NULL != this) {free(this); 很关键的一句,封装得这么深!this = NULL;}}/** * Starts the Computer object, starts the screen, the printer and so on. * @param this * @return OK or KO */static int _start(Computer* this) {puts("Computer is starting");this->screen->start(this->screen);  启动电脑的时候同时启动屏幕和打印机,这里的参数真是奇怪呀???this->printer->start(this->printer); 为什么这么写呢?难道在级联?puts("Computer is ready");return OK;}/** * Run the Computer object, run the screen, the printer and start to work. * @param this * @return OK or KO */static int _run(Computer* this) {puts("Computer is preparing to run");this->screen->run(this->screen);  运行电脑时候同时调用它所有附件的run()函数。this->printer->run(this->printer, "MyDoc"); 这是要打印一份文档吗???puts("Computer is running");return OK;}/** * Stop the Computer object, stop screen, printer, say goodbye. * @param this * @return OK or KO */static int _stop(Computer* this) {puts("Computer is stopping");this->screen->stop(this->screen);this->printer->stop(this->printer);puts("Computer has stopped");return OK;}/** * Constructor, allocate memory and set up credentials. * @param vendor name of the screen manufacturer * @param serial serial number * @param screen screen object to control * @param printer printer object to control * @return an instance of Computer */Computer * computer_new(char* vendor, char* serial, Screen* screen,Printer * printer) {Computer * this;  怎么喜欢用this呀???这可是c++中的使用的!this = (Computer *) calloc(1, sizeof(*this));  这个函数就是在内部申请了内存并赋一些指针的值,初始化嘛。this->destroy = _destroy;this->start = _start;this->run = _run;this->stop = _stop;this->vendor = vendor;this->serial = serial;this->screen = screen;  附件指针this->printer = printer;
附件指针

return this;}

printer.h

/* * printer.h * *  Created on: 11 oct. 2012 *      Author: Alexandre Melard */#ifndef PRINTER_H#define PRINTER_Htypedef struct __printer {char* vendor;char* serial;int (*start)(struct __printer*);   主类computer有这些方法,附件也得有这些方法,调用主类的这个方法时,遍历附件都调用一遍!!!int (*run)(struct __printer*, char*);int (*stop)(struct __printer*);void (*destroy)(struct __printer *);} Printer;Printer * printer_new(char *, char*);#endif /* PRINTER_H */

printer.c

/* * printer.c * *  Created on: 11 oct. 2012 *      Author: Alexandre Melard */#include "common.h"#include "printer.h"/** * Destructor for Printer object. * use to clean up the memory * @param this */static void _destroy(Printer* this) {if (NULL != this) {free(this);this = NULL;}}/** * Starts the Printer object, turn on power clean-up and prepare print heads. * @param this * @return OK or KO */static int _start(Printer* this) {puts("Printer is started");return OK;}/** * Run the Printer object, print a document. * @param this * @return OK or KO */static int _run(Printer* this, char* doc) {printf("Printer is printing doc [%s]\n", doc);return OK;}/** * Stop the Printer object, close computer connection, switch off the power. * @param this * @return OK or KO */static int _stop(Printer* this) {puts("Printer has stopped");return OK;}/** * Constructor, allocate memory and set up credentials. * @param vendor name of the screen manufacturer * @param serial serial number * @return an instance of Printer */Printer * printer_new(char* vendor, char* serial) {Printer * this;this = (Printer *) calloc(1, sizeof(*this));this->destroy = _destroy;this->start = _start;this->run = _run;this->stop = _stop;this->vendor = vendor;this->serial = serial;return this;}

screen.h

/* * Computer.h * *  Created on: 11 oct. 2012 *      Author: Alexandre Melard */#ifndef SCREEN_H#define SCREEN_Htypedef struct __screen {char* vendor;char* serial;int (*start)(struct __screen*);  这个屏幕也是computer的附件,也具有所有函数指针。int (*run)(struct __screen*);int (*stop)(struct __screen*);void (*destroy)(struct __screen *);} Screen;Screen * screen_new(char*, char*);#endif /* SCREEN_H */

screen.c

/* * screen.c * *  Created on: 11 oct. 2012 *      Author: Alexandre Melard */#include "common.h"#include "screen.h"/** * Destructor for Screen object. * use to clean up the memory * @param this */static void _destroy(Screen* this) {if (NULL != this) {free(this);this = NULL;}}/** * Starts the Screen object, turn on lights and so on. * @param this * @return OK or KO */static int _start(Screen* this) {puts("Screen is started");return OK;}/** * Run the Screen object, accept input signal and show it. * @param this * @return OK or KO */static int _run(Screen* this) {puts("Screen is running");return OK;}/** * Stop the Screen object, close computer connection, turn off the light. * @param this * @return OK or KO */static int _stop(Screen* this) {puts("Screen has stopped");return OK;}/** * Constructor, allocate memory and set up credentials. * @param vendor name of the screen manufacturer * @param serial serial number * @return an instance of Screen */Screen * screen_new(char* vendor, char* serial) {Screen * this;this = (Screen *) calloc(1, sizeof(*this));this->destroy = _destroy;this->start = _start;this->run = _run;this->stop = _stop;this->vendor = vendor;this->serial = serial;return this;}

main.c

/* * main.c * *  Created on: 11 oct. 2012 *      Author: Alexandre Melard */#include "common.h"#include "computer.h"/** * Main function, instantiate and run Computer and Visitors. * @return */int main(void) {puts("visitor example starting");   这个主函数很奇怪呀?不知道这是要show什么 ?Printer* printer = printer_new("CANON", "S123REE");  创建一个printer,不是类,是结构体模拟的类!Screen* screen = screen_new("LACIE", "LACIE321");   创建一个Screen,不是类,是结构体模拟的类!Computer* computer = computer_new("HP", "HP2134R", screen, printer); 创建一个computer.computer->start(computer);  //调用computer的start()函数指针时将同时调用附件的start()函数。computer->run(computer);computer->stop(computer);computer->destroy(computer);    这个destroy()就得分别调用了,因为前面computer的destroy()中并没有调用附件的destroy()screen->destroy(screen);          如果加上的话,这里就不用再次调用了,多方便呀。。。printer->destroy(printer);return EXIT_SUCCESS;}

results

visitor example startingComputer is startingScreen is startedPrinter is startedComputer is readyComputer is preparing to runScreen is runningPrinter is printing doc [MyDoc]Computer is runningComputer is stoppingScreen has stoppedPrinter has stoppedComputer has stopped这代码其实也没有什么使用,就是为了演示一下c中如何使用面向对象进行封装!
0 0