It seems to me that you want to implement Policy-Based Design . I'm not sure what to do Model and Strategy exactly, but it looks like Model is the root class, and Strategy is the Policy class that users would like to provide in some cases for special processing. It seems that the only reason you keep a pointer to the Strategy<T> object, this is the ability to call functions on it.
In this case, you can create your class as follows:
template<class Strategy> class Model : public Strategy { public: void DoSomething() {
You call the methods of the Strategy class to do your magic. By allowing users to define their own Strategy class, you are giving them the opportunity to define their own "magic." You need to apply the rules about which method the Strategy class will provide at a minimum so that you can call these methods in Model .
For example, suppose Model is actually a kind of resource manager, capable of being a simple smart pointer or another, like a resource manager for a critical Windows section. Let's rename Model to auto_resource , and Strategy will become release_policy and will be responsible for freeing any resource assigned to it. In this case, you may have:
class pointer_release_policy { public: template<class Object> void release(Object* obj) { delete obj; } }; template<class Managed, class release_policy> class auto_resource : public release_policy { public: // ... ctors etc defined here ~auto_resource() { release_policy::release(managed_); } private: Managed managed_; };
What you can use for std::string pointers like this:
typedef auto_resource<std::string*, pointer_release_policy> string_ptr; string_ptr my_str;
... and when my_str falls off the stack, the release method is automatically called.
Later, you want to add a new policy for the release of the Windows HANDLE s mutex:
class handle_release_policy { public: template<class Handle> void release(Handle h) { CloseHandle(h);
You can use it as follows:
typedef auto_resource<HANDLE, handle_resource_policy> handle_resource;
Of course, in order to make full use of all this, you need to add functionality for assignment, copying, release, etc. resources. Here is a complete working example that brings everything together. I have provided two sets of policies, one for Windows CRITICAL_SECTION s and one for SOCKET s:
class SimpleCopyPolicy { public: template<class Resource> Resource copy(const Resource& rhs) const { Resource ret = rhs; return ret; } protected: ~SimpleCopyPolicy(){}; }; class CritsecReleasePolicy { public: template<class Handle> bool release(Handle& h) { DeleteCriticalSection(&h); return true; } protected: ~CritsecReleasePolicy() {}; }; class CritsecLockPolicy // CRITICAL_SECTION lock/unlock policies { public: template<class Handle> bool lock(Handle& h) { EnterCriticalSection(const_cast<CRITICAL_SECTION*>(&h)); return true; } template<class Handle> bool unlock(Handle& h) { LeaveCriticalSection(&h); return true; } }; class SocketReleasePolicy { public: template<class Handle> bool release(Handle h) { return 0 != closesocket(h); } protected: ~SocketReleasePolicy(){}; }; template<class Resource, typename ReleasePolicy, typename CopyPolicy = SimpleCopyPolicy> class simple_auto_resource : public ReleasePolicy, public CopyPolicy { public: typedef simple_auto_resource<Resource,ReleasePolicy,CopyPolicy> base_type; simple_auto_resource() : res(0) {} simple_auto_resource(const Resource & r) : res(copy(r)) {} ~simple_auto_resource() { if(res) release(res); } void clear() { if(res) release(res); res = 0; } Resource& get() { return res; } const Resource& get() const { return res; } Resource detach() { Resource ret = res; res = 0; return ret; } operator const Resource&() const { return get(); } operator Resource&() { return get(); } base_type& operator=(const Resource& rhs) { clear(); res = copy(rhs); return * this; } template<class Comp> bool operator==(const Comp& rhs) const { return res == (Resource)rhs; } template<class Comp> bool operator!=(const Comp& rhs) const { return res != (Resource)rhs; } template<class Comp> bool operator<(const Comp& rhs) const { return res < (Resource)rhs; } private: Resource res; }; typedef simple_auto_resource<CRITICAL_SECTION, CritsecReleasePolicy> auto_critsec; typedef simple_auto_resource<SOCKET,SocketReleasePolicy> auto_socket;
For more on policy-based development, see C ++ Modern Design .
...