Hands-On Design Patterns with Kotlin

Book description

Make the most of Kotlin by leveraging design patterns and best practices to build scalable and high performing apps

About 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 environment
Who This Book Is For

This 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 assumed

What 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 architectures
In Detail

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.

Style and approach

This book explains design patterns in a step-by-step manner with clear and concise code examples

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On Design Patterns with Kotlin
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  5. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  7. Getting Started with Kotlin
    1. Basic language syntax and features
      1. Multi-paradigm
      2. Code structure
        1. No semicolons
        2. Naming conventions
        3. Packages
      3. Types
        1. Type inference
        2. val versus var
        3. Comparison
        4. Null safety
      4. Declaring functions
      5. Control flow
        1. Using the if expression
        2. Using the when expression
      6. String interpolation
      7. Classes and inheritance
        1. Classes
        2. Inheritance
        3. Constructors
      8. Properties
      9. Data classes
      10. More control flow – loops
        1. The for loop
        2. For-each loop
        3. While loop
      11. Extension functions
    2. Introduction to design patterns
      1. What are design patterns?
        1. Design patterns in real life
        2. Design process
      2. Why use design patterns in Kotlin?
    3. Summary
  8. Working with Creational Patterns
    1. Singleton
    2. Factory Method
      1. Factory
      2. Static Factory Method
        1. Advantages of the Static Factory Method
          1. Caching
          2. Subclassing
        2. Static Factory Method in Kotlin
          1. Companion object
    3. Abstract Factory
      1. Abstract Factory in action
        1. Introduction to generics in Kotlin
        2. Back to our bases
        3. Making improvements
    4. Builder
      1. Composing an email
        1. Collection types in Kotlin
        2. Creating an email – first attempt
        3. Creating an email – second attempt
        4. Creating an email – the Kotlin way
        5. Creating an email – the Kotlin way – second attempt
    5. Prototype
      1. Building your own PC
        1. Starting from a prototype
    6. Summary
  9. Understanding Structural Patterns
    1. Decorator
      1. Enhancing a class
        1. Operator overloading
      2. Dude, where's my map?
      3. The great combinator
        1. Caveats
    2. Adapter
      1. Different adapters
      2. Adapters in the real world
        1. Caveats of using adapters
    3. Bridge
      1. Bridging changes
        1. Type aliases
        2. You're in the army now
        3. Constants
        4. A lethal weapon
    4. Composite
      1. Get together
      2. The Squad
        1. Varargs and secondary constructors
      3. Counting bullets
    5. Facade
      1. Keep it simple
    6. Flyweight
      1. Being conservative
      2. Saving memory
    7. Proxy
      1. A short detour into the RMI world
      2. A replacement
        1. Lazy delegation
    8. Summary
  10. Getting Familiar with Behavioral Patterns
    1. Strategy
      1. Fruit arsenal
      2. Citizen function
      3. Switching sides
    2. Iterator
      1. One, two... many
      2. Running through the values
    3. State
      1. Fifty shades of State
      2. State of the Nation
    4. Command
      1. Undoing commands
    5. Chain of responsibility
    6. Interpreter
      1. We need to go deeper
      2. A language of your own
        1. Taking a break
      3. Call suffix
    7. Mediator
      1. Trouble in the Jungle
      2. The middleman
        1. Flavors
        2. Caveats
    8. Memento
      1. Remembrance
    9. Visitor
      1. Writing a crawler
    10. Template method
    11. Observer
      1. Animal Choir
    12. Summary
  11. Functional Programming
    1. Why functional programming?
    2. Immutability
      1. Tuples
      2. Value mutation
      3. Immutable collections
    3. Functions as values
      1. Higher-order functions
      2. Pure functions
      3. Currying
      4. Memoization
    4. Expressions, not statements
      1. Pattern matching
    5. Recursion
    6. Summary
  12. Streaming Your Data
    1. The it notation
    2. The map() function
    3. Filter family
    4. Find family
    5. Drop family
    6. Sort family
    7. ForEach
    8. Join family
    9. Fold/Reduce
    10. Flat family
    11. Slice
    12. Chunked
    13. Zip/Unzip
    14. Streams are lazy, collections are not
    15. Sequences
    16. Summary
  13. Staying Reactive
    1. Reactive principles
      1. Responsiveness
      2. Resiliency
      3. Elasticity
      4. Message-driven
    2. Reactive extension
      1. Hot Observable
      2. Multicast
      3. Subject
        1. ReplaySubject
        2. BehaviorSubject
        3. AsyncSubject
        4. SerializedSubject
      4. Flowables
        1. Holding state
      5. FlowableProcessor
      6. Batching
      7. Throttling
    3. Summary
  14. Threads and Coroutines
    1. Threads
      1. Thread safety
      2. Threads are expensive
    2. Coroutines
      1. Starting coroutines
      2. Jobs
      3. Coroutine starvation
      4. Coroutines under the hood
      5. Fixing starvation
      6. Waiting for a coroutine
      7. Canceling a coroutine
      8. Returning results
      9. Setting timeouts
      10. Parent jobs
      11. Channels
      12. Producers
      13. Actors
    3. Summary
  15. Designed for Concurrency
    1. Active Object
      1. Testing
    2. Deferred value
    3. Barrier
      1. CountDownLatch
      2. Data class as Barrier
    4. Scheduler
      1. Understanding contexts
    5. Pipelines
      1. Establishing a pipeline
    6. The fan-out design pattern
    7. The fan-in design pattern
      1. Managing workers
    8. Buffered channels
    9. Unbiased select
    10. Mutexes
    11. Selecting on close
    12. Sidekick channel
    13. Deferred channel
    14. Summary
  16. Idioms and Anti-Patterns
    1. Let
    2. Apply
    3. Also
    4. Run
    5. With
    6. Instance checks
    7. Try-with-resources
    8. Inline functions
    9. Reified
    10. Constants
    11. Constructor overload
    12. Dealing with nulls
    13. Explicit async
    14. Validation
    15. Sealed, not enumerated
    16. More companions
    17. Scala functions
    18. Summary
  17. Reactive Microservices with Kotlin
    1. Getting started with Vert.x
      1. Routing
    2. Handling requests
      1. Verticles
      2. Subrouting
    3. Testing
      1. Helper methods
    4. Working with databases
      1. Managing configuration
      2. Managing the database
    5. EventBus
      1. Consumer
      2. Producer
      3. More testing
    6. Summary
  18. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On Design Patterns with Kotlin
  • Author(s): Alexey Soshin
  • Release date: June 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788998017