Table des matières générée avec Doctoc
Modèles de conception pratiques en C
Ce sera un référentiel de
== == == == == == == == == == == == == == == == == == == == == == == == == == ==
private protected public static pure
- - - - - - - - - - - - - - - - - - - + - - - - - - - - + - - - - - - - + - - - - - - - - + - - - - - + - -
constructor + + +
destructor
virtual +
methods
virtual + +
routine + + +
variables
member - + +
== == == == == == == == == == == == == == == == == == == == == == == == =
+ have implemented
- can implemented with the "handle/body" idiom , but ...Make a pattern
--------------
$ cd auto-gen
$ make
$ make runall
$ make clean_all
Auto Generate class
-------------------
$ cd tools
$ python gencode.py --file json/prototype.json > log <<< the generated code in dir ./tools/code/c/prototype
L'OOP vient de Myobj.h:
struct shape_rectangle * rect ;
rect = malloc ( sizeof ( * rect ));
if (! rect ) return -1 ;
shape_rectangle_init ( rect );
shape_draw ( & rect -> shape );
shape_free ( & rect -> shape ); struct shape_ops ;
struct shape {
struct shape_ops * ops ;
struct color * _color ;
};
struct shape_ops {
void ( * _destructor )( struct shape * );
void ( * free )( struct shape * );
void ( * draw )( struct shape * );
struct shape_ops * __super ;
};
void shape_init ( struct shape * ); struct shape_rectangle * rect ;
shape_rectangle_init ( rect );
shape_draw ( & rect -> shape );
shape_free ( & rect -> shape ); struct shape_rectangle {
struct shape shape ;
};
void shape_rectangle_init ( struct shape_rectangle * ); struct shape_rectangle * rect ;
struct shape_circle * circle ;
shape_draw ( & rect -> shape );
shape_draw ( & circle -> shape ); // file: OOC.h
struct Class {
size_t size ;
void * ( * ctor ) ( void * self , va_list * app );
};
void * new ( const void * _class , ...) {
const struct Class * class = _class ; // assign the address of `struct String` class
void * p = calloc ( 1 , class -> size ); // allocate the sizeof(struct String);
assert ( p );
* ( const struct Class * * ) p = class ; // Force the conversion of p and set the argument `class` as the value of this pointer.
if ( class -> ctor ) {
va_list ap ;
va_start ( ap , _class );
p = class -> ctor ( p , & ap ); // Now what is `p` here, a `struct String` or `struct Class`.
// and if it is `struct Class` then how it convert to `struct String` in `String_ctor` function
// given below.
va_end ( ap );
}
return p ;
}
// file: mystring.h
#include "OOC.h"
struct String {
const void * class ; // must be first
char * text ;
};
static void * String_ctor ( void * _self , va_list * app ) {
struct String * self = _self ;
const char * text = va_arg ( * app , const char * );
self -> text = malloc ( strlen ( text ) + 1 );
assert ( self -> text );
strcpy ( self -> text , text );
return self ;
}
// Initialization
static const struct Class _String = {
sizeof ( struct String ),
String_ctor
};
const void * String = & _String ;
// file: main.c
#include "mystring.h"
int main ( void )
{
void * a = new ( String , "some text" );
}Le référentiel contient un dossier par chaque modèle de conception.
OOP: http://www.codeproject.com/articles/22769/introduction-to-object-oried-programming-concep http://www.tutorialspoint.com/cplusplus/cpp_object_oriented.htm http://oopsconcepts.blogspot.ca/
ops -> vtable
t_ops -> half class level vtable
caps -> DI - callback (construct)
cbs -> DI - client/request callback (argument)
Les principes solides de la programmation orientée objet
Framework-lib coopérer avec le client:
Tous les commentaires constructifs sont les bienvenus. N'hésitez pas à vous débarrasser et à étendre existant ou à ajouter vos propres exemples et à envoyer une demande de traction avec vos modifications!
La licence du MIT (MIT)
Copyright (C) 2014 Wilson Huawen Yu
L'autorisation est accordée gratuitement à toute personne qui obtient une copie de ce logiciel et des fichiers de documentation associés (le "logiciel"), pour traiter le logiciel sans restriction, y compris sans limiter les droits d'utilisation, de copie, de modification, de fusion, de publication, de distribution, de sublince et / ou de vendre des copies des conditions suivantes.
L'avis de droit d'auteur ci-dessus et le présent avis d'autorisation sont inclus dans toutes les copies ou des parties substantielles du logiciel.
Le logiciel est fourni "tel quel", sans garantie d'aucune sorte, express ou implicite, y compris, mais sans s'y limiter, les garanties de qualité marchande, d'adéquation à un usage particulier et de non-contrefaçon. En aucun cas, les auteurs ou les titulaires de droits d'auteur ne seront pas responsables de toute réclamation, dommage ou autre responsabilité, que ce soit dans une action de contrat, de délit ou autre, découlant de, ou en relation avec le logiciel ou l'utilisation ou d'autres transactions dans le logiciel.
Pynsource - outil UML pour les modèles de conception python expliqués simplement
Modèles de conception .NET
Modèle de conception logicielle
Modèles de conception informatique