Scala Design Patterns - Second Edition

Book description

Learn how to write efficient, clean, and reusable code with Scala

About This Book
  • Unleash the power of Scala and apply it in the real world to build scalable and robust applications.
  • Learn about using and implementing Creational, Structural, Behavioral, and Functional design patterns in Scala
  • Learn how to build scalable and extendable applications efficiently
Who This Book Is For

If you want to increase your understanding of Scala and apply design patterns to real-life application development, then this book is for you.Prior knowledge of Scala language is assumed/ expected.

What You Will Learn
  • Immerse yourself in industry-standard design patterns—structural, creational, and behavioral—to create extraordinary applications
  • See the power of traits and their application in Scala
  • Implement abstract and self types and build clean design patterns
  • Build complex entity relationships using structural design patterns
  • Create applications faster by applying functional design patterns
In Detail

Design patterns make developers' lives easier by helping them write great software that is easy to maintain, runs efficiently, and is valuable to the company or people concerned. You'll learn about the various features of Scala and will be able to apply well-known, industry-proven design patterns in your work.

The book starts off by focusing on some of the most interesting and latest features of Scala while using practical real-world examples. We will be learning about IDE's and Aspect Oriented Programming. We will be looking into different components in Scala. We will also cover the popular "Gang of Four" design patterns and show you how to incorporate functional patterns effectively. The book ends with a practical example that demonstrates how the presented material can be combined in real-life applications. You'll learn the necessary concepts to build enterprise-grade applications.

By the end of this book, you'll have enough knowledge and understanding to quickly assess problems and come up with elegant solutions.

Style and approach

The design patterns in the book are explained using real-world, step-by-step examples. For each design pattern, there are tips on when to use it and when to look for something more suitable. This book can also be used as a practical guide, showing you how to leverage design patterns effectively. We've designed the book to be used as a quick reference guide while creating applications.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Scala Design Patterns Second Edition
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  5. 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
  6. The Design Patterns Out There and Setting Up Your Environment
    1. Design patterns
      1. Scala and design patterns
      2. The need for design patterns and their benefits
    2. Design pattern categories
      1. Creational design patterns
        1. The abstract factory design pattern
        2. The factory method design pattern
        3. The lazy initialization design pattern
        4. The singleton design pattern
        5. The object pool design pattern
        6. The builder design pattern
        7. The prototype design pattern
      2. Structural design patterns
        1. The adapter design pattern
        2. The decorator design pattern
        3. The bridge design pattern
        4. The composite design pattern
        5. The facade design pattern
        6. The flyweight design pattern
        7. The proxy design pattern
      3. Behavioral design patterns
        1. The value object design pattern
        2. The null object design pattern
        3. The strategy design pattern
        4. The command design pattern
        5. The chain of responsibility design pattern
        6. The interpreter design pattern
        7. The iterator design pattern
        8. The mediator design pattern
        9. The memento design pattern
        10. The observer design pattern
        11. The state design pattern
        12. The template method design pattern
        13. The visitor design pattern
      4. Functional design patterns
        1. Monoids
        2. Monads
        3. Functors
      5. Scala-specific design patterns
        1. The lens design pattern
        2. The cake design pattern
        3. Pimp my library
        4. Stackable traits
        5. The type class design pattern
        6. Lazy evaluation
        7. Partial functions
        8. Implicit injection
        9. Duck typing
        10. Memoization
    3. Choosing a design pattern
    4. Setting up the development environment
      1. Installing Scala
        1. Tips for installing Scala manually
        2. Tips for installing Scala using SBT
      2. Scala IDEs
      3. Dependency management
        1. SBT
        2. Maven
        3. SBT versus Maven
    5. Summary
  7. Traits and Mixin Compositions
    1. Traits
      1. Traits as interfaces
        1. Mixing in traits with variables
      2. Traits as classes
      3. Extending classes
      4. Extending traits
    2. Mixin compositions
      1. Mixing traits in
      2. Composing
        1. Composing simple traits
        2. Composing complex traits
        3. Composing with self-types
      3. Clashing traits
        1. Same signatures and return types
        2. Same signatures and different return types traits
        3. Same signatures and return types mixins
        4. Same signatures and different return types mixins
    3. Multiple inheritance
      1. The diamond problem
      2. The limitations
    4. Linearization
      1. Rules of inheritance hierarchies
      2. Linearization rules
      3. How linearization works
      4. Initialization
      5. Method overriding
    5. Testing traits
      1. Using a class
      2. Mixing the trait in
        1. Mixing into the test class
        2. Mixing into the test cases
      3. Running the tests
    6. Traits versus classes
    7. Summary
  8. Unification
    1. Functions and classes
      1. Functions as classes
        1. Function literals
        2. Functions without syntactic sugar
      2. Increased expressivity
    2. Algebraic data types and class hierarchies
      1. ADTs
        1. Sum ADTs
        2. Product ADTs
        3. Hybrid ADTs
        4. The unification
      2. Pattern matching
        1. Pattern matching with values
        2. Pattern matching for product ADTs
    3. Modules and objects
      1. Using modules
    4. Summary
  9. Abstract and Self Types
    1. Abstract types
      1. Generics
      2. Abstract types
      3. Generics versus abstract types
        1. Usage advice
    2. Polymorphism
      1. Subtype polymorphism
      2. Parametric polymorphism
      3. Ad hoc polymorphism
        1. Adding functions for multiple types
    3. Self types
      1. Using self types
        1. Requiring multiple components
        2. Conflicting components
        3. Self types and the cake design pattern
      2. Self types versus inheritance
        1. Inheritance leaking functionality
    4. Summary
  10. Aspect-Oriented Programming and Components
    1. Aspect-oriented programming
      1. Understanding application efficiency
        1. Timing our application without AOP
        2. Timing our application with AOP
    2. Components in Scala
      1. Using Scala's expressive power to build components
        1. Implementing components
        2. Self types for components
    3. Summary
  11. Creational Design Patterns
    1. What are creational design patterns?
    2. The factory method design pattern
      1. An example class diagram
      2. A code example
        1. Scala alternatives
      3. What it is good for?
      4. What it is not so good for?
    3. The abstract factory
      1. An example class diagram
      2. A code example
        1. Scala alternatives
      3. What it is good for?
      4. What it is not so good for?
    4. Other factory design patterns
      1. The static factory
      2. The simple factory
      3. Factory combinations
    5. Lazy initialization
      1. An example class diagram
      2. A code example
      3. What it is good for?
      4. What it is not so good for?
    6. The singleton design pattern
      1. An example class diagram
      2. A code example
      3. What it is good for?
      4. What it is not so good for?
    7. The builder design pattern
      1. An example class diagram
      2. A code example
        1. A Java-like implementation
        2. Implementation with a case class
        3. Using generalized type constraints
          1. Changing the Person class
          2. Adding generalized type constraints to the required methods
          3. Using the type-safe builder
        4. Using require statements
      3. What it is good for?
      4. What it is not so good for?
    8. The prototype design pattern
      1. An example class diagram
      2. A code example
      3. What it is good for?
      4. What it is not so good for?
    9. Summary
  12. Structural Design Patterns
    1. Defining structural design patterns
    2. The adapter design pattern
      1. Example class diagram
      2. Code example
        1. The adapter design pattern with final classes
        2. The adapter design pattern the Scala way
      3. What it is good for
      4. What it is not so good for
    3. The decorator design pattern
      1. Example class diagram
      2. Code example
        1. The decorator design pattern the Scala way
      3. What it is good for
      4. What it is not so good for
    4. The bridge design pattern
      1. Example class diagram
      2. Code example
        1. The bridge design pattern the Scala way
      3. What it is good for
      4. What it is not so good for
    5. The composite design pattern
      1. Example class diagram
      2. Code example
      3. What it is good for
      4. What it is not so good for
    6. The facade design pattern
      1. Example class diagram
      2. Code example
      3. What it is good for
      4. What it is not so good for
    7. The flyweight design pattern
      1. Example class diagram
      2. Code example
      3. What it is good for
      4. What it is not so good for
    8. The proxy design pattern
      1. Example class diagram
      2. Code example
      3. What it is good for
      4. What it is not so good for
    9. Summary
  13. Behavioral Design Patterns – Part One
    1. Defining behavioral design patterns
    2. The value object design pattern
      1. An example class diagram
      2. A code example
      3. Alternative implementation
      4. What it is good for
      5. What it is not so good for
    3. The null object design pattern
      1. An example class diagram
      2. A code example
      3. What it is good for
      4. What it is not so good for
    4. The strategy design pattern
      1. An example class diagram
      2. A code example
        1. The strategy design pattern the Scala way
      3. What it is good for
      4. What it is not so good for
    5. The command design pattern
      1. An example class diagram
      2. A code example
        1. The command design pattern the Scala way
      3. What it is good for
      4. What it is not so good for
    6. The chain of responsibility design pattern
      1. An example class diagram
      2. A code example
        1. The chain of responsibility design pattern the Scala way
      3. What it is good for
      4. What it is not so good for
    7. The interpreter design pattern
      1. An example class diagram
      2. A code example
      3. What it is good for
      4. What it is not so good for
    8. Summary
  14. Behavioral Design Patterns – Part Two
    1. The iterator design pattern
      1. Example class diagram
      2. Code example
      3. What it is good for
      4. What it is not so good for
    2. The mediator design pattern
      1. Example class diagram
      2. Code example
      3. What it is good for
      4. What it is not so good for
    3. The memento design pattern
      1. Example class diagram
      2. Code example
      3. What it is good for
      4. What it is not so good for
    4. The observer design pattern
      1. Example class diagram
      2. Code example
      3. What it is good for
      4. What it is not so good for
    5. The state design pattern
      1. Example class diagram
      2. Code example
      3. What it is good for
      4. What it is not so good for
    6. The template method design pattern
      1. Example class diagram
      2. Code example
      3. What it is good for
      4. What it is not so good for
    7. The visitor design pattern
      1. Example class diagram
      2. Code example
        1. The visitor design pattern the Scala way
      3. What it is good for
      4. What it is not so good for
    8. Summary
  15. Functional Design Patterns – the Deep Theory
    1. Abstraction and vocabulary
    2. Monoids
      1. What are monoids?
      2. Monoids in real life
      3. Using monoids
        1. Monoids and foldable collections
        2. Monoids and parallel computations
        3. Monoids and composition
      4. When to use monoids
    3. Functors
      1. Functors in real life
        1. Using our functors
    4. Monads
      1. What is a monad?
        1. The flatMap method
        2. The unit method
        3. The connection between map, flatMap, and unit
          1. The names of the methods
        4. The monad laws
      2. Monads in real life
      3. Using monads
        1. The Option monad
        2. A more advanced monad example
      4. Monad intuition
    5. Summary
  16. Applying What We Have Learned
    1. The lens design pattern
      1. Lens example
        1. Without the lens design pattern
          1. Immutable and verbose
          2. Using mutable properties
        2. With the lens design pattern
      2. Minimizing the boilerplate
    2. The cake design pattern
      1. Dependency injection
        1. Dependency injection libraries and Scala
      2. Dependency injection in Scala
        1. Writing our code
        2. Wiring it all up
        3. Unit testing our application
      3. Other dependency injection alternatives
        1. Implicits for dependency injection
        2. Reader monad for dependency injection
    3. The pimp my library design pattern
      1. Using pimp my library
      2. Pimp my library in real life
    4. The stackable traits design pattern
      1. Using stackable traits
    5. The type class design pattern
      1. Type class example
      2. Type class design pattern alternatives
    6. Lazy evaluation
      1. Evaluating by-name parameters only once
      2. Alternative lazy evaluation
    7. Partial functions
      1. Partial functions are not partially applied functions
      2. Partially defined functions
    8. Implicit injection
      1. Implicit conversions
      2. Dependency injection using implicits
        1. Testing with implicit dependency injection
    9. Duck typing
      1. Duck typing example
      2. Duck typing alternatives
      3. When to use duck typing
    10. Memoization
      1. Memoization example
      2. Memoization alternatives
    11. Summary
  17. Real-Life Applications
    1. Reasons to use libraries
    2. The Scalaz library
      1. Monoids in Scalaz
        1. Using monoids
        2. Testing monoids
      2. Monads in Scalaz
        1. Using monads
        2. Testing monads
      3. The possibilities of Scalaz
    3. Writing a complete application
      1. Application specifications
      2. Implementation
        1. The libraries to use
          1. Reading the application configuration
          2. Reading the scheduler configuration
          3. Scheduling tasks
          4. Accessing a database
          5. Executing console commands
        2. Writing some code
        3. Wiring it all up
        4. The end result
      3. Testing our application
        1. Unit testing
        2. Application testing
      4. The future of our application
    4. Summary
  18. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Scala Design Patterns - Second Edition
  • Author(s): Ivan Nikolov
  • Release date: April 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788471305