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?

7
  • 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, 2022 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, 2022 at 15:10
  • I disagree. Stay as far away from object arrays as humanly possible.
    – Flater
    Jun 13, 2022 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, 2022 at 15:11
  • 1
    Alas, I left my sarcasm detector at home. The advice seemed genuine. Jun 13, 2022 at 15:12

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.