Share PowerPoint. Anywhere!

Sablony

Uploaded from authorPOINT Lite
Download as Download Not Available PPT
Presentation Description

No description available

Views: 1
Like it  ( Likes) Dislike it  ( Dislikes)
Added: November 15, 2007 This presentation is Public
Presentation Category :Entertainment
Presentation StatisticsNew!
Views on authorSTREAM: 1
Presentation Transcript

Pokročilé návrhové vzory : Pokročilé návrhové vzory Martin Děcký 15.11.2007 3:54


Pokročilé návrhové vzory : Pokročilé návrhové vzory


Visitor : Visitor class Visitor { public: virtual void VisitFoo(Foo &) = 0; virtual void VisitBar(Bar &) = 0; }; class Foo { public: virtual void Accept(Visitor & visitor) { v.VisitFoo(*this); } };


Obecný Visitor : class BaseVisitor { public: virtual ~BaseVisitor() { } }; template class Visitor { public: typedef R ReturnType; virtual ReturnType Visit(T&) = 0; }; Obecný Visitor


Obecný Visitor : Obecný Visitor template class BaseVisitable { public: typedef R ReturnType; virtual ~BaseVisitable() { } virtual ReturnType Accept(BaseVisitor&) = 0; protected: template static ReturnType AcceptImpl(T& visited, BaseVisitor& guest) { if (Visitor* p = dynamic_cast*>(&guest)) return p->Visit(visited); return ReturnType(); } }; #define DEFINE_VISITABLE() \ virtual ReturnType Accept(BaseVisitor& guest) { \ return AcceptImpl(*this, guest); \ } class ConcreteVisitor : public BaseVisitor, public Visitor, public Visitor { public: void Visit(Foo &foo); void Visit(Bar &bar); };


Traits : Traits I::value_type value; // OK if I is a class int::value_type value; // not working int*::value_type value; // not working Řešení v STL Použití především pro iterátory, číselné typy (numeric_limits) apod. template struct iterator_traits { static const bool star = false; typedef typename Iterator::value_type value_type; typedef typename Iterator::pointer pointer; typedef typename Iterator::reference reference; }; template struct iterator_traits { static const bool star = false; typedef T value_type; typedef T* pointer; typedef T& reference; };


Curriously recurring template pattern : Curriously recurring template pattern class Base { public: void fnc() { implementation(); } virtual void implementation() = 0; }; class Derived : public Base { public: virtual void implementation() { std::cout << "Derived" << std::endl; } }; int main(int argc, char *argv[]) { Base *obj = new Derived; obj->fnc(); // "Derived" }


Curriously recurring template pattern : Curriously recurring template pattern template class Base { public: void fnc() { static_cast(this)->implementation(); } }; class Derived : public Base { public: void implementation() { std::cout << "Derived" << std::endl; } }; int main(int argc, char *argv[]) { Base *obj = new Derived; obj->fnc(); // "Derived" }


Použití CRTP : Použití CRTP


Policies : Policies


Ukázka použití policies : Ukázka použití policies template class Manager : public CreationPolicy, public ThreadPolicy { public: void fnc() { ThreadPolicy::Lock(); Foo *foo = CreationPolicy::Create(); ThreadPolicy::Unlock(); } }; class ThreadUnsafe { public: void Lock() { } void Unlock() { } }; class ThreadSafe { private: mutex_t mtx; public: void Lock() { mutex_lock(&mtx); } void Unlock() { mutex_unlock(&mtx); } }; template class CreateWithNew { public: static T* Create() { return new T; } protected: ~CreateWithNew() { /* Only derived classes can destroy policy */ } }; Manager, ThreadSafe> manager; manager.fnc();


Template template parameters : Template template parameters template