This shows you the differences between two versions of the page.
designpatterns [2015/03/25 16:25] |
designpatterns [2022/04/16 12:22] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ===== Reference ===== | ||
+ | |||
+ | * design pattern matrix | ||
+ | * http://www.netobjectives.com/download/dpmatrix.pdf | ||
+ | * http://uml.org.cn/sjms/pdf/dpmatrix.pdf | ||
+ | * http://sourcemaking.com/design_patterns# CPlusPlus | ||
+ | * [[Livre]] : http://rzr.online.fr/isbn/2841773507 # Design patterns Tête la première (Broché) fr | ||
+ | * https://github.com/jwasham/coding-interview-university#dont-feel-you-arent-smart-enough# LearN | ||
+ | * https://www.fluentcpp.com/2020/12/18/on-design-patterns-in-cpp/# | ||
+ | |||
+ | |||
+ | ===== TEMPLATES ===== | ||
+ | |||
+ | // file:///~/singleton.hpp | ||
+ | template <class T> | ||
+ | class Singleton | ||
+ | { | ||
+ | public: | ||
+ | static T* get() { | ||
+ | if ( instance == 0 ) instance = new T; | ||
+ | return instance; | ||
+ | } | ||
+ | protected: | ||
+ | Singleton() { } | ||
+ | ~Singleton() { } | ||
+ | private: | ||
+ | static T* instance; | ||
+ | }; | ||
+ | |||
+ | template<class T> | ||
+ | T* Singleton<T>::instance = NULL; // here the tricky part | ||
+ | |||
+ | // file:///~/main.cpp | ||
+ | #include <string> | ||
+ | using namespace std; | ||
+ | #include "singleton.hpp" | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | Singleton<string>::get(); | ||
+ | return EXIT_SUCCESS; | ||
+ | } | ||
+ | |||
+ | |||
+ | Note: [[Microsoft]] msvs allows default types , not GCC so avoid those : | ||
+ | template<class T = char> //... | ||
+ | |||
+ | |||
+ | ===== TEMPLATES & DESIGN PATTERN ===== | ||
+ | |||
+ | what do u think of this weird code : | ||
+ | |||
+ | /// Design Pattern (with templates) | ||
+ | template <class T> | ||
+ | class VisitorAble { | ||
+ | public: | ||
+ | virtual int visit(T & in_data)=0; | ||
+ | }; | ||
+ | |||
+ | template <class T> | ||
+ | class AcceptAble { | ||
+ | public: | ||
+ | virtual int accept(T & in_visitor) { in_visitor.visit( *this ); } | ||
+ | }; | ||
+ | |||
+ | class Visitor : public Visitor<Data> { public: ... visit ... }; | ||
+ | |||
+ | class Data : public AcceptAble<VisitorAble<Data> > {}; | ||
+ | |||
+ | // recursive template ? | ||
+ | |||
+ | |||
+ | ===== MORE ===== | ||
+ | |||
+ | @TaG: [[Design]] [[Pattern]] [[C++]] [[CPlusPlus]] [[templates]] [[DP]] [[pointers]] [[OOP]] [[STL]] [[auto_ptr]] [[boost]] | ||