The command design pattern the Scala way

The command design pattern is another example of a design pattern that can be implemented differently in Scala compared to other languages. We will show another implementation of the preceding example. This time, we will use the by-name parameters feature of the language. It is replaceable with passing functions as parameters (something we've already seen before for the strategy design pattern), but more verbose. Let's see what it will look like.

Not much of the application actually has to change. We've only refactored and renamed the RobotController and RobotExample classes. Here is the former class, now called RobotByNameController:

class RobotByNameController {  val history = ListBuffer[() => Unit ...

Get Scala Design Patterns - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.