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 515005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:35:01+00:00 2026-05-13T07:35:01+00:00

I’m looking for the best way to dispatch objects to the correct target object.

  • 0

I’m looking for the best way to dispatch objects to the correct “target” object.

I have a base command class: Cmd, two sub-classes: BufferCmd and StateCmd. Command “GotoLine” is derived from BufferCmd and “ChangeCmd” is derived from StateCmd. BufferCmds are intended to go to a Buffer class and StateCmds are intended to go to a State object.

I currently have a Visitor pattern set up so that I can do something like:

Buffer buffer;
State state;

Cmd *c;
GotoLineCmd gotoCmd = new GotoLineCmd (15);
ChangeCmd changeCmd = new ChangeCommand (...)

c = &gotoCmd;
c->accept (buffer);
c = &changeCmd;
c->accept (state);

I want to use the Visitor pattern because I’d like to be able to do something roughly like:

Cmd *cmds [5];
cmds [0] = new GotoLineCmd (...); 
cmds [1] = new CopyLineCmd (...); 
cmds [2] = new PasteCmd (...); 

foreach (Cmd *c in cmds) {
    c->accept (buffer);
}

Unfortunately in order to use this I need do know what object to send the command to. I’d like to be able to do something like this:

Derive Buffer from Commandable
Derive State from Commandable

Commandables *commandables [1] = {new Buffer (), new State () };

// Then have the foreach type statement look like:
foreach (Cmd *c in cmds) {
    c->accept (commandables);
}

Is there a pattern that is most appropriate for this type of situation? Should I even be using the Visitor pattern? Obvious I want to avoid this:

foreach (Cmd *c in cmds) {
    foreach (Commandable *cmdAbles in commandables) {
        if (c->accept (commandables)) {
              // Okay command accepted... 
              break;
        }
    }
}

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-13T07:35:01+00:00Added an answer on May 13, 2026 at 7:35 am

    Sounds more like you want the aptly-named Command pattern.

    The key is to move the differing parameters of accept() into the constructor of each class derived from Cmd. For example, GotoLineCommand‘s constructor would take the line and the buffer objects as parameters to its constructor, and it would store a pointer or reference to the buffer object.

    Once you’ve done that, you no longer need the parameters to accept() and the interface is the same across all classes derived from Cmd

    class Buffer
    {
       public:
          void gotoLine(int line);
    };
    
    class Cmd
    {
       public:
          virtual void accept() = 0;
    };
    
    class GotoLineCommand: public Cmd
    {
       public:
          GotoLineCommand(Buffer & buffer, int line) :
             buffer_(buffer),
             line_(line)
          {
          }
    
          virtual void accept()
          {
             buffer_.gotoLine(line_);
          }
    
       private:
          Buffer & buffer_;
          int line_;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 304k
  • Answers 304k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In your Page_Load you need to check IsPostBack If it… May 13, 2026 at 8:48 pm
  • Editorial Team
    Editorial Team added an answer You can't use type qualifiers or static in size portion… May 13, 2026 at 8:48 pm
  • Editorial Team
    Editorial Team added an answer You can set Response.BufferOutput = false or you can call… May 13, 2026 at 8:48 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.