Scala设计模式UML图例和代码实现实战 行为模式--命令设计模式
在讨论命令设计模式时,通常会有几个对象,每个对象都有自己的特定角色:
1.命令:我们可以将其视为调用者调用的接口及其实现。{ MakeSandwichCommand,CleanUpCommand PourJuiceCommand}
2.Receiver:这是实际知道如何执行命令的对象。可以将此视为传递给命令的对象,然后在接口方法中使用。{Robot}
3.Invoker:它通过调用它们的接口方法来调用命令。正如我们前面提到的,它甚至可能不知道正在调用哪些命令。{RobotController}
4.客户端:它或多或少地指导使用调用者执行哪些命令。{RobotExample,RobotByNameExample}
既然我们知道了最重要的对象及其在命令设计模式中的作用,我们可以看一个例子。对于类图,我们假设我们有一个可以烹饪的机器人。
我们通过控制器连接它并向我们的机器人发送命令。事情很简单,但应该足以理解这种模式是如何工作的。这是类图:我们可以在这里快速识别RobotCommand接口及其实现是命令。接收器是Robot类,因为它知道如何运行发出给它的所有命令。
RobotController类是调用者。它不知道它执行什么类型的命令,只需在需要时运行它们。
我们没有在上图中显示我们的客户端类,因为它只是由运行代码的示例应用程序表示,该代码实现了先前显示的类图。
您可以很容易地看到,如果更改了代表上图的代码,它可以轻松添加多线程支持并支持撤消。
代码示例现在,是时候看到代表上一个图表的有趣部分 - 代码。与往常一样,我们将通过各个班级,并在必要时作出简要说明。
我们将要看的第一段代码是Robot类。我们已经提到它充当接收器并且知道如何执行某些特定功能:我们保持代码简单,并且方法只是向命令行打印不同的东西。接下来是具有不同实现的robot命令:
前面的代码绝对没有什么特别之处。它是一个简单的特征,由不同的类实现。它依赖于Robot接收器,它知道如何执行方法。
RobotController类是我们的调用者,并根据RobotCommand接口发出给它的命令。只要遵循接口,它就不需要知道它发出的命令。我们已经决定添加一些可以在以后用于回滚的命令的历史记录。调用程序的代码如下所示:Scala方式的命令设计模式命令设计模式是设计模式的另一个示例,与其他语言相比,它可以在Scala中以不同方式实现。我们将展示前面示例的另一个实现。这次,我们将使用该语言的by-name参数功能。它可以作为参数传递函数(我们之前已经看过的策略设计模式),但更详细。让我们看看它会是什么样子。
实际上不太需要改变应用程序。我们只重构并重命名了RobotController和RobotExample类。
这是前一个类,现在称为RobotByNameController:
class RobotByNameController {
val history = ListBuffer [()=> Unit]()def issueCommand(command:=> Unit):Unit = {command _ =:history command} d ef showHistory( ):Unit = {history.foreach(println)}
}
正如您所看到的,我们不会将一个实际的命令对象,而只是一个by-name参数传递给issueCommand方法。这个方法的作用是将调用推迟检索传递的值,直到实际需要它为止。为了使前面的代码工作,我们还必须重构我们的示例代码:当我们不想为命令接口及其实现编写额外的代码时,by-name参数方法很有用。我们可以直接传递任何函数调用(在这种情况下,直接来自接收器),它将一直保持到需要数据或根本不调用。输出将与以前相同,但现在我们有功能,历史打印输出看起来会略有不同。
对于我们想要因某种原因而延迟,记录或排序方法调用的情况,命令设计模式非常有用。
case class Robot() {
def cleanUp(): Unit = System.out.println("Cleaning up.")
def pourJuice(): Unit = System.out.println("Pouring juice.")
def makeSandwich(): Unit = System.out.println("Making a sandwich.")
}
import scala.collection.mutable.ListBuffer
trait RobotCommand {
def execute(): Unit
}
case class MakeSandwichCommand(robot: Robot) extends RobotCommand {
override def execute(): Unit = robot.makeSandwich()
}
case class PourJuiceCommand(robot: Robot) extends RobotCommand {
override def execute(): Unit = robot.pourJuice()
}
case class CleanUpCommand(robot: Robot) extends RobotCommand {
override def execute(): Unit = robot.cleanUp()
}
class RobotController {
val history = ListBuffer[RobotCommand]()
def issueCommand(command: RobotCommand): Unit = {
command +=: history
command.execute()
}
def showHistory(): Unit = {
history.foreach(println)
}
}
class RobotByNameController {
val history = ListBuffer[() => Unit]()
def issueCommand(command: => Unit): Unit = {
command _ +=: history
command
}
def showHistory(): Unit = {
history.foreach(println)
}
}
object RobotExample {
def main(args: Array[String]): Unit = {
val robot = Robot()
val robotController = new RobotController
robotController.issueCommand(MakeSandwichCommand(robot))
robotController.issueCommand(PourJuiceCommand(robot))
System.out.println("I'm eating and having some juice.")
robotController.issueCommand(CleanUpCommand(robot))
System.out.println("Here is what I asked my robot to do:")
robotController.showHistory()
}
}
object RobotByNameExample {
def main(args: Array[String]): Unit = {
val robot = Robot()
val robotController = new RobotByNameController
robotController.issueCommand(MakeSandwichCommand(robot).execute())
robotController.issueCommand(PourJuiceCommand(robot).execute())
System.out.println("I'm eating and having some juice.")
robotController.issueCommand(CleanUpCommand(robot).execute())
System.out.println("Here is what I asked my robot to do:")
robotController.showHistory()
}
}
执行结果: