Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 872545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:47:55+00:00 2026-05-15T10:47:55+00:00

I want to breakup a class so that its decoupled from the logic of

  • 0

I want to breakup a class so that its decoupled from the logic of performing certain task so that users can write new strategies as they wish to without interfering with the central model. So, I want to use templated strategy class but without having to have the user of the strategy to be templatised:

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

I would like the DoSomething function to not be templated. Is there any other way I can achieve what I want to do here?

thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:47:56+00:00Added an answer on May 15, 2026 at 10:47 am

    It seems to me like what you want to implement is a Policy-Based Design. I’m not sure what Model and Strategy do exactly, but it seems like Model is the root class, and Strategy is the Policy class, which users would want to provide in some cases to perform special handling. It also seems like the only reason you keep a pointer to the Strategy<T> object around is so that you can call functions on it.

    In this case, you can design your class like this:

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

    You call methods on the Strategy class to do your magic. By letting the users define their own Strategy class, you give them the opportunity to define their own “magic”. You need to apply rules on what method the Strategy class will provide at a minimum, so that you can call those methods in Model.

    For example, suppose Model is actually some kind of resource manager, capable of being a simple smart pointer, or something else like a resource manager for a Windows Critical Section. Let’s rename Model to auto_resource and Strategy will become release_policy and will be responsible for releasing whatever resource was assigned to it. In that case, you might 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_;
    };
    

    Which you could 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 will automatically be called.

    Later you want to add a new policy for releasing Windows mutex HANDLEs:

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

    You can use this thusly:

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

    Of course, in order to flesh all this out you need to add functionality for assigning, copying, releasing etc the resources. Here is a complete working example that puts everything together. I’ve provided 2 sets of policies, one for Windows CRITICAL_SECTIONs, and another for SOCKETs:

    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 design, see Modern C++ Design.

    …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find a way to write some unit-tests that can be used
want to have a Hyperlink-Button in a gridView in which I can display a
Want to send the text from my current vb application to the Active Window
want to fetch the value from dynamically created textfield with tag from NSMutableArray.... Txt_New_Estimated
Want to develop a query refinement tool just like Google Suggest so that i
I have a Kohana 3.0.14 website that i want to put on multiple domains,
I have a PROGRAMMING task (homework) to investigate brute force neural net methods. That
I want user to enter a sentence then I break up that sentence into
A client has given me a task to do that I've not done before,
I am very new programmer to Java regular expressions. I do not want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.