2010-06-11 177 views
2

我想分解一个类,使其与执行特定任务的逻辑分离,以便用户可以根据需要编写新策略而不会干扰中央模型。所以,我想用模板策略类,但不必有策略的用户将被templatised:模板策略模式

class Model { 
    ... 
    boost::shared_ptr< Strategy <T> > m_pStrategy; 
    ... 
    public: 
    template < typename T > 
    void DoSomething() { m_pStrategy <T> ::DoSomething(); } 
    }; 

我想不会是模板的DoSomething的功能。有什么其他方法可以实现我想在这里做的事情吗?

谢谢。

+2

您正在参数化'DoSomething'中的* data member *。它甚至如何编译? “T”来自“策略< T >”哪里? – 2010-06-11 11:37:28

回答

7

在我看来,你想要实现的是一个Policy-Based Design。我不确定ModelStrategy是做什么的,但看起来好像是Model是根类,而Strategy是Policy类,在某些情况下,用户希望提供该类来执行特殊处理。它也似乎是你保持一个指向Strategy<T>对象的唯一原因是你可以调用它的函数。

在这种情况下,你可以设计你的类是这样的:

template<class Strategy> 
class Model : public Strategy { 
public: 
void DoSomething() 
{ 
    // magic happens (we will fill this in shortly) 
}; 
}; 

你呼吁Strategy类的方法做你的魔术。通过让用户定义他们自己的课程,让他们有机会定义他们自己的“魔力”。您需要应用规则,至少要提供什么方法​​,以便您可以调用Model中的这些方法。

例如,假设Model实际上是某种资源管理器,能够成为简单的智能指针,或者其他类似Windows关键部分的资源管理器。我们将Model重命名为auto_resourceStrategy将变为release_policy,并将负责释放分配给它的任何资源。在这种情况下,你可能有:

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_; 
}; 

,你可以使用std::string指针这样的:

typedef auto_resource<std::string*, pointer_release_policy> string_ptr; 
string_ptr my_str; 

...和my_str脱落的堆栈时,该release方法会自动被调用。

以后要添加新的政策发布的Windows互斥HANDLE S:

class handle_release_policy 
{ 
public: 
    template<class Handle> void release(Handle h) 
    { 
    CloseHandle(h); // this is a WINAPI function that deallocates the specified resource 
    }; 
}; 

您可以使用此正是如此:

typedef auto_resource<HANDLE, handle_resource_policy> handle_resource; 
//... allocate & use the mutex... 
handle_resource mutex = CreateMutex(0, 0, 0); 

当然,为了充实这一切了你需要添加分配,复制,释放资源等功能。这是一个完整的工作示例,它将所有内容放在一起我提供了2套的政策,一个用于Windows CRITICAL_SECTION s,而另一个用于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; 

更多基于策略的设计,见Modern C++ Design

...

+0

谢谢你的回答... :) – SWKK 2010-06-11 14:54:59

+0

@SWKK:我的荣幸,享受。 – 2010-06-11 15:00:45

+0

已经过了2年..但这是好东西。让我问你一下。 _strategy pattern_与_policy based pattern_有什么不同?我从你的代码中看到的一件事是一个不同背景的主题,其中有非常相似的策略算法。套接字,指针和HANDLE发布策略都适用于不同的上下文:_sockets指针和Handles_。 **我期待的**设计模式是当上下文不变时,但算法/协议是唯一的。所以也许有两个distink需要**策略模式的许多上下文**以及**策略模式的许多原语** – jaybny 2012-08-02 04:05:41

0

将功能移出class Strategy<T>