Kotlin Design Patterns and Best Practices
eBook - ePub

Kotlin Design Patterns and Best Practices

  1. 356 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Kotlin Design Patterns and Best Practices

About this book

Future-proof your applications with best practices and design patterns in KotlinKey Features• Understand traditional and modern design patterns to improve the design of your application• Combine the benefits of object-oriented, functional, reactive, and concurrent programming• Choose the best microservices architecture and frameworks for your web applicationBook DescriptionThis 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. What you will learn• Implement all the classical design patterns using the Kotlin programming language• Apply reactive and concurrent design patterns to make your application more scalable• Discover best practices in Kotlin and explore its new features• Understand the key principles of functional programming and learn how they apply to Kotlin• Find out how to write idiomatic Kotlin code and learn which patterns to avoid• Harness the power of Kotlin to design concurrent and reliable systems with ease• Create an effective microservice with Kotlin and the Ktor frameworkWho this book is forThis book is for developers who want to apply design patterns they've learned from other languages in Kotlin and build reliable, scalable, and maintainable applications. You'll need a good grasp on at least one programming language before you get started with this book. Java or design patterns will be particularly useful, but you'll still be able to follow along if you code in other languages.

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access Kotlin Design Patterns and Best Practices by Alexey Soshin,Anton Arhipov in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in Java. We have over one million books available in our catalogue for you to explore.

Information

Section 1: Classical Patterns

In this section, we will cover the basic syntax of the Kotlin programming language and the implementation of all the classical design patterns in Kotlin.
The classical design patterns deal with three major problems in system design: how to create objects efficiently, how to encapsulate object hierarchies, and how to make object behavior more dynamic.
We'll discuss which design patterns come as part of the language, and how to implement those that don't.
This section comprises the following chapters:
  • Chapter 1, Getting Started with Kotlin
  • Chapter 2, Working with Creational Patterns
  • Chapter 3, Understanding Structural Patterns
  • Chapter 4, Getting Familiar with Behavioral Patterns

Chapter 1: Getting Started with Kotlin

The bulk of this chapter will be dedicated to basic Kotlin syntax. It is important to be comfortable with a language before we start implementing any design patterns in it.
We'll also briefly discuss what problems design patterns solve and why you should use them in Kotlin. This will be helpful to those who are less familiar with the concept of design patterns. But even for experienced engineers, it may provide an interesting perspective.
This chapter doesn't aim to cover the entire language vocabulary but to get you familiar with some basic concepts and idioms. The following chapters will expose you to even more language features as they become relevant to the design patterns that we'll discuss.
In this chapter, we will cover the following main topics:
  • Basic language syntax and features
  • Understanding Kotlin code structure
  • Type system and null safety
  • Reviewing Kotlin data structures
  • Control flow
  • Working with text and loops
  • Classes and inheritance
  • Extension functions
  • Introduction to design patterns
By the end of this chapter, you'll have a knowledge of Kotlin's basics, which will be the foundation for the following chapters.

Technical requirements

To follow the instructions in this chapter, you'll need the following:
  • IntelliJ IDEA Community Edition (https://www.jetbrains.com/idea/download/)
  • OpenJDK 11 or higher (https://openjdk.java.net/install/)
The code files for this chapter are available at https://github.com/PacktPublishing/Kotlin-Design-Patterns-and-Best-Practices/tree/main/Chapter01.

Basic language syntax and features

Whether you come from Java, C#, Scala, or any other statically typed programming language, you'll find Kotlin syntax quite familiar. This is not by coincidence but to make the transition to this new language as smooth as possible for those with previous experience in other languages. Besides that familiarity, Kotlin brings a vast amount of features, such as better type safety. As we move ahead, you'll notice that all of them are attempting to solve real-world problems. That pragmatic approach is remarkably consistent across the language. For example, one of the strongest benefits of Kotlin is complete Java interoperability. You can have Java and Kotlin classes alongside each other and freely use any library that is available in Java for a Kotlin project.
To summarize, the goals of the language are as follows:
  • Pragmatic: Makes things we do often easy to achieve
  • Readable: Keeps a balance between conciseness and clarity on what the code does
  • Easy to reuse: Supports adapting code to different situations
  • Safe: Makes it hard to write code that crashes
  • Interoperable: Allows the use of existing libraries and frameworks
This chapter will discuss how these goals are achieved.

Multi-paradigm language

Some of the major paradigms in programming languages are procedural, object-oriented, and functional paradigms.
Being pragmatic, Kotlin allows for any of these paradigms. It has classes and inheritance, coming from the object-oriented approach. It has higher-order functions from functional programming. You don't have to wrap everything in classes if you don't want to, though. Kotlin allows you to structure your entire code as just a set of procedures and structs if you need to. You will see how all these approaches come together, as different examples will combine different paradigms to solve the problems discussed.
Instead of covering all aspects of a topic from start to finish, we will be building the knowledge as we go.

Understanding Kotlin code structure

The first thing you'll need to do when you start programming in Kotlin is to create a new file. Kotlin's file extension is usually .kt.
Unlike Java...

Table of contents

  1. Kotlin Design Patterns and Best Practices Second Edition
  2. Foreword
  3. Preface
  4. Section 1: Classical Patterns
  5. Chapter 1: Getting Started with Kotlin
  6. Chapter 2: Working with Creational Patterns
  7. Chapter 3: Understanding Structural Patterns
  8. Chapter 4: Getting Familiar with Behavioral Patterns
  9. Section 2: Reactive and Concurrent Patterns
  10. Chapter 5: Introducing Functional Programming
  11. Chapter 6: Threads and Coroutines
  12. Chapter 7: Controlling the Data Flow
  13. Chapter 8: Designing for Concurrency
  14. Section 3: Practical Application of Design Patterns
  15. Chapter 9: Idioms and Anti-Patterns
  16. Chapter 10: Concurrent Microservices with Ktor
  17. Chapter 11: Reactive Microservices with Vert.x
  18. Assessments
  19. Other Books You May Enjoy