mardi 9 mai 2023

how to implement the "history" command in the "command" pattern?

I am new to programming. I am writing a small application in Java using the design pattern "command".

  1. I have created a main class "App" which plays the role of a client. "App" creates commands and sends them to the "Invoker" class.
  2. "Invoker" already runs commands and stores the history of the commands as an ArrayList.
  3. Running the command activates a method in the "Receiver", where the main execution of the command is defined.

I have a few questions:

  1. If it's not too hard to implement the "Do Something" command, how do you implement the "History" command? Does "Receiver" have to have a link to "Invoker"? Should "Invoker" catch the "History" command and, without running it, issue the request itself?

  2. Same question with the "Exit" command. Should "App" catch the "Exit" command by checking at the very beginning, or should the command go from "Invoker" through "Recevier" and back to "App"?

  3. I thought about endowing all classes with the "Receiver" interface, but I don't think that's a good idea. Is it worth doing this?

I've tried a lot of options, one of which is combining the Invoker and the Receiver. But the "Exit" command in this case will require combining and "App" class.

I also searched for complete examples of this pattern, but they all didn't use one or more classes.

I can make it all via one class, but I really want to understand this pattern and use all classes.

Aucun commentaire:

Enregistrer un commentaire