Helion


Szczegóły ebooka

Kotlin Design Patterns and Best Practices - Second Edition

Kotlin Design Patterns and Best Practices - Second Edition


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.

  • Kotlin Design Patterns and Best Practices Second Edition
  • Foreword
  • Contributors
  • About the author
  • About the reviewers
  • Preface
    • Who this book is for
    • What this book covers
    • To get the most out of this book
    • Download the example code files
    • Download the color images
    • Conventions used
    • Get in touch
    • Share Your Thoughts
  • Section 1: Classical Patterns
  • Chapter 1: Getting Started with Kotlin
    • Technical requirements
    • Basic language syntax and features
      • Multi-paradigm language
    • Understanding Kotlin code structure
      • Naming conventions
      • Packages
      • Comments
      • Hello Kotlin
    • Understanding types
      • Basic types
      • Type inference
      • Values
      • Comparison and equality
      • Declaring functions
    • Null safety
    • Reviewing Kotlin data structures
      • Lists
      • Sets
      • Maps
      • Mutability
      • Alternative implementations for collections
      • Arrays
    • Control flow
      • The if expression
      • The when expression
    • Working with text
      • String interpolation
      • Multiline strings
    • Loops
      • for-each loop
      • The for loop
      • The while loop
    • Classes and inheritance
      • Classes
      • Interfaces
      • Abstract classes
      • Visibility modifiers
      • Inheritance
      • Data classes
    • Extension functions
    • Introduction to design patterns
      • What are design patterns?
      • Why use design patterns in Kotlin?
    • Summary
    • Questions
  • Chapter 2: Working with Creational Patterns
    • Technical requirements
    • Singleton
    • Factory Method
      • Static Factory Method
    • Abstract Factory
      • Casts
      • Subclassing
      • Smart casts
      • Variable shadowing
      • Collection of Factory Methods
    • Builder
      • Fluent setters
      • Default arguments
    • Prototype
      • Starting from a prototype
    • Summary
    • Questions
  • Chapter 3: Understanding Structural Patterns
    • Technical requirements
    • Decorator
      • Enhancing a class
      • The Elvis operator
      • The inheritance problem
      • Operator overloading
      • Caveats of the Decorator design pattern
    • Adapter
      • Adapting existing code
      • Adapters in the real world
      • Caveats of using adapters
    • Bridge
      • Bridging changes
      • Type aliasing
      • Constants
    • Composite
      • Secondary constructors
      • The varargs keyword
      • Nesting composites
    • Facade
    • Flyweight
      • Being conservative
      • Saving memory
      • Caveats of the Flyweight design pattern
    • Proxy
      • Lazy delegation
    • Summary
    • Questions
  • Chapter 4: Getting Familiar with Behavioral Patterns
    • Technical requirements
    • Strategy
      • Fruit arsenal
      • Citizen functions
    • Iterator
    • State
      • Fifty shades of State
      • State of the nation
    • Command
      • Undoing commands
    • Chain of Responsibility
    • Interpreter
      • We need to go deeper
      • A language of your own
      • Call suffix
    • Mediator
      • The middleman
      • Mediator flavors
      • Mediator caveats
    • Memento
    • Visitor
      • Writing a crawler
    • Template method
    • Observer
      • Animal choir example
    • Summary
    • Questions
  • Section 2: Reactive and Concurrent Patterns
  • Chapter 5: Introducing Functional Programming
    • Technical requirements
    • Reasoning behind the functional approach
    • Immutability
      • Immutable collections
      • The problem with shared mutable state
      • Tuples
    • Functions as values
      • Learning about higher-order functions
      • Higher-order functions in a standard library
    • The it notation
      • Closures
      • Pure functions
      • Currying
      • Memoization
    • Using expressions instead of statements
      • Pattern matching
    • Recursion
    • Summary
    • Questions
  • Chapter 6: Threads and Coroutines
    • Technical requirements
    • Looking deeper into threads
      • Thread safety
      • Why are threads expensive?
    • Introducing coroutines
      • Starting coroutines
      • Jobs
      • Coroutines under the hood
      • Setting timeouts
      • Dispatchers
      • Structured concurrency
    • Summary
    • Questions
  • Chapter 7: Controlling the Data Flow
    • Technical requirements
    • Reactive principles
      • Responsive principle
      • Resilient principle
      • Elastic principle
      • Message-driven principle
    • Higher-order functions on collections
      • Mapping elements
      • Filtering elements
      • Finding elements
      • Executing code for each element
      • Summing up elements
      • Getting rid of nesting
    • Exploring concurrent data structures
      • Sequences
      • Channels
      • Flows
    • Summary
    • Questions
  • Chapter 8: Designing for Concurrency
    • Technical requirements
    • Deferred Value
    • Barrier
      • Using data classes as barriers
    • Scheduler
      • Creating your own schedulers
    • Pipeline
      • Composing a pipeline
    • Fan Out
    • Fan In
    • Racing
      • Unbiased select
    • Mutex
    • Sidekick channel
    • Summary
    • Questions
  • Section 3: Practical Application of Design Patterns
  • Chapter 9: Idioms and Anti-Patterns
    • Technical requirements
    • Using the scope functions
      • Let function
      • Apply function
      • Also function
      • Run function
      • With function
    • Type checks and casts
    • An alternative to the try-with-resources statement
    • Inline functions
    • Implementing Algebraic Data Types
    • Reified generics
    • Using constants efficiently
    • Constructor overload
    • Dealing with nulls
    • Making asynchronicity explicit
    • Validating input
    • Preferring sealed classes over enums
    • Summary
    • Questions
  • Chapter 10: Concurrent Microservices with Ktor
    • Technical requirements
    • Getting started with Ktor
    • Routing requests
    • Testing the service
    • Modularizing the application
    • Connecting to a database
    • Creating new entities
    • Making the tests consistent
    • Fetching entities
    • Organizing routes in Ktor
    • Achieving concurrency in Ktor
    • Summary
    • Questions
  • Chapter 11: Reactive Microservices with Vert.x
    • Technical requirements
    • Getting started with Vert.x
    • Routing in Vert.x
    • Verticles
    • Handling requests
      • Subrouting the requests
    • Testing Vert.x applications
    • Working with databases
      • Managing configuration
    • Understanding Event Loop
    • Communicating with Event Bus
      • Sending JSON over Event Bus
    • Summary
    • Questions
  • Assessments
    • Chapter 1, Getting Started with Kotlin
    • Question 1
    • Answer
    • Question 2
    • Answer
    • Question 3
    • Answer
    • Chapter 2, Working with Creational Patterns
    • Question 1
    • Answer
    • Question 2
    • Answer
    • Question 3
    • Answer
    • Chapter 3, Understanding Structural Patterns
    • Question 1
    • Answer
    • Question 2
    • Answer
    • Question 3
    • Answer
    • Chapter 4, Getting Familiar with Behavioral Patterns
    • Question 1
    • Answer
    • Question 2
    • Answer
    • Question 3
    • Answer
    • Chapter 5, Introducing Functional Programming
    • Question 1
    • Answer
    • Question 2
    • Answer
    • Question 3
    • Answer
    • Chapter 6, Threads and Coroutines
    • Question 1
    • Answer
    • Question 2
    • Answer
    • Question 3
    • Answer
    • Chapter 7, Controlling the Data Flow
    • Question 1
    • Answer
    • Question 2
    • Answer
    • Question 3
    • Answer
    • Chapter 8, Designing for Concurrency
    • Question 1
    • Answer
    • Question 2
    • Answer
    • Question 3
    • Answer
    • Chapter 9, Idioms and Anti-Patterns
    • Question 1
    • Answer
    • Question 2
    • Answer
    • Question 3
    • Answer
    • Chapter 10, Concurrent Microservices with Ktor
    • Question 1
    • Answer
    • Question 2
    • Answer
    • Question 3
    • Answer
    • Chapter 11, Reactive Microservices with Vert.x
    • Question 1
    • Answer
    • Question 2
    • Answer
    • Question 3
    • Answer
    • Why subscribe?
  • Other Books You May Enjoy
    • Packt is searching for authors like you
    • Share Your Thoughts

  • Tytuły: Kotlin Design Patterns and Best Practices - Second Edition
  • Autor: Alexey Soshin
  • Tytuł oryginału: Kotlin Design Patterns and Best Practices - Second Edition
  • ISBN Ebooka: 9781801816281, 9781801816281
  • Data wydania: 2022-01-21
  • Identyfikator pozycji: e_2t57
  • Kategorie:
  • Wydawca: Packt Publishing