0 boilerplate implementing State pattern in Kotlin

State pattern is pretty nice solution for solving problems related to changing behavior of some component(class) at runtime, which varies depending on current state of that object. Clients of our class having dynamic behavior have an impression that, upon interaction with that class, it seems like there’s different implementation of that object as of that interaction. There’s no magic involved – we’re just using composition, and our dynamic object just delegates call to State object it encapsulates. Interface of State object defines set of actions (methods) that change behavior of our wrapping class (set of actions that cause system to transition). State implementations are those that are in charge of making transitions from State to State.

Here’s an example of coffee machine, that acts differently based on State it’s currently in. Here, CoffeeMachine is our dynamic class, and it encapsulates instance of our CoffeeMachineState interface. We initially set Off implementation of State when starting our coffee machine. Then, our implementations transition the state, based on different actions being performed.

Go ahead – subscribe for more interesting content to come! 🙂

Advertisements

Decorator pattern in Kotlin

If you feel curious how would an implementation of decorator design pattern look like in Kotlin, you came to the right place. This example is just a very basic thing that you then tweak until it’s perfect. You can find decorator definition yourself, but what’s important is that you can compose chain of decorators at runtime and in such a way – you can control runtime behavior of your system.

Liked this one? Subscribe 🙂

Factory method pattern in Kotlin

Ladies & Gents, Kotlin language can come quite handy write concise and expressive code, without too much boilerplate, while keeping all advantages of being JVM citizen. Let’s quickly peek into example of creating Factory method creational design pattern implementation.

package patterns

interface Gadget {
    fun play()
    fun getInfo()
}

enum class Purpose {
    FUN, WORK
}

class GadgetFactory {
    companion object {
        fun createGadget(purpose: Purpose): Gadget = when (purpose) {
            Purpose.FUN -> object : Gadget {
                private val model = "XBox One S | year: 2016"
                override fun getInfo() {
                    println(model)
                }

                override fun play() {
                    println("I'm having great fun playing $model")
                }
            }
            Purpose.WORK -> object : Gadget {
                private val model = "Macbook Pro 2015 | year: 2015"
                override fun getInfo() = println(model)
                override fun play() {
                    println("I'm working hard using my $model")
                }
            }
        }
    }
}

val purpose: String = args[0]
val gadget = GadgetFactory.createGadget(when (purpose) {
    "FUN" -> Purpose.FUN
    "WORK" -> Purpose.WORK
    else -> throw IllegalArgumentException("No gadget for such purpose :(")
})

println("\n**** GADGET INFO ****\n")
gadget.getInfo()

println("\n**** PLAYING GADGET ****\n")
gadget.play()

Example demonstrates simple case where user enters which kind of Gadget she/he wants via command line argument, and based on value provided, he gets back some kind of Gadget dynamically instantiated. Continue reading

Strategy pattern example with Kotlin scripts

I thought it would be nice to use advantages of kotlin language to showcase strategy pattern implementation. In order to get the example running – we need to install kotlin binaries (installation). I’m running simple kotlin script in this example.

Having scripting support it makes it very easy to fire up some process with full kotlin language capabilities. You could use it to do some kind of administrative tasks in your business environment.  Continue reading

Lightweight strategy pattern in Java 8

In computer programming, the strategy pattern (also known as the policy pattern) is a software design pattern that enables an algorithm’s behavior to be selected at runtime. The strategy pattern. defines a family of algorithms, encapsulates each algorithm, and. makes the algorithms interchangeable within that family.

Let’s start straight away with an example. Task is to build an application that would load a user information from arbitrary storage, log user details using arbitrary logger and finally – save user using different persistence options. App should be a Java console application receiving input as command line parameters.

Continue reading