Scala Design Patterns

Book description

Write efficient, clean, and reusable code with Scala

About This Book

  • Unleash the power of Scala and apply it in the real world
  • Increase your efficiency by leveraging the power of Creational, Structural, Behavioural, and Functional design patterns
  • Build object oriented and functional applications quickly and effectively

Who This Book Is For

If you want to increase your understanding of Scala and apply it to real-life application development, then this book is for you. We've also designed the book to be used as a quick reference guide while creating applications. Previous Scala programming knowledge is expected.

What You Will Learn

  • Immerse yourself in industry-standard design patterns - structural, creational, and behavioral - to create extraordinary applications
  • Feel 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

Scala has become increasingly popular in many different IT sectors. The language is exceptionally feature-rich which helps developers write less code and get faster results. Design patterns make developer's 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 will learn about the various features of Scala and 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 features of Scala while using practical real-world examples. We will also cover the popular "Gang of Four" design patterns and show you how to incorporate functional patterns effectively. By the end of this book, you will have enough knowledge and understanding to quickly assess problems and come up with elegant solutions.

Style and approach

The design patterns in the book will be explained using real-world, step-by-step examples. For each design pattern, there will be hints about 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.

Table of contents

  1. Scala Design Patterns
    1. Table of Contents
    2. Scala Design Patterns
    3. Credits
    4. About the Author
    5. Acknowledgments
    6. About the Reviewer
    7. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    8. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    9. 1. 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. How to choose a design pattern
      4. Setting up the development environment
        1. Installing Scala
        2. Scala IDEs
        3. Dependency management
          1. SBT
          2. Maven
          3. SBT versus Maven
      5. Summary
    10. 2. 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 traits
          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
    11. 3. 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
    12. 4. 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 leaks functionality
      4. Summary
    13. 5. Aspect-Oriented Programming and Components
      1. Aspect-oriented programming
      2. Chapter case example
        1. Without AOP
        2. With AOP
      3. Components in Scala
        1. Scala is rich
          1. Implementing components
          2. Self types for components
      4. Summary
    14. 6. Creational Design Patterns
      1. What are creational design patterns?
      2. The factory method design pattern
        1. Class diagram
        2. Code example
          1. Scala alternatives
        3. What is it good for?
        4. What is it not so good for?
      3. The abstract factory
        1. Class diagram
        2. Code example
          1. Scala alternatives
        3. What is it good for?
        4. What is it not so good for?
      4. Other factory design patterns
        1. The static factory
        2. The simple factory
        3. Factory combinations
      5. Lazy initialization
        1. Class diagram
        2. Code example
        3. What is it good for?
        4. What is it not so good for?
      6. The singleton design pattern
        1. Class diagram
        2. Code example
        3. What is it good for?
        4. What is it not so good for?
      7. The builder design pattern
        1. Class diagram
        2. Code example
          1. Java-like implementation
          2. Implementation with a case class
          3. Using generalized type constraints
            1. Changing the Person class
            2. Changing the PersonBuilder class
            3. Adding generalized type constraints to the required methods
            4. Using the type-safe builder
          4. Using require statements
        3. What is it good for?
        4. What is it not so good for?
      8. The prototype design pattern
        1. Class diagram
        2. Code example
        3. What is it good for?
        4. What is it not so good for?
      9. Summary
    15. 7. Structural Design Patterns
      1. What are structural design patterns?
      2. The adapter design pattern
        1. Class diagram
        2. Code example
          1. The adapter design pattern with final classes
          2. The adapter design pattern the Scala way
        3. What is it good for?
        4. What is it not so good for?
      3. The decorator design pattern
        1. Class diagram
        2. Code example
          1. The decorator design pattern the Scala way
        3. What is it good for?
        4. What is it not so good for?
      4. The bridge design pattern
        1. Class diagram
        2. Code example
          1. The bridge design pattern the Scala way
        3. What is it good for?
        4. What is it not so good for?
      5. The composite design pattern
        1. Class diagram
        2. Code example
        3. What is it good for?
        4. What is it not so good for?
      6. The facade design pattern
        1. Class diagram
        2. Code example
        3. What is it good for?
        4. What is it not so good for?
      7. The flyweight design pattern
        1. Class diagram
        2. Code example
        3. What is it good for?
        4. What is it not so good for?
      8. The proxy design pattern
        1. Class diagram
        2. Code example
        3. What is it good for?
        4. What is it not so good for?
      9. Summary
    16. 8. Behavioral Design Patterns – Part 1
      1. What are behavioral design patterns?
      2. The value object design pattern
        1. Class diagram
        2. Code example
        3. What is it good for?
        4. What is it not so good for?
      3. The null object design pattern
        1. Class diagram
        2. Code example
        3. What is it good for?
        4. What is it not so good for?
      4. The strategy design pattern
        1. Class diagram
        2. Code example
        3. The strategy design pattern the Scala way
      5. The command design pattern
        1. Class diagram
        2. Code example
        3. The command design pattern the Scala way
        4. What is it good for?
        5. What is it not so good for?
      6. The chain of responsibility design pattern
        1. Class diagram
        2. Code example
        3. The chain of responsibility design pattern the Scala way
        4. What is it good for?
        5. What is it not so good for?
      7. The interpreter design pattern
        1. Class diagram
        2. Code example
        3. What is it good for?
        4. What is it not so good for?
      8. Summary
    17. 9. Behavioral Design Patterns – Part 2
      1. The iterator design pattern
        1. Class diagram
        2. Code example
        3. What is it good for?
        4. What is it not so good for?
      2. The mediator design pattern
        1. Class diagram
        2. Code example
        3. What is it good for?
        4. What is it not so good for?
      3. The memento design pattern
        1. Class diagram
        2. Code example
        3. What is it good for?
        4. What is it not so good for?
      4. The observer design pattern
        1. Class diagram
        2. Code example
        3. What is it good for?
        4. What is it not so good for?
      5. The state design pattern
        1. Class diagram
        2. Code example
        3. What is it good for?
        4. What is it not so good for?
      6. The template method design pattern
        1. Class diagram
        2. Code example
        3. What is it good for?
        4. What is it not so good for?
      7. The visitor design pattern
        1. Class diagram
        2. Code example
          1. The visitor design pattern the Scala way
        3. What is it good for?
        4. What is it not so good for?
      8. Summary
    18. 10. 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
        2. 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
          4. The names of the methods
          5. The monad laws
        2. Monads in real life
        3. Using monads
          1. The option monad
          2. More advanced monad example
        4. Monad intuition
      5. Summary
    19. 11. Functional Design Patterns – Applying What We Learned
      1. The lens design pattern
        1. Lens example
        2. Without the lens design pattern
          1. Immutable and verbose
          2. Using mutable properties
        3. With the lens design pattern
          1. Minimizing the boilerplate
      2. The cake design pattern
        1. Dependency injection
        2. Dependency injection libraries and Scala
        3. Dependency injection in Scala
          1. Writing our code
          2. Wiring it all up
          3. Unit testing our application
        4. Other alternatives
          1. Implicits
          2. Reader monad
      3. The pimp my library design pattern
        1. Using the 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. 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. Alternatives
        3. When to use duck typing
      10. Memoization
        1. Memoization example
        2. Alternatives
      11. Summary
    20. 12. Real-Life Applications
      1. Why should we use libraries?
      2. The Scalaz library
        1. Monoids in Scalaz
          1. Using monoids
        2. Testing monoids
        3. Monads in Scalaz
          1. Using monads
          2. Testing monads
        4. The possibilities of Scalaz
      3. Writing a complete application
        1. Application specifications
        2. Implementation
          1. The libraries to use
          2. Writing some code
          3. Wiring it all up
          4. The end result
        3. Testing
          1. Unit testing
          2. Application testing
        4. The future of our application
      4. Summary
    21. Index

Product information

  • Title: Scala Design Patterns
  • Author(s): Ivan Nikolov
  • Release date: February 2016
  • Publisher(s): Packt Publishing
  • ISBN: 9781785882500