Book Image

Hands-On Design Patterns with Kotlin

By : Alexey Soshin
Book Image

Hands-On Design Patterns with Kotlin

By: Alexey Soshin

Overview of this book

Design patterns enable you as a developer to speed up the development process by providing you with proven development paradigms. Reusing design patterns helps prevent complex issues that can cause major problems, improves your code base, promotes code reuse, and makes an architecture more robust. The mission of this book is to ease the adoption of design patterns in Kotlin and provide good practices for programmers. The book begins by showing you the practical aspects of smarter coding in Kotlin, explaining the basic Kotlin syntax and the impact of design patterns. From there, the book provides an in-depth explanation of the classical design patterns of creational, structural, and behavioral families, before heading into functional programming. It then takes you through reactive and concurrent patterns, teaching you about using streams, threads, and coroutines to write better code along the way By the end of the book, you will be able to efficiently address common problems faced while developing applications and be comfortable working on scalable and maintainable projects of any size.
Table of Contents (13 chapters)

State

You can think of the State design pattern as an opinionated Strategy, which we discuss at the beginning of this chapter. But while Strategy is changed from the outside, by the client, the State may change internally, based solely on the input it gets.

Look at this dialog a client wrote with Strategy:

Client: Here’s a new thing to do, start doing it from now on.
Strategy: OK, no problem.
Client: What I like about you is that you never argue with me.

Compare it with this one:

Client: Here’s some new input I got from you.
State: Oh, I don't know. Maybe I'll start doing something differently. Maybe not.

The client should also expect that the State may even reject some of its inputs:

Client: Here's something for you to ponder, State.
State: I don't know what it is! Don't you see I'm busy? Go bother some Strategy with this!

So, why do...