Scala Design Patterns - Sample Chapter
Scala Design Patterns - Sample Chapter
Scala Design Patterns - Sample Chapter
ee
$ 54.99 US
34.99 UK
P U B L I S H I N G
Ivan Nikolov
pl
C o m m u n i t y
D i s t i l l e d
E x p e r i e n c e
Sa
m
Ivan Nikolov
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 purposesbig 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.
Preface
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 themwe 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.
Preface
The last point doesn't have to do much with design patterns. However, it is always a
good idea to build projects properly, as this makes it much easier to work in future.
[1]
Design patterns
Before delving into the Scala design patterns, we have to explain what they actually
are, why they exist, and why it is worth being familiar with them.
Software is a broad subject, and there are innumerable examples of things people
can do with it. At first glance, most of these things are completely differentgames,
websites, mobile phone applications, and specialized systems for different industries.
There are, however, many similarities in how software is built. Many times, people
have to deal with similar issues, no matter the type of software they create. For
example, computer games as well as websites might need to access a database.
And throughout time, by experience, developers learn how structuring their code
differs for the various tasks that they perform.
A formal definition for design patterns will allow you to understand the value of
using good practice in building elegant and efficient software.
The formal definition for design patterns
A design pattern is a reusable solution to a recurring problem
in software design. It is not a finished piece of code, but a
template, which helps solving the particular problem or
family of problems.
Design patterns are best practices, to which the software community has arrived
over a period of time. They are supposed to help write efficient, readable, testable,
and easily extendable code. In some cases, they can be a result of a programming
language not being expressive enough to elegantly achieve a goal. This means that
more feature-rich languages might not even need a design pattern, while others still
do. Scala is one of those rich languages, and in some cases, it makes the use of some
design patterns obsolete or simpler. We will see how exactly it does that in this book.
The lack or existence of a certain functionality within a programming language
also makes it able to implement additional design patterns that others cannot.
The opposite is also validit might not be able to implement things that others can.
Chapter 1
[3]
This book focuses on the design patterns from the point of view of Scala. As we
mentioned previously, Scala is a hybrid language. This leads us to a few famous
design patterns that are not needed anymoreone example is the null object design
pattern, which can simply be replaced by Scala's Option. Other design patterns
become possible using different approachesthe decorator design pattern can be
implemented using stackable traits. Finally, some new design patterns become
available, which are applicable specifically to the Scala programming languagethe
cake design pattern, pimp my library, and so on. We will focus on all of these and
make it clear where the richness of Scala helps us to make our code even cleaner
and simpler.
Even if there are many different design patterns, they can all be collated into a few
main groups:
Creational
Structural
Behavioral
Functional
Some of the design patterns that are specific to Scala can be assigned to the previous
groups. They can either be additional or replacements of the already existing ones.
They are typical to Scala and take advantage of some advanced language features
or simply features not available in other languages.
The first three groups contain the famous Gang of Four design patterns. Every design
pattern book covers them and so will we. The rest, even if they can be assigned to
one of the first three groups, will be specific to Scala and functional programming
languages. In the next few subsections, we will explain the main characteristics of
these groups and briefly present the actual design patterns that fall under them.
Hiding details about the actual creation and how objects are combined
[4]
Chapter 1
The following few sections give a brief definition of what these patterns are. They will
be looked at in depth individually later in this book.
[5]
Chapter 1
The next subsections will put some light on what these patterns are about before we
delve in them later in this book.
[7]
[8]
Chapter 1
In this book, we will focus our attention on the following behavioral design patterns:
[9]
Parameter values
The client then decides which commands to be executed and when by the invoker.
This design pattern can easily be implemented in Scala using the by-name
parameters feature of the language.
[ 10 ]
Chapter 1
[ 11 ]
Monoids
Monads
Functors
After we've looked at some Scala functional programming concepts, and we've
been through these, we will mention some interesting design patterns from the
Scala world.
A brief explanation of the preceding listed patterns will follow in the next few
subsections.
[ 12 ]
Chapter 1
Monoids
Monoid is a concept that comes from mathematics. We will take a look at it in more
detail with all the theory needed to understand it later in this book. For now, it will
be enough to remember that a monoid is an algebraic structure with a single associative
binary operation and an identity element. Here are the keywords you should remember:
The identity element. This means a+i = i+a = a. Here, the identity is i.
What is important about monoids is that they give us the possibility to work with
many different types of values in a common way. They allow us to convert pairwise
operations to work with sequences; the associativity gives us the possibility for
parallelization, and the identity element allows us to know what to do with empty
lists. Monoids are great to easily describe and implement aggregations.
Monads
In functional programming, monads are structures that represent computations as
sequences of steps. Monads are useful for building pipelines, adding operations with
side effects cleanly to a language where everything is immutable, and implementing
compositions. This definition might sound vague and unclear, but explaining
monads in a few sentences seems to be something hard to achieve. Later in this book,
we will focus on them and try and clear things up without the use of a complex
mathematical theory. We will try to show why monads are useful and what they
can help with, as long as developers understand them well.
Functors
Functors come from a category theory, and as for monads, it takes time to explain
them properly. We will look at functors later in this book. For now, you could
remember that functors are things that can allow us to lift a function of the type
A => B to a function of the type F[A] => F[B].
[ 13 ]
Pimp my library
Stackable traits
Lazy evaluation
Partial functions
Implicit injection
Duck typing
Memoization
The next subsections will give you some brief information about these patterns
before we properly study them later in this book.
Pimp my library
Many times, engineers need to work with libraries, which are made to be as generic
as possible. Sometimes, we need to do something more specific to our use case,
though. The pimp my library design pattern provides a way to write extension
methods for libraries, which we cannot modify. We can also use it for our own
libraries as well. This design pattern also helps to achieve a better code readability.
[ 14 ]
Chapter 1
Stackable traits
The stackable traits is the Scala way to implement the decorator design pattern. It can
also be used to compose functions, and it's based on a few advanced Scala features.
Lazy evaluation
Many times, engineers have to deal with operations that are slow and/or expensive.
Sometimes, the result of these operations might not even be needed. Lazy evaluation
is a technique that postpones the operation execution until it is actually needed. It
could be used for application optimization.
Partial functions
Mathematics and functional programming are really close together. As a consequence,
there are functions that exist that are only defined for a subset of all the possible input
values they can get. A popular example is the square root function, which only works
for non-negative numbers. In Scala, such functions can be used to efficiently perform
multiple operations at the same time or to compose functions.
Implicit injection
Implicit injection is based on the implicit functionality of the Scala programming
language. It automatically injects objects whenever they are needed, as long as
they exist in a specific scope. It can be used for many things, including dependency
injection.
Duck typing
This is a feature that is available in Scala and is similar to what some dynamic
languages provide. It allows developers to write code, which requires the callers to
have some specific methods (but not implement an interface). When someone uses
a method with a duck type, it is actually checked during compile time whether the
parameters are valid.
[ 15 ]
Memoization
This design pattern helps with optimization by remembering function results, based
on the inputs. This means that as long as the function is stable and will return the
same result when the same parameters are passed, one can remember its results
and simply return them for every consecutive identical call.
Is this piece of code going to be fairly static or will it change in the future?
[ 16 ]
Chapter 1
Installing Scala
Of course, you will need the Scala programming language. It evolves quickly, and
the newest version could be found at http://www.scala-lang.org/download/.
There are a few tips about how to install the language in your operating system at
http://www.scala-lang.org/download/install.html.
Tips about installing Scala
You can always download multiple versions of Scala and experiment with
them. I use Linux and my tips will be applicable to Mac OS users, too.
Windows users can also do a similar setup. Here are the steps:
Install Scala under /opt/scala-{version}/ or any other path you
prefer. Then, create a symlink using the following command: sudo ln
-s /opt/scala-{version} scala-current. Finally, add the path
to the Scala bin folder to your .bashrc (or equivalent) file using the
following lines: export SCALA_HOME=/opt/scala-current and
export PATH=$PATH:$SCALA_HOME/bin. This allows us to quickly
change versions of Scala by just redefining the symlink.
Another way to experiment with any Scala version is to install SBT (you
can find more information on this). Then, simply run sbt in your console,
type ++ 2.11.7 (or any version you want), and then issue the console
command. Now you can test Scala features easily.
Using SBT or Maven or any other build tool will automatically download
Scala for you. If you don't need to experiment with the console, you can
skip the preceding steps.
Using the preceding tips, we can use the Scala interpreter by just typing scala in
the terminal or follow the sbt installation process and experiment with different
language features in the REPL.
Scala IDEs
There are multiple IDEs out there that support development in Scala. There is
absolutely no preference about which one to use to work with the code. Some
of the most popular ones are as follows:
IntelliJ
Eclipse
NetBeans
They contain plugins to work with Scala, and downloading and using them should
be straightforward.
[ 17 ]
Dependency management
Running most of the examples in this book will not require any additional
dependencies in terms of special libraries. In some cases, though, we might need to
show how a Scala code is unit tested, which will require us to use a testing framework.
Also, later we might present some real-life use cases in which an additional library
is used. Dealing with dependencies nowadays is done using specialized tools. They
usually are interchangeable, and which one to use is a personal choice. The most
popular tool used with Scala projects is SBT, but Maven is also an option, and there
are many others out there as well.
Modern IDEs provide the functionality to generate the required build configuration
files, but we will give some generic examples that could be useful not only here, but
in future projects. Depending on the IDE you prefer, you might need to install some
extra plugins to have things up and running, and a quick Google search should help.
SBT
SBT stands for Simple Build Tool and uses the Scala syntax to define how a project
is built, managing dependencies, and so on. It uses .sbt files for this purpose. It also
supports a setup based on Scala code in .scala files, as well as a mix of both.
To download SBT, go to http://www.scala-sbt.org/download.html.
The following screenshot shows the structure of a skeleton SBT project:
[ 18 ]
Chapter 1
It contains information about how to build the assembly JARa merge strategy, final
JAR name, and so on. It uses a plugin called sbtassembly (https://github.com/
sbt/sbt-assembly).
[ 19 ]
The build.sbt file is the file that contains the dependencies of the project, some
extra information about the compiler, and metadata. The skeleton file looks as
follows:
organization := "com.ivan.nikolov"
name := "skeleton-sbt"
scalaVersion := "2.11.7"
scalacOptions := Seq("-unchecked", "-deprecation", "-encoding",
"utf8")
javaOptions ++= Seq("-target", "1.8", "-source", "1.8")
publishMavenStyle := true
libraryDependencies ++= {
val sparkVersion = "1.2.2"
Seq(
"org.apache.spark" %% "spark-core" % sparkVersion %
"provided",
"com.datastax.spark" %% "spark-cassandra-connector" % "1.2.1",
"org.scalatest" %% "scalatest" % "2.1.3" % "test",
"org.mockito" % "mockito-all" % "1.9.5" % "test" // mockito
for tests
)
}
As you can see, here we define the Java version against which we compile some
manifest information and the library dependencies.
The dependencies for our project are defined in the libraryDependencies section of
our SBT file. They have the following format:
"groupId" %[%] "artifactId" % "version" [% "scope"]
[ 20 ]
Chapter 1
Finally, there is the project/plugins.sbt file that defines different plugins used to
get things up and running. We already mentioned (sbtassembly):
logLevel := Level.Warn
addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.0")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.13.0")
[ 21 ]
There are different plugins online that provide useful functionalities. Here are some
common sbt commands that can be run from the root folder in the terminal of this
skeleton project:
Useful SBT commands
sbt: This opens the sbt console for the current project. All of the
commands that will follow can be issued from here by omitting
the sbt keyword.
sbt test: This runs the application unit tests.
sbt compile: This compiles the application.
sbt assembly: This creates an assembly of the application
(a fat JAR) that can be used to run as any other Java JAR.
Maven
Maven holds its configuration in files named pom.xml. It supports multimodule
projects easily, while for sbt, there needs to be some extra work done. In Maven,
each module simply has its own child pom.xml file.
To download Maven, go to https://maven.apache.org/download.cgi.
The next screenshot shows the structure of a skeleton Maven project:
The main pom.xml file is much longer than the preceding sbt solution. Let's have a
look at its parts separately.
[ 22 ]
Chapter 1
There is usually some metadata about the project and different properties that can be
used in the POM files in the beginning:
<modelVersion>4.0.0</modelVersion>
<groupId>com.ivan.nikolov</groupId>
<artifactId>skeleton-mvn</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<scala.version>2.11.7</scala.version>
<scalatest.version>2.2.4</scalatest.version>
<spark.version>1.2.2</spark.version>
</properties>
Finally, there are the build definitions. Here, we can use various plugins to do
different things with our project and give hints to the compiler. The build definitions
are enclosed in the <build> tags.
[ 23 ]
The first plugin we have used is scala-maven-plugin, which is used when working
with Scala and Maven:
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
</configuration>
</plugin>
[ 24 ]
Chapter 1
</execution>
</executions>
</plugin>
Finally, we have the maven-assembly-plugin that is used for building the fat JAR of
the application:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
The complete pom.xml file is equivalent to the preceding sbt files that we presented.
As before, the Spark and Datastax dependencies are here just for illustration purposes.
Useful Maven commands
[ 25 ]
Summary
By now, we have a fair idea about what a design pattern means and how it can
affect the way we write our code. We've iterated the most famous design patterns
out there, and we have outlined the main differences between them. We saw that
in many cases, we could use Scala's features in order to make a pattern obsolete,
simpler, or different to implement compared to the classical case for pure objectoriented languages. This book will show you how Scala makes it easier to write
high-quality code.
Knowing what to look for when picking a design pattern is important, and you
should already know what specific details to watch out for and how important
specifications are.
Last but not least, we advise you to run the examples in this book, and we have
provided some pointers that should make this really easy. In some cases, creating
a complete solution using SBT or Maven might be too much hassle and something
unnecessary, but we believe it is a good practice to follow. Additionally, the
approaches we explained are used throughout the industry and will be beneficial
outside the scope of this book.
In the next chapter, we will get straight to the practical part of this book, where we
will look at traits and mixing compositions, what they are useful for, and how and
when to use them.
[ 26 ]
www.PacktPub.com
Stay Connected: