Kotlin

Command Design Pattern in Kotlin

Introduction

Command is a behavioural design pattern which intent is to encapsulate all information about an action. It is commonly used when you need to issue requests without knowing what action will be performed or who is the receiver of the action. You can look at Command design pattern as black box. Invoker of the request does not know what is happening inside the box.

Command design pattern decouples invoker of the request from the object that does know how to perform that request. We are using this pattern because we want to achieve loose coupling as much as possible, since we do not want that our software is hard to change.

Implementation

I implemented this example with Kotlin language from JetBrain that recently got a lot of popularity since it is now an official language for Android. First I wanted to do in Java, but there are a lot of more examples in Java than in Kotlin. If you are familiar with Java, then you will not have problems with this example.

We have an abstract command that can be interface or abstract class depending on your need. In my example it is an interface, but it can be an abstract class depending on your system design. That interface have funtion with name execute. To this function I will pass User class that acts as data object. After that we need to create a concrete implementation of that abstract command.

data class User(val name: String) {
}

interface Command {
    fun execute(user: User)
}

class AddUser : Command {
    override fun execute(user: User) {
        println("Adding a new user with name: "+ user.name)
    }
}

class DeleteUser : Command {
    override fun execute(user: User) {
        println("Deleting user with name: "+user.name)
    }
}

class EditUser : Command {
    override fun execute(user: User) {
        println("Editing user with name: "+user.name)
    }
}

fun main(args: Array<String>) {

    var user = User("Kotlin")

    var add = AddUser()
    add.execute(user)

    var edit = EditUser();
    edit.execute(user)

    var delete = DeleteUser()
    delete.execute(user)

}

When you run the program you should see output like this:

Adding a new user with name: Kotlin
Editing user with name: Kotlin
Deleting user with name: Kotlin

Conclusion

As you can see we implemented a trivial example, but the general idea of this pattern is to decouple our system from an action. So for example, if we have need to add user in multiple places and decide to change implementation of how we add user to the system, without this pattern we would need to do it in multiple places. Now we can easily just change implementation on execute function inside concrete command.

The most common example of usage this pattern for me when I am implementing use cases. So, when I call concrete use case from client class then that client class and concrete implementation inside execute function have lose coupling since they do not know much about each other.

 

If you are interested in topics like this about development, you can follow me on Twitter @Zookey90.

No Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.