Book Image

Kotlin Design Patterns and Best Practices - Second Edition

By : Alexey Soshin
Book Image

Kotlin Design Patterns and Best Practices - Second Edition

By: Alexey Soshin

Overview of this book

This book shows you how easy it can be to implement traditional design patterns in the modern multi-paradigm Kotlin programming language, and takes you through the new patterns and paradigms that have emerged. This second edition is updated to cover the changes introduced from Kotlin 1.2 up to 1.5 and focuses more on the idiomatic usage of coroutines, which have become a stable language feature. You'll begin by learning about the practical aspects of smarter coding in Kotlin, as well as understanding basic Kotlin syntax and the impact of design patterns on your code. The book also provides an in-depth explanation of the classical design patterns, such as Creational, Structural, and Behavioral families, before moving on to functional programming. You'll go through reactive and concurrent patterns, and finally, get to grips with coroutines and structured concurrency to write performant, extensible, and maintainable code. By the end of this Kotlin book, you'll have explored the latest trends in architecture and design patterns for microservices. You’ll also understand the tradeoffs when choosing between different architectures and make informed decisions.
Table of Contents (17 chapters)
1
Section 1: Classical Patterns
6
Section 2: Reactive and Concurrent Patterns
11
Section 3: Practical Application of Design Patterns

What this book covers

Chapter 1, Getting Started with Kotlin, covers basic Kotlin syntax and discusses what design patterns are good for and why they should be used in Kotlin. The goal of this chapter is not to cover the entire Kotlin vocabulary but to get you familiar with some basic concepts and idioms. The following chapters will slowly expose you to more language features as they become relevant to the design patterns we'll discuss.

Chapter 2, Working with Creational Patterns, explains all the classical creational patterns. These patterns deal with how and when to create your objects. Mastering these patterns will allow you to manage the life cycle of your objects better and write code that is easy to maintain.

Chapter 3, Understanding Structural Patterns, focuses on how to create hierarchies of objects that are flexible and simple to extend. It covers the Decorator and Adapter patterns, among others.

Chapter 4, Getting Familiar with Behavioral Patterns, covers behavioral patterns with Kotlin. Behavioral patterns deal with how objects interact with one another and how objects can change behavior dynamically. We'll see how objects can communicate efficiently and in a decoupled manner.

Chapter 5, Introducing Functional Programming, covers the basic principles of functional programming and how they fit into the Kotlin programming language. It will cover topics such as immutability, higher-order functions, and functions as values.

Chapter 6, Threads and Coroutines, dives deeper into how to launch new threads in Kotlin and covers the reasons why coroutines can scale much better than threads. We will discuss how the Kotlin compiler treats coroutines and the relationship with coroutine scopes and dispatchers.

Chapter 7, Controlling the Data Flow, covers higher-order functions for collections. We'll see how sequences, channels, and flows apply those functions in a concurrent and reactive manner.

Chapter 8, Designing for Concurrency, explains how concurrent design patterns help us manage many tasks at once and structure their life cycle. By using these patterns efficiently, we can avoiding problems such as resource leaks and deadlocks.

Chapter 9, Idioms and Anti-Patterns, discusses the best and worst practices in Kotlin. You'll learn what idiomatic Kotlin code should look like and also which patterns to avoid. After completing this chapter, you should be able to write more readable and maintainable Kotlin code, as well as avoiding some common pitfalls.

Chapter 10, Concurrent Microservices with Ktor, puts the skills we've learned so far to use by building a microservice using the Kotlin programming language. For that, we'll use the Ktor framework, which was developed by JetBrains.

Chapter 11, Reactive Microservices with Vert.x, demonstrates an alternative approach to building microservices with Kotlin by using the Vert.x framework, which is based on reactive design patterns. We'll discuss the tradeoffs between the approaches, looking at some real code examples, and figure out when to use them.

Assessments contains all the answers to the questions from all the chapters in this book.