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"
Manager
holds a collection ofshared_ptr<Foo>
Manager
provides access to the collection by returningshared_ptr<Foo>
- I have a
Command
abstract class and a hierarchy of commands for actions to perform onFoo
- Client code should not call
Command::execute()
, onlyManager
should,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"
Manager
holds a collection ofshared_ptr<Foo>
Manager
provides access to the collection by returningshared_ptr<const Foo>
- I have a
Command
abstract class and a hierarchy of commands for actions to perform onFoo
- Client code cannot (without workarounds) call
Command::execute()
, onlyManager
can,Manager::execute(shared_ptr<Command>)
, so that it can handle undo/redo and get non-const smart pointers - A
Manager
must be able to allowCommand
objects to access and modifyshared_ptr<Foo>
even though the user initializesCommand
objecst 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?