Реализации на Kotlin
Command
Общая реализация на языке Kotlin
fun interface ICommand {
operator fun invoke()
}class CopyCommand : ICommand {
override operator fun invoke() {
println("Copying document.")
}
}class PrintCommand : ICommand {
override operator fun invoke() {
println("Printing document.")
}
}class ScanCommand : ICommand {
override operator fun invoke() {
println("Scanning document.")
}
}class Invoker(var command: ICommand) {
fun execute() {
println("Executing...")
command()
}
}fun main() {
val copyCommand = CopyCommand()
val scanCommand = ScanCommand()
val printCommand = PrintCommand()
val printer = Invoker(copyCommand)
printer.execute()
printer.command = scanCommand
printer.execute()
printer.command = printCommand
printer.execute()
}Last updated
Was this helpful?