update: I wrote a second approach to this pattern(although the full explanation and an interesting implementation is here, so I recommend reading it too)
Following the series “Gang of Four Design Patterns” in Scala (after previous episodes Visitor and Chain Of Responsibility), here I propose my implementation of the Command pattern. If you are not familiar with it, Wikipedia entry is a good place to start, as well as this Carlos Caballero post
This pattern involves quite a lot of communication among the different actors (objects in the original proposal) so it has been difficult to implement it following a functional approach. In fact, I think that my implementation is not functional at all, so I am completely open to suggestions and improvements!
The interesting point of this pattern is how a Command wraps one of more actions, and then decouples who will execute the actions (the Receiver), who executes the commands (the client) and who determines which receiver will execute the actions (the Invoker). This provides a great flexibility and simplifies the addition of new commands.
From my implementation, I’d like to point out that the commands are represented with objects and not with case classes as could be expected. This way they are all singletons and a single instance is created on the application. Also, this allows to create new commands just by extending Command trait.
In this case, Increment command calls a single action from the Receiver, but DecrementALot calls two. This is to show that there is no need to have a one to one equivalence between the Receiver and the Commands.
Also, Receiver represents the internal status of the application and it can evolve along time. This is why I used a var to model the state.
Altogether, the implementation of this pattern has been way more challenging that I expected and having a fully functional version still remains an open problem for me.