Created
March 6, 2022 12:38
-
-
Save rommansabbir/4ad970110ed2532c0f7bea50eca733ff to your computer and use it in GitHub Desktop.
Command Design Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CommandPatternExample { | |
companion object { | |
@JvmStatic | |
fun main(args: Array<String>) { | |
val remoteControl: BaseRemoteControl = RemoteControl() | |
println("-----Testing onButtonPressed on RemoteControl for Car-----") | |
val car = Car() | |
val carMoveCommand: Command = CarMoveCommand(car) | |
remoteControl.onButtonPressed(carMoveCommand) | |
println("-----Testing offButtonPressed on RemoteControl for Car-----") | |
val carStopCommand: Command = CarStopCommand(car) | |
remoteControl.offButtonPressed(carStopCommand) | |
println("-----Testing undoButtonPressed() on RemoteControl for Car-----") | |
remoteControl.undoButtonPressed() | |
println("-----Testing onButtonPressed on RemoteControl for RotatingTop-----") | |
val top = RotatingTop() | |
val topRotateCommand: Command = TopRotateCommand(top) | |
remoteControl.onButtonPressed(topRotateCommand) | |
println("-----Testing offButtonPressed on RemoteControl for RotatingTop-----") | |
val topStopRotateCommand: Command = TopStopRotateCommand(top) | |
remoteControl.offButtonPressed(topStopRotateCommand) | |
println("-----Testing undoButtonPressed on RemoteControl for RotatingTop-----") | |
remoteControl.undoButtonPressed() | |
} | |
} | |
} | |
/** | |
* Base for Remote Control System. | |
* [execute] to execute some operation. | |
* [undo] to undo recent operation. | |
*/ | |
interface Command { | |
fun execute() | |
fun undo() | |
} | |
/** | |
* Car class with some basic features [move] and [stop]. | |
*/ | |
class Car { | |
fun move() = println("Car is moving") | |
fun stop() = println("Car has stopped") | |
} | |
/** | |
* RotatingTop class with some basic features [startRotating] and [stopRotating]. | |
*/ | |
class RotatingTop { | |
fun startRotating() = println("Top has start rotating") | |
fun stopRotating() = println("Top has stopped rotating") | |
} | |
/** | |
* Concrete class of [Command], implement (Car move and stop) the operations for [execute] and [undo]. | |
* | |
* @param car [Car] object. | |
*/ | |
class CarMoveCommand(private val car: Car) : Command { | |
override fun execute() { | |
println("CarMoveCommand.execute(): Invoking move() on Car") | |
car.move() | |
} | |
override fun undo() { | |
println("CarMoveCommand.undo(): Undoing previous action->Invoking stop() on Car") | |
car.stop() | |
} | |
} | |
/** | |
* Concrete class of [Command], implement (Car stop and move) the operations for [execute] and [undo]. | |
* | |
* @param car [Car] object. | |
*/ | |
class CarStopCommand(private val car: Car) : Command { | |
override fun execute() { | |
println("CarStopCommand.execute(): Invoking stop() on Car") | |
car.stop() | |
} | |
override fun undo() { | |
println("CarStopCommand.undo(): Undoing previous action-> Invoking move() on Car") | |
car.move() | |
} | |
} | |
/** | |
* Concrete class of [Command], implement (Rotating start or stop) the operations for [execute] and [undo]. | |
* | |
* @param rotatingTop [RotatingTop] object. | |
*/ | |
class TopRotateCommand(private var rotatingTop: RotatingTop) : Command { | |
override fun execute() { | |
println("TopRotateCommand.execute(): Invoking startRotating() on RotatingTop") | |
rotatingTop.startRotating() | |
} | |
override fun undo() { | |
println("TopRotateCommand.undo(): Undoing previous action->Invoking stopRotating() on RotatingTop") | |
rotatingTop.stopRotating() | |
} | |
} | |
/** | |
* Concrete class of [Command], implement (Rotating stop or start) the operations for [execute] and [undo]. | |
* | |
* @param rotatingTop [RotatingTop] object. | |
*/ | |
class TopStopRotateCommand(private var rotatingTop: RotatingTop) : Command { | |
override fun execute() { | |
println("TopStopRotateCommand.execute(): Invoking stopRotating() on RotatingTop") | |
rotatingTop.stopRotating() | |
} | |
override fun undo() { | |
println("TopStopRotateCommand.undo(): Undoing previous action->Invoking startRotating() on RotatingTop") | |
rotatingTop.startRotating() | |
} | |
} | |
/** | |
* Base Remote Control features. | |
*/ | |
interface BaseRemoteControl { | |
fun onButtonPressed(onCommand: Command) | |
fun offButtonPressed(offCommand: Command) | |
fun undoButtonPressed() | |
} | |
/** | |
* Implementation of [BaseRemoteControl]. | |
*/ | |
class RemoteControl : BaseRemoteControl { | |
private var onCommand: Command? = null | |
private var offCommand: Command? = null | |
private var undoCommand: Command? = null | |
override fun onButtonPressed(onCommand: Command) { | |
this.onCommand = onCommand | |
onCommand.execute() | |
undoCommand = onCommand | |
} | |
override fun offButtonPressed(offCommand: Command) { | |
this.offCommand = offCommand | |
offCommand.execute() | |
undoCommand = offCommand | |
} | |
override fun undoButtonPressed() { | |
undoCommand?.undo() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment