Bet way of registering targets on ApplicationCommandManager

In a program with many different ApplicationCommandTargets, how do you go about registering them with the central ApplicationCommandManager? I’ve never found a way that feels satisfactory.

(I’m talking about ApplicationCommandManager::registerAllCommandsForTarget() here).

There two obvious choices are:

  1. Register the targets in their respective constructors;
  2. Register the targets in the constructor of the class that holder the ApplicationCommandManager.

If you choose 1, then you end up re-registering the class every time you create a new instance of the class. In a class which you create and destroy often, this feels a bit gratuitous. (You also have to take a reference of the parent class which holds the ApplicationCommandManager, which seems like bad practice.)

If you choose 2, then you find yourself having to register a class that hasn’t been created yet. So then you have to create temporary objects, and wrestle with constructors that expect certain input that isn’t easily available.

Ideally I would want to register commands using a static method, since the commands typically don’t relate to the internal state of the object. But I can’t see any way of doing this, as the relevant methods in ApplicationCommandTarget are virtual.

Is there a third way? If not, is there a way of approaching 1 or 2 that makes them more viable?

Would like to know this as well. The interface of ApplicationCommandManager and ApplicationCommandTarget is very unclear in that regard. What is the recommended way of using these classes?

Would like to add another question:

What if you registerAllCommandsForTarget() and both targets register the same command with a different name? e.g. Select All changes name depending on the component that has focus (Select All Documents vs. Select All Assets).