I have several sets of actions (e.g., copy, paste, undo, redo, show dockable window XYZ, zoom, etc.) that I don't want to duplicate in multiple locations but that are shared by different parts of the GUI like the main menu, a toolbar, and right-click menus.
What is the best way to share them? I'm using Qt 5.3 with C++, but this is mostly independent from any specific GUI framework or language.
Some possibilities:
Designate one central place, say the main window, to create all of them with their text, icon, and callback. Then:
Pass in the actions to the constructor when creating the subcomponents of the GUI. This can make the constructor param lists rather long.
Call setters on the subcomponents of the GUI after the subcomponent is constructed and pass in all necessary actions. This makes the constructor shorter but isn't much prettier in the end.
Supply getters from the main window and have the subcomponents get the actions they want. The subcomponents generally have a pointer to the main window already. This makes it so the main window is ignorant of who cares about which action, but it also exposes a bunch of public members (unless I use the Attorney-Client idiom or similar).
Add them to a separate, global-ish repository where the main window adds them and users look them up by name or key or something, as needed. This is similar to the other options but separates the concerns a little better and exposes only a single parameterized getter rather than a bunch of specific getters. Downside: This adds a global-ish object that everyone accesses.
Define the actions in the "main" use case, say, the main menu, and then have getters for everyone else. This locates them in one place and would mean the main window would need to supply a single getter function for the main menu instead. But it still exposes a bunch of internal members as public.
What's the best approach? Is there something better that I haven't listed here?