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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:00:32+00:00 2026-06-09T10:00:32+00:00

Lately I’ve been using some pattern quite a lot but I don’t know if

  • 0

Lately I’ve been using some pattern quite a lot but I don’t know if it is really good or not.

It goes as follows:
I have a set of function, lets call them ActionFoo, ActionBar and ActionZapper. These might differ in implementation but generally are used for same things across these. They may or may not be used together in a sequence(i.e. some of them can be used as a standalone), but there are some cases when they are, indeed grouped.

If I DO want to use them in a sequence I generally have two options:

1) write them manually each time

2) create a class hierarchy:

Approach #1:

void SomeActionSequence1()
{
    ActionFoo1(1);
    ActionBar1("Moo");
    ActionZapper1("Moo", 42);
}

void SomeActionSequence2()
{
    ActionFoo4(1);
    ActionBar2("Moo");
    ActionZapper1("Moo", 42);
}

This has drawbacks:

1) I won’t have an ability to store state and will have to pass a lot of parameters to these Actions

2) I won’t really have a coherent interface and won’t be able to easily use autocompletion

Approach #2

class Base
{
    public:
    Base(){}
    virtual ~Base(){}
    virtual void ActionFoo(int) = 0;
    virtual void ActionBar(string) = 0;
    virtual void ActionZapper(string, int) = 0;
    void ExecuteActionSequence();
};

void Base::ExecuteActionSequence()
{
    ActionFoo(1);
    ActionBar("Moo");
    ActionZapper("Moo", 42);
}

Derived1 : public Base
{
    void ActionFoo(int){/*some inplementation*/};
    void ActionBar(string){/*some inplementation*/};
    void ActionZapper(string, int){/*some inplementation*/};
}

Derived2 : public Base
{
    void ActionFoo(int){/*some inplementation*/};
    void ActionBar(string){/*some inplementation*/};
    void ActionZapper(string, int){/*some inplementation*/};
}

and use it kinda like this:

Base* actionSequence = new Derived1();
actionSequence->ExecuteActionSequence();

Correct virtuals will be used and all seems ok except 2 small things:

1) Extensibility – I will have to write a class for each complex action

2) More importantly – either a lot of functions will be duplicated between these classes or
I will have a hierarchical tree too complex on my hands

I kinda “circumvent” problems of both approaches with “Interface Object” pattern (note, the name is mine, maybe it has a proper one)

What I do is this:

class InterfaceClass
{
    public:
    InterfaceClass(){};
    ~InterfaceClass(){};

    void ActionFoo(int i)
    {
        if(fooPlaceholder != 0)
            fooPlaceholder(i);
    }
    void ActionBar(string str)
    {
        if(barPlaceholder != 0)
            barPlaceholder(str);
    }
    void ActionZapper(string str, int i)
    {
        if(zapperPlaceholder != 0)
            zapperPlaceholder(str, i);
    };
    void ExecuteActionSequence();

    std::function<void(int)> fooPlaceholder;
    std::function<void(string)> barPlaceholder;
    std::function<void(string, int)> zapperPlaceholder;
};

void InterfaceClass::ExecuteActionSequence()
{
    ActionFoo(1);
    ActionBar("Moo");
    ActionZapper("Moo", 42);
}

in my application I do:

InterfaceClass complexAction;
complexAction.fooPlaceholder = ActionFoo;
complexAction.barPlaceholder = ActionBar;
complexAction.zapperPlaceholder = ActionZapper;

complexAction.ExecuteActionSequence();

Note that ActionFoo, ActionBar and ActionZapper are free functions, but at the same time I am using them in an interface. Also – I can easily switch between implementations of these functions, even at runtime(If I need this).

The advantage of this approach is – there is no need to create separate class structures for new actions and there is no code duplication of Action* functions.
Also – all functions can be brought to scope only where the complexAction is initialized.

The disadvantages are, I think, that it is not obvious just which Action* function is being used in the InterfaceClass object. Also – there is no ability to dynamic_cast such a class to determine just what it is.

I highly suspect that these are not only disadvantages of such approach so I would like comments about that.

  • 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-09T10:00:35+00:00Added an answer on June 9, 2026 at 10:00 am

    It sounds like you want the Chain of Responsibility pattern

    abstract class Action {
        Action child;
        Action(Action child) { this.child = child; }
        Action() { }
    
        void doAction(StateContext context);
    
        void execute(StateContext context) {
           if (child) child.execute(context); 
           doAction(context); 
        } 
    }
    
    class ZapAction extends Action {
        ZapAction(String theString, int theValue, Action child) { ... }
        void doAction(Context context) { context.setZap(theString); } 
    }
    
    
    Action actionSequenceAlpha = new ZapAction("", 1, new FooAction()); 
    Action actionSequenceBeta = new FooAction(new BarAction(new ZapAction));
    

    Advantages – Don’t need to change this base object with a fixed set of strategies when you add a new Action, you can map actions in all sorts of fun and exciting ways, each object has a single responsibility and it is a nice standard pattern so everyone knows what is going on.

    The other option would be to separate the sequence from the Action. Have an Action interface with the three Actions inheriting it. Then have a Sequence class with an execute method and a List of Actions

    class Action { } 
    class FooAction extends Action { }
    
    class Sequence {
        List<Action> actions;
        void execute() {
           foreach (action : actions) action.execute();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lately I've been writing some JS code using jQuery and JavaScript as it is
Lately I've been favoring using named pipes (option --enable-named-pipes) in MySQL running on windows,
Lately, I have taken to the pattern of having a lot of diagnostic logging
Lately I've been thinking a lot about building a website/blog/community oriented site. However I
Lately, I've gotten some weird linker errors. I've been taught that there's two ways
Lately I've been running into some subtle layout issues in my iOS app. For
Lately we've been getting into a bit of co-development, and I don't think I'm
Lately, I have seen a lot of questions being asked about output for some
lately I have been writing a lot of tiny plugins for my web projects
Lately I have been finding that some of the ColdFusion applications on my production

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.