Scala Design Patterns
上QQ阅读APP看书,第一时间看更新

Preface

Software engineering and design has existed for many years now. We use software almost everywhere in our lives, and this makes programs extremely distinct in terms of the problems they solve.

Regardless of the number of things that can be done with programming, there are still some specific features that repeat over and over again. Over time, people have come up with some best practices that help to tackle specific patterns that emerge in programs. These are called design patterns.

Design patterns solve not only commonly occurring problems, but also deal with language limitations. No matter what the specific design patterns are and what single issue they solve, all of them in the end aim at producing better software. This includes improved readability, simplicity, easier maintainability, testability, extendibility, and efficiency. Today, design patterns are an important part of every good software engineer's arsenal.

Together with the large number of problems that we tackle with programming, there are also many languages that we can use. Every language is different and has its strengths and weaknesses, so we also need to take this into consideration when doing something. In this book, we will look at design patterns from the point of view of Scala.

Scala has become extremely popular in the last couple of years. Many companies use it in production for various purposes—big data processing, writing APIs, machine learning, and so on. Switching to Scala from popular languages, such as Java, turns out to be quite simple because it is a hybrid of an object-oriented language and a functional programming language. Using Scala to its full potential, however, requires us to be familiar with not only the object-oriented features, but also with the functional ones. The use of Scala could improve performance and the time it takes to implement the features. One of the reasons is the really high expressivity of Scala.

The fact that Scala is close to object-oriented languages means that many of the design patterns for object-oriented programming are still applicable here. The fact that it is also functional means that some other design patterns also applicable, and some of the original ones could be modified to better fit the paradigm of Scala. In this book, we will be focusing on all of them—we will go through some specific features of Scala and then we will look at the popular Gang of Four design patterns viewed from the Scala perspective. We will also become familiar with design patterns that are exclusive to Scala, and we will understand different functional programming concepts, including monoids and monads. Having meaningful examples always makes learning and understanding easier. We will try to provide examples that you can easily map to real problems that you would potentially be solving. We will also introduce some libraries, which will be useful for anyone who writes real-world applications.

What this book covers

Chapter 1, The Design Patterns Out There and Setting Up Your Environment, is a brief introduction to design patterns, why they exist, and their different types. This chapter also provides you with tips on how you can set up your environment in order to easily run the examples in the book.

Chapter 2, Traits and Mixin Compositions, talks about traits and mixin compositions in Scala, multiple inheritance, and the rules of linearization that the Scala programming language uses when extending multiple traits.

Chapter 3, Unification, covers the various unifications that exist in Scala, which make it as expressive as it is.

Chapter 4, Abstract and Self Types, covers the different types of polymorphism that exist in Scala that help to make generic and extendible software.

Chapter 5, Aspect-Oriented Programming and Components, shows the concept of aspect-oriented programming and how it can be applied to Scala. This chapter also explains what components are and how to build applications using multiple small and simple components.

Chapter 6, Creational Design Patterns, covers the most popular creational design patterns from the Gang of Four. All patterns are viewed from the point of view of Scala and alternatives are shown where applicable.

Chapter 7, Structural Design Patterns, goes through the most popular structural design patterns from the Gang of Four from the Scala point of view. This chapter also shows Scala alternatives where this is applicable and gives usage advice.

Chapter 8, Behavioral Design Patterns – Part 1, covers Part 1 of behavioral design patterns from the Gang of Four viewed from the Scala perspective. This chapter also provides examples and usage advice.

Chapter 9, Behavioral Design Patterns – Part 2, covers Part 2 of behavioral design patterns from the Gang of Four viewed from the Scala perspective. This chapter also provides examples and usage advice.

Chapter 10, Functional Design Patterns – The Deep Theory, delves into pure functional programming concepts such as monoids, functors, and monads. This chapter also explains these concepts in an understandable way, along with some examples.

Chapter 11, Functional Design Patterns – Applying What We Learned, presents design patterns that are specific to Scala. It is loaded with examples along with theory and usage advice.

Chapter 12, Real-Life Applications, introduces you to the Scalaz library. You will write a complete application that applies many of the concepts learned in the book, and you will finish off with a summary.

What you need for this book

You will need to install an IDE that supports the Scala programming language (IntelliJ IDEA or Eclipse), even though a text editor also works well to read the code. You will need to have Maven and Scala (not 100% required as Maven will take care of downloading Scala for the purposes of compilation).

The examples in the book were written and tested on a Unix-based operating system; however, they should also successfully compile and run on Windows.

Who this book is for

You are a software engineer who already has some knowledge of Scala, but wants to get more practical understanding of how to apply it in real-world application development, or you might simply want to have a useful reference to consult while designing applications. Having an understanding of the importance of using best practices and writing nice code is good; however, even if you don't, hopefully you will be convinced by the time you finish reading this book. Prior knowledge of design patterns is not required, but if you are familiar with some, this book will be useful, as we will be looking at them from the point of view of Scala.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

object IOAction {
  
  def apply[T](result: => T): IOAction[T] =
    new SimpleAction[T](result)
  
  private class SimpleAction[T](result: => T) extends IOAction[T] {
    override def apply(state: State): (State, T) = 
      (state.next, result)
  }
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

abstract class FileIO {
  // this makes sure nobody can create a state
  private class FileIOState(id: Int) extends State {
    override def next: State = new FileIOState(id + 1)
  }

  def run(args: Array[String]): Unit = {
    val action = runIO(args(0), args(1))
    action(new FileIOState(0))
  }

  def runIO(readPath: String, writePath: String): IOAction[_]
}

New terms and important words are shown in bold.

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.