Ideas for simplifying design/removing a level of indirection in method calls

132 Views Asked by At

In the following bit of code below (full version of ideone) , the pointers to the methods Container::void updateFoo and Container::void updateBar are registered in a map m_updateMethod. These methods simply call Foo::update and Bar::update respectively.

Is there a way to avoid this extra level of indirection, by storing Foo::update and Bar::update?

class Foo
{
public:
    void update( )
    {
        std::cout << "Updating Foo ..." << std::endl;
    }
};

class Bar
{
public:
    void update( )
    {
        std::cout << "Updating Bar ..." << std::endl;
    }
};

class Container
{
public:
    typedef void (Container::*updateMethod)();
    typedef std::map<std::string , updateMethod> UpdateMethodMap;

    static const UpdateMethodMap    m_updateMethod;
    static UpdateMethodMap          initializeUpdateMethodMap();
    static updateMethod             getUpdateMethod( const std::string attributeName );

    void update( const std::string name )
    {
        updateMethod updater = getUpdateMethod( name );
        if( updater )
        {
            (this->*updater)();
        }
    }

    void updateFoo()
    {
        m_foo.update();
    }

    void updateBar()
    {
        m_bar.update();
    }

private:    
    Foo m_foo;
    Bar m_bar;
};
0

There are 0 best solutions below