Presentation is loading. Please wait.

Presentation is loading. Please wait.

(…A FEW OF THEM) C++ DESIGN PATTERNS. WHAT ARE THEY? Commonly occurring constructs Could be part of good software engineering Not universally agreed Good.

Similar presentations


Presentation on theme: "(…A FEW OF THEM) C++ DESIGN PATTERNS. WHAT ARE THEY? Commonly occurring constructs Could be part of good software engineering Not universally agreed Good."— Presentation transcript:

1 (…A FEW OF THEM) C++ DESIGN PATTERNS

2 WHAT ARE THEY? Commonly occurring constructs Could be part of good software engineering Not universally agreed Good to at least know what they are. We’ll look at and (eventually) implement 3 Singletons Factories Observers In C++, these are often designed with templates

3 TEMPLATE REFRESHER

4 BASIC IDEA Generic [type] bits of code New variants are generated (by compiler) as needed Saves a huge amount of redundant coding / copy-paste Two variants that we’ll use: “Templatized classes” “Templatized functions” There are lots of other applications: template metaprogramming (aka C++ black-magic) Beyond the scope of this class;-) Much harder (imo) than “normal” C++ One important quirk: Templates are “expanded” at compile-time on per-file basis. You can’t put them in a.cpp file [unless you only need it there] It wouldn’t be available to other cpp files. This is especially ugly in templatized classes – we have to do all bodies inline.

5 EXAMPLE ONE We have a function, foo, that [currently] takes a double param. void foo(double param) { cout << “The value is: " << param << endl; } We want to do something like this for other types (strings, int’s, etc.) Side-note: int’s would be silently cast as doubles here. But for other types, we’d have to re-write the code.

6 TEMPLATES …or we can write a templatized function template // “class T” is also OK void func(T param) { cout << "Using general template definition: " << param << endl; } Now this code will be re-generated as needed Example: func(5); // or func (5); func(3.7f); // generates float version func(3.7); // generates double version func("abc"); // generates const char* version func(std::string("abc")); // generates std::string version

7 TEMPLATE SPECIALIZATION Suppose we want to do something slightly different for selected type We need a template specialization template<> // marks the following as a template specialization void func (std::string s) // Note the <> here too { cout << "Using std::string specialization of func: " << s.length() << endl; } template<> void func (double d) { cout << "Using double specialization of func: " << (int)d << endl; }

8 SINGLETONS

9 DESCRIPTION Goals: Ensure we have one and only one instances of a class Allow (controlled) global access Why? Great for “managers” E.g. TextureManager in Ogre Manages memory and gives references to texture data We wouldn’t want TextureManager A managing textures x,y,z and TextureManager B managing textures q, r, s…would we? Avoids some parameters passing: We don’t have to pass a pointer to the Texture Manager to use it.

10 IMPLEMENTATION OVERVIEW Have a static [class] attribute pointing to the instance Initially NULL In C++ this must be done in a.cpp file (somewhere) Template specialization is needed if Singleton is templatized template<> Foo* Singleton ::msSingleton = NULL; Set by constructor (it should be NULL – raise exception if not) We need to ensure the destructor is called only once [raise exception if not] Have a static method to get the singleton (pointer / reference) Actually create the singleton somewhere Pro / Con Discussion: Automatically create the instance…


Download ppt "(…A FEW OF THEM) C++ DESIGN PATTERNS. WHAT ARE THEY? Commonly occurring constructs Could be part of good software engineering Not universally agreed Good."

Similar presentations


Ads by Google