Scala Design Patterns.
eBook - ePub

Scala Design Patterns.

Ivan Nikolov

Condividi libro
  1. 396 pagine
  2. English
  3. ePUB (disponibile sull'app)
  4. Disponibile su iOS e Android
eBook - ePub

Scala Design Patterns.

Ivan Nikolov

Dettagli del libro
Anteprima del libro
Indice dei contenuti
Citazioni

Informazioni sul libro

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.

Domande frequenti

Come faccio ad annullare l'abbonamento?
È semplicissimo: basta accedere alla sezione Account nelle Impostazioni e cliccare su "Annulla abbonamento". Dopo la cancellazione, l'abbonamento rimarrà attivo per il periodo rimanente già pagato. Per maggiori informazioni, clicca qui
È possibile scaricare libri? Se sì, come?
Al momento è possibile scaricare tramite l'app tutti i nostri libri ePub mobile-friendly. Anche la maggior parte dei nostri PDF è scaricabile e stiamo lavorando per rendere disponibile quanto prima il download di tutti gli altri file. Per maggiori informazioni, clicca qui
Che differenza c'è tra i piani?
Entrambi i piani ti danno accesso illimitato alla libreria e a tutte le funzionalità di Perlego. Le uniche differenze sono il prezzo e il periodo di abbonamento: con il piano annuale risparmierai circa il 30% rispetto a 12 rate con quello mensile.
Cos'è Perlego?
Perlego è un servizio di abbonamento a testi accademici, che ti permette di accedere a un'intera libreria online a un prezzo inferiore rispetto a quello che pagheresti per acquistare un singolo libro al mese. Con oltre 1 milione di testi suddivisi in più di 1.000 categorie, troverai sicuramente ciò che fa per te! Per maggiori informazioni, clicca qui.
Perlego supporta la sintesi vocale?
Cerca l'icona Sintesi vocale nel prossimo libro che leggerai per verificare se è possibile riprodurre l'audio. Questo strumento permette di leggere il testo a voce alta, evidenziandolo man mano che la lettura procede. Puoi aumentare o diminuire la velocità della sintesi vocale, oppure sospendere la riproduzione. Per maggiori informazioni, clicca qui.
Scala Design Patterns. è disponibile online in formato PDF/ePub?
Sì, puoi accedere a Scala Design Patterns. di Ivan Nikolov in formato PDF e/o ePub, così come ad altri libri molto apprezzati nelle sezioni relative a Informatica e Programmazione in Java. Scopri oltre 1 milione di libri disponibili nel nostro catalogo.

Informazioni

Anno
2018
ISBN
9781788472098
Edizione
2
Argomento
Informatica

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...

Indice dei contenuti