I am trying to implement a very clean Command Pattern in a library.
I have the following structure right now (a few parts are still being finished up):
- users (client-code) have some Object, call it "Manager"
Managerholds a collection ofshared_ptr<Foo>Managerprovides access to the collection by returningshared_ptr<Foo>- I have a
Commandabstract class and a hierarchy of commands for actions to perform onFoo - Client code should not call
Command::execute(), onlyManagershould,Manager::execute(shared_ptr<Command>), so that it can handle undo/redo
I would like to follow the following rules:
- users (client-code) have some Object, call it "Manager"
Managerholds a collection ofshared_ptr<Foo>Managerprovides access to the collection by returningshared_ptr<const Foo>- I have a
Commandabstract class and a hierarchy of commands for actions to perform onFoo - Client code cannot (without workarounds) call
Command::execute(), onlyManagercan,Manager::execute(shared_ptr<Command>), so that it can handle undo/redo and get non-const smart pointers - A
Managermust be able to allowCommandobjects to access and modifyshared_ptr<Foo>even though the user initializesCommandobjecst withshared_ptr<const Foo>
I am just trying to figure out the best way to handle giving out shared_ptr<const Foo> while allowing number 5 and 6 to work.
Is there any example/design pattern that does this which I could learn from? Is this a good idea compared to what I already have/am working on?