0

I have three layers. I wish to reduce coupling between them and other modules. I wish to use the command pattern between all layers. A command should be able to be passed from one layer to another. For this, all layers must have implementations of the same set of interfaces, for example the command interface.

I'd prefer if the layers do not depend on some global module that defines shared classes and interfaces. Is this unrealistic? Is there a neat manner in which multiple layers can implement the same interfaces without depending on some global module at runtime?

reign
  • 25
  • 3
  • 1
    This seems to be a problem of your own design. Either pass it through and deal with needing a shared contract, or don’t and deal with mapping one layer to the next. – Flater Jun 13 '22 at 15:10
  • Use `Maps` or object arrays. Or serialize inputs and outputs to byte[] or strings. But then you need serializer and deserializers. – Laiv Jun 13 '22 at 15:10
  • I disagree. Stay as far away from object arrays as humanly possible. – Flater Jun 13 '22 at 15:11
  • It was pure sarcasm. the kind of decoupling he looks for is unpractical. The thing is sooner or later he will need to make something to map inputs and outputs and he'll realise that whatever he does, needs to be reusable. – Laiv Jun 13 '22 at 15:11
  • 1
    Alas, I left my sarcasm detector at home. The advice seemed genuine. – Robert Harvey Jun 13 '22 at 15:12
  • Do these layers all run in the same JVM? If so, the problem seems trivial. If not, in what form are the commands passed between layers? – David Conrad Jun 13 '22 at 15:13
  • All layers are in the same JVM. I guess I'll just have shared interfaces. – reign Jun 13 '22 at 15:19

1 Answers1

2

Anywhere you wish to pass a command to be used (by an Invoker) must know how to call execute() on that command. So it must have the Command interface.

Anything you wish the command to call action() on must provide the command an interface (Receiver) to call.

Anything you wish to only take, hold, or pass Command objects doesn't care about any of these interfaces. You're free to pass around Command references as you like. So long as they end up somewhere that knows what to do with them nothing in between needs to care. That's some extremely loose coupling right there.

Given all that, the Command Pattern doesn't really care about your layers. So it'll be up to you to keep your layers focused on whatever their respective jobs are.

candied_orange
  • 102,279
  • 24
  • 197
  • 315