Introduction aux design patterns

15
Introduction aux design patterns Introduction aux design patterns Clément Simon 28 mars 2015

Transcript of Introduction aux design patterns

Page 1: Introduction aux design patterns

Introduction aux design patterns

Introduction aux design patterns

Clément Simon

28 mars 2015

Page 2: Introduction aux design patterns

Introduction aux design patterns

Sommaire

I Les origines

I Le singleton

Page 3: Introduction aux design patterns

Introduction aux design patterns

Origines

Qu’est-ce que c’est ?

"Ce sont des modèles théoriques adaptables qui résolvent unproblème précis."

Un schéma de conception nomme, décrit, explique et permetd’évaluer une conception d’un système extensible et réutilisabledigne d’intérêt pour un problème récurrent.

Page 4: Introduction aux design patterns

Introduction aux design patterns

Origines

Gang Of Four

Erich Gamma (JUnit , Eclipse)

Richard Helm (POO)

John Vlissides (John Vlissides Award)

Ralph Johnson (Smalltalk)

Page 5: Introduction aux design patterns

Introduction aux design patterns

Origines

Gang Of Four

Gang Of Four - Histoire

Thèse d’Erich Gamma sur les modèles début 1991

+ Richard Helm dans l’année

+ John Vlissides juste après

+ Ralph Johnson en 92⇒ En 93 : sorti du 1er catalogue.

⇒ Très longue gestation : "Ce livre a vu 4 pays, 3 mariages de sesauteurs, et la naissance de 2 enfants." Sortie en 1995.

Page 6: Introduction aux design patterns

Introduction aux design patterns

Origines

Les différents types

I Creational Patterns (Singleton, Abstract Factory, ...)I Structural Patterns (Adapter, Decorateur, ...)I Behavioral Patterns (Command, State, ...)

Page 7: Introduction aux design patterns

Introduction aux design patterns

Le singleton : un cas simple...

Le plus simple design pattern mais aussi le plus controversé.Avant C++11 :

Page 8: Introduction aux design patterns

Introduction aux design patterns

Le singleton : un cas simple...

Après C++11 :

Page 9: Introduction aux design patterns

Introduction aux design patterns

Le singleton : un cas simple...

Sans templates

class A{public:

static A& getInstance ();};

A& A :: getInstance (){static A instance;return instance ;

}

//Appel :A& sin=A:: getInstance ();

Page 10: Introduction aux design patterns

Introduction aux design patterns

Le singleton : un cas simple...

Avec templates

template <class T> class Singleton{public:

static T& getInstance ();};

class A : public Singleton <A>{};

Page 11: Introduction aux design patterns

Introduction aux design patterns

Le singleton : un cas simple...

Avec templates

template <class T> T& Singleton <T>:: getInstance (){static T instance;return instance ;

}

//Appel :A& sin=Singleton <A>:: getInstance ();

Page 12: Introduction aux design patterns

Introduction aux design patterns

Le singleton : sujet à débat !

Singleton VS Variable Globale

I "Le singleton est une variable globale"I "Le singleton est une variable globale déguisé"I "Le singleton introduit un état global, ce qui rend les variables

globales non populaire"I "Pourquoi l’état global est-il si mauvais ? -> Il rend le

programme impredictible."

Page 13: Introduction aux design patterns

Introduction aux design patterns

Le singleton : sujet à débat !

Singleton VS Variable Globale

Page 14: Introduction aux design patterns

Introduction aux design patterns

Conclusion

Heureusement que le GoF à écrit ce livre !

Les design patterns permettent de ne pas réinventer 100 fois unmême fonctionnement -> Réutilisabilité .

Le Singleton est plus fiable qu’une variable globale.

Page 15: Introduction aux design patterns

Introduction aux design patterns

Sources

Merci pour votre attention.

Sources :I Design Patterns - Elements of Reusable Object-Oriented

Software (Gang Of Four)I Cours d’Ingénierie logicielle[FMIN107] de C.Dony.I Présentation des principaux design patterns en C++

(http ://come-david.developpez.com/tutoriels/dps/)I Singletons en C++ (http ://h-deb.clg.qc.ca/Sujets/Divers–

cplusplus/CPP–Singletons.html)