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

  • SEARCH
  • Home
  • 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 8586025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:12:36+00:00 2026-06-11T22:12:36+00:00

The OutputPolicy of my class sometimes goes to a static/singleton object and sometime goes

  • 0

The OutputPolicy of my class sometimes goes to a static/singleton object and sometime goes to a 1-to-1 object.

Not sure if I can explain in english.. so here is the desired behavior in pseudo code:

template< 
    class Algo,
    template<class> OutputPolicy
>
class AlgoBase : Algo, OutputPolicy<Algo>
{
};

class AlgoImp
{
public:
    AlgoImp(string topic) {} 
    void OnData(
    {
        cout << " AlgoImp::OnData";
        if ( .... ) 
            NewMsg(msg);
    }
};

class AlgoImp2
{
public:
    AlgoImp2(string topic) {} 
    void OnData(
    {
        cout << " AlgoImp2::OnData";
        if ( .... ) 
            NewMsg(msg);
    }
};

AlgoImp::OnData, does some processing and calls NewMsg. I am trying to decouple the implimentation of the NewMsg() call.

template < class T>
class OutMessageQueueTcp
{
    void NewMsg(string in)
    {
        //Should be a signgleton or static
        cout << " OutMessageQueueTcp::NewMsg";
    }
};

If OutputPolicy is OutMessageQueueTcp (above) then there should only be one instance of OutputPolicy. So I would either have to derive from a Singleton or use a static variable somewhere.

template <class T>
class OutMessageAsInput : AlgoBase<AlgoImp2, OutMessageQueueTcp>
{ 
    void NewMsg(string in)
    {
        cout << " OutMessageAsInput::OnData";
        AlgoBase<AlgoImp2, OutMessageQueueTcp>::NewMsg(in);
    }
};

If OutputPolicy is OutMessageAsInput (above) then the out becomes input to a sister object and then will be sent to OutMessageQueueTcp.

Here is the main.

main 
{
    AlgoBase<AlgoImp, OutMessageAsInput> myalgo1;
    myalgo1.OnData("hi");
}

And my desired output:

AlgoImp::OnData
OutMessageAsInput::NewMsg 
AlgoImp2::OnData
OutMessageQueueTcp::NewMsg

What I really have is a collection of AlgoImp objects, each with its own topic and data. AlgoImp will generate outout by calling NewMsg. Sometimes these messages should be sent to a Socket, and sometimes they should be processed by AlgoImp2.

This decision will happen at compile time. There will be many Algo’s each doing diff things. Seems like I am trying to do some kind of Aspect oriented stuff.

Question:

Is it possible to have a Base class be either a singleton or a normal object by making it a policy? Is there a design pattern for something like this? Maybe some kind of factory?

Thanks for the help.

  • 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-06-11T22:12:37+00:00Added an answer on June 11, 2026 at 10:12 pm

    To keep things simple – OutputPolicy at your algo classes level should be a simple class with simple method. Of course you can implement it with singleton or static methods from the real implementation class:

    template < class T>
    class OutMessageQueueTcp
    {
    public:
        void NewMsg(string in)
        {
           impl.NewMsg(in);
        }
    private:
       static OutMessageQueueTcpImpl<T> impl;
    };
    

    Or:

    template < class T>
    class OutMessageQueueTcp
    {
    public:
        void NewMsg(string in)
        {
           OutMessageQueueTcpImpl<T>::NewMsg(in);
        }
    };
    

    For your Algo* classes: if output policy is given at compile time – make all these classes template clasess, and treat OutputPolicy as a Strategy – keeo it aggregated (as member variable) in your algo classes:

    template <template <typename> class OutputPolicy>
    class AlgoImp
    {
    public:
        AlgoImp(string topic) {} 
        void OnData(
        {
            cout << " AlgoImp::OnData";
            if ( .... ) 
                outputPolicy.NewMsg(msg);
        }
    private:
       OutputPolicy<AlgoImp> outputPolicy;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have that template class that uses a policy for it's output and another
I installed kvm and set several guests on a server using vmbuilder. Here is
I have a MySQL instance running on a Debian server and I can connect
Greeting all, iptables -L gives the following output [root@ibmd ~]# iptables -L Chain INPUT
I use iptables (PREROUTING) to redirect all TCP Traffic to a local port. Now
I would like to delete POSTROUTING rule below, [root@hostname ~]# service iptables status Table:
I have one Ubuntu Based memcache server. Now i want my new server to
I have been using an Ubuntu 8.04 server with fail2ban for a while now
I ran some node.js tests on my windows machine and everything worked well. Now
os centos 6.3 Nginx doesn't work in local network, but it works in localhost.

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.