Scala Design Patterns.
eBook - ePub

Scala Design Patterns.

Ivan Nikolov

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

Scala Design Patterns.

Ivan Nikolov

Book details
Book preview
Table of contents
Citations

About This Book

Learn how to write efficient, clean, and reusable code with ScalaAbout 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 efficientlyWho This Book Is ForIf 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 patternsIn DetailDesign 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 approachThe 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.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
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.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
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.
Do you support text-to-speech?
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.
Is Scala Design Patterns. an online PDF/ePUB?
Yes, you can access Scala Design Patterns. by Ivan Nikolov in PDF and/or ePUB format, as well as other popular books in Ciencia de la computación & Programación en Java. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781788472098

Behavioral Design Patterns – Part One

Our journey through the Scala design patterns has arrived at the group of behavioral design patterns. There are more members in this group than the others we've already been through, so we will split it into two separate parts. In this chapter, we will focus on the following behavioral design patterns:
  • Value object
  • Null object
  • Strategy
  • Command
  • Chain of responsibility
  • Interpreter
This chapter and the next one will give some clarity about what behavioral design patterns are, where they are useful, and how to implement them in Scala. We will be following a path similar to the previous chapters where we presented the patterns, showed a class diagram and a code example, and finally, gave a few hints about what to watch out for and where certain patterns are preferred to be used. Hopefully, you will get a feel for them and be able to confidently identify situations where they are applicable.

Defining behavioral design patterns

Behavioral design patterns, as the name suggests, are to do with behavior. Their purpose is to identify and implement common communication patterns between objects in an application. They define object interaction in such a way that the communication between objects is easy and coupling is still kept at a low level.
Behavioral design patterns describe how objects and classes interact with each other using messages. Contrary to creational and structural design patterns, the behavioral design patterns describe a flow or a process. This means that a developer should be really familiar with the actual process they are trying to implement. As with every other type of design pattern, behavioral design patterns exist in order to increase the testability, maintainability, and flexibility of the produced code.

The value object design pattern

In programming, there are different ways of comparing data. We can compare object identities or their values. These are useful in different scenarios and here, we will see what value objects are and when they can be used.
Value objects are small and simple immutable objects. Their equality is based not on identity, but on value equality.
Value objects are used to represent numbers, money, dates, and so on. They should be small and immutable; otherwise, changing values could cause bugs and unexpected behavior. They are quite useful in multithreaded applications due to their immutability. They are also commonly used as data transfer objects in enterprise applications.

An example class diagram

In languages such as Java, there is no direct support for value objects. What developers end up doing is to declare the fields as final and implement the hashCode and equals methods.
Immutability, however, is a concept that is pretty much enforced in Scala. We already saw the algebraic data types (ADTs) earlier—they also fall in the value object category. Case classes and tuples are also immutable and they are used to achieve the value object design pattern. The following class diagram shows an example of the value object design pattern in Scala:
This diagram really doesn't do anything special. It is a representation of a case class called Date. This is everything we need to do in order to achieve immutability and be able to implement the value object design pattern.

A code example

In our code example, we will use our Date class. Dates are quite commonly used in software products. Of course, there are libraries that provide complete functionality around date manipulations, but this should be good enough for an example. First of all, here is the Date class representation:
case class Date(
day: Int,
month: String,
year: Int
)
This is everything we need in order to get a value object. Scala does everything for us in the background by creating default implementations for the hashCode, equals, and toString methods. Case classes give us extra power, but this is out of this section's scope.
Now, let's use our Date class:
object DateExample {
def main(args: Array[String]): Unit = {
val thirdOfMarch = Date(3, "MARCH", 2016)
val fourthOfJuly = Date(4, "JULY", 2016)
val newYear1 = Date(31, "DECEMBER", 2015)
val newYear2 = Date(31, "DECEMBER", 2015)
System.out.println(s"The 3rd of March 2016 is the same as
the 4th of July 2016:
${thirdOfMarch == fourthOfJuly}")
System.out.println(s"The new year of 2015 is here twice:
${newYear1 == newYear2}")
}
}
As you can see, we used our object as values. We should note that here, we have absolutely no validation of the parameters; however, it is something easy to add, but not relevant for the current example. If we run our code now, we will see the following output:
Just to prove that case classes allow us to implement the value object design pattern easily and normal classes don't, let's try and change our Date class to a normal one and then use it in the same example. Our class will change to the following:
class BadDate(
day: Int,
month: String,
year: Int
)
Then, we will take the same example; however this time, we will use the BadDate, and since it's not a case class, we will create it with the new keyword:
object BadDateExample {
def main(args: Array[String]): Unit = {
val thirdOfMarch = new BadDate(3, "MARCH", 2016)
val fourthOfJuly = new BadDate(4, "JULY", 2016)
val newYear1 = new BadDate(31, "DECEMBER", 2015)
val newYear2 = new BadDate(31, "DECEMBER", 2015)
System.out.println(s"The 3rd of March 2016 is the same as the
4th of July 2016:
${thirdOfMarch == fourthOfJuly}")
System.out.println(s"The new year of 2015 is here twice:
${newYear1 == newYear2}")
}
}
The output of this exa...

Table of contents