Hands-On Design Patterns with Kotlin
Build scalable applications using traditional, reactive, and concurrent design patterns in Kotlin
Alexey Soshin
- 310 pages
- English
- ePUB (mobile friendly)
- Available on iOS & Android
Hands-On Design Patterns with Kotlin
Build scalable applications using traditional, reactive, and concurrent design patterns in Kotlin
Alexey Soshin
About This Book
Make the most of Kotlin by leveraging design patterns and best practices to build scalable and high performing appsAbout This Book• Understand traditional GOF design patterns to apply generic solutions• Shift from OOP to FP; covering reactive and concurrent patterns in a step-by-step manner• Choose the best microservices architecture and MVC for your development environmentWho This Book Is ForThis book is for developers who would like to master design patterns with Kotlin to build efficient and scalable applications. Basic Java or Kotlin programming knowledge is assumedWhat You Will Learn• Get to grips with Kotlin principles, including its strengths and weaknesses• Understand classical design patterns in Kotlin• Explore functional programming using built-in features of Kotlin• Solve real-world problems using reactive and concurrent design patterns• Use threads and coroutines to simplify concurrent code flow• Understand antipatterns to write clean Kotlin code, avoiding common pitfalls• Learn about the design considerations necessary while choosing between architecturesIn DetailDesign 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 wayBy 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.Style and approachThis book explains design patterns in a step-by-step manner with clear and concise code examples
Frequently asked questions
Information
Getting Familiar with Behavioral Patterns
- Strategy
- Iterator
- State
- Command
- Chain of responsibility
- Interpreter
- Mediator
- Memento
- Visitor
- Template method
- Observer
Strategy
enum class Direction {
LEFT, RIGHT
}
abstract class Projectile(private val x: Int,
private val y: Int,
private val direction: Direction)
class OurHero {
private var direction = Direction.LEFT
private var x: Int = 42
private var y: Int = 173
fun shoot(): Projectile {
return object : Projectile(x, y, direction) {
// Draw and animate projectile here
}
}
}
- Peashooter: Shoots small peas that fly straight. Our hero starts with it.
- Pomegranate: Explodes when hitting an enemy, much like a grenade.
- Banana: Returns like a boomerang when it reaches the end of the screen.
Fruit arsenal
interface Weapon {
fun shoot(x: Int,
y: Int,
direction: Direction): Projectile
}
class Peashooter : Weapon {
override fun shoot(x: Int,
y: Int,
direction: Direction) =
object : Projectile(x, y, direction) {
// Fly straight
}
}
class Pomegranate : Weapon {
override fun shoot(x: Int,
y: Int,
direction: Direction) =
object : Projectile(x, y, direction) {
// Explode when you hit first enemy
}
}
class Banana : Weapon {
override fun shoot(x: Int,
y: Int,
direction: Direction) =
object : Projectile(x, y, direction) {
// Return when you hit screen border
}
}
private var currentWeapon : Weapon = Peashooter()
fun shoot(): Projectile = currentWeapon.shoot(x, y, direction)
fun equip(weapon: Weapon) {
currentWeapon = weapon
}
Citizen function
val x = 7
var myPet = Canary("Michael")
val square = fun (x: Int): Long {
return (x * x).toLong()
}
Switching sides
object Weapons {
// Functions we'll be there soon
}