Command design Pattern in Kotlin | Design Patterns

In command pattern, the request is send to the invoker and invoker pass it to the encapsulated command object. Command object passes the request to the appropriate method of Receiver to perform the specific action. The client program create the receiver object and then attach it to the Command. Then it creates the invoker object and attach the command object to perform an action. Now when client program executes the action, it’s processed based on the command and receiver object.


package behavioral
interface DoorCommand {
    fun execute()
}
class OpenCommand(val doors: String) : DoorCommand {
    override fun execute() = println("Opened $doors")
}
class CloseCommand(val doors: String) : DoorCommand {
    override fun execute() = println("Closed $doors")
}
class HAL9000DoorsOperations(doors: String) {
    val openCommand: DoorCommand = OpenCommand(doors = doors)
    val closeCommand: DoorCommand = CloseCommand(doors = doors)
    fun close() = closeCommand.execute()
    fun open() = openCommand.execute()
}
fun main(args: Array<String>) {
    val podBayDoors = "Pod Bay Doors"
    val doorModule = HAL9000DoorsOperations(doors = podBayDoors)
    doorModule.open()
    doorModule.close()
}			

 
Join My Facebook Group
Join Group
 
aaaaaaaaaaaaa
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Selenium Online Training : Our next online training course for Selenium with Java starts from 17th December 2018.

You can attend first 3 classes for free, the total course fee is INR 10,000

The course time would be 8.00 PM(IST) for the first three classes

If you are interested to learn, then you can join the course by sending email to chercher.tech@gmail.com

or Register below


 
Join My Facebook Group
Join Group