{{announcement.body}}
{{announcement.title}}

# How to Use the Decorator Design Pattern in Scala

DZone 's Guide to

# How to Use the Decorator Design Pattern in Scala

### Want to learn more about using the decorator design pattern? Check out this example on how to use this design pattern in Scala!

· Java Zone ·
Free Resource

Comment (1)

Save
{{ articles[0].views | formatCount}} Views

In this blog, we are going to discuss the decorator design patterns with Scala. In this tutorial, let’s say that I own a pizza outlet, and we know that everyone has very different tastes. Therefore, there can be various combinations of toppings.

If I have n number of toppings, I will have to create p(n) = 2 * p(n-1) + 1 subclasses:

p(0) = 0

p(1) = 2 * p(1-1) + 1 = 1

p(2) = 2 * p(2-1) + 1 = 2 * p(1) + 1 = 2 * 1 + 1 = 3

p(3) = 2 * p2 + 1 = 2 * 3 + 1 = 7

p(4) = 2 * p3 + 1 = 2 * 7 + 1 = 15

So, if I have three toppings, the number of subclasses will be p(3) = 7.

Okay, wow! My business is growing, and now, I want to expand it. So, I am going to add two more topping options for my valuable customers.

Now, when I have five topping options, the number of subclasses will be p(5) = 31. But, wait!
This is a very tedious task. What am I going to do now? Am I going to drop the idea of expanding the business? Nope! I am going to use the decorator design pattern to solve this problem.

## What Is a Design Pattern?

Design patterns are the best practices that a programmer can use to solve commonly-faced problems when designing an application or system.

It is not a finished design that can be transformed directly into the source code, but it is a description or template for how to solve a problem that can be used in many different situations.

## Decorator Design Pattern

The decorator design pattern is a structural design pattern. A structural design pattern focuses on the Class and Object composition. The decorator design pattern is all about adding responsibilities to objects dynamically. This pattern also gives some additional responsibility to our base class.

The decorator design pattern is about creating a decorator class that can wrap the original class and provides additional functionality, keeping the class methods signature intact. It is somewhat like the chain of responsibility pattern, with the difference being that, in the chain of responsibility pattern, exactly one of the classes handles the request, while in the decorator design pattern, all classes handle the request.

A design that uses the decorator often results in a system composed of lots of little objects that all look alike.

In the following example, we show the UML diagram that we follow with the decorator design pattern to solve our problem:

Firstly, we have created a Topping trait that is being implemented by the classes BasePizza and ToppingDecorator, and the Pizza class is composing it. The ToppingDecorator is further extended by classes CheeseTopping and OnionTopping.

``````class BasePizza extends Topping {
def getName() : String = "Pizza"

def getPrice() : Double = 77.0

def addTopping() : Topping = this
}``````

``````class CheeseTopping(override val topping : Topping) extends ToppingDecorator(topping) {
override def getPrice() : Double = {
super.getPrice() + 59.0
}

override def getName() : String = {
val previous = super.getName()
"Ocean Cheese " + previous
}
}``````

``````class OnionTopping(override val topping : Topping) extends ToppingDecorator(topping) {
override def getPrice() : Double = {
super.getPrice() + 39.0
}

override def getName() : String = {
val previous = super.getName()
"Sprinkled Onion " + previous
}
}``````

``````class Pizza {
var topping : Topping = new BasePizza

def getPrice() : Double = {
topping.getPrice()
}

def getName() : String = {
topping.getName()
}

def addNewTopping(toppingName : String) : Boolean = {
toppingName match
{
case "Onion" =>
{
this.topping = new OnionTopping(topping)
true
}
case "Cheese" =>
{
this.topping = new CheeseTopping(topping)
true
}
case _ =>
println("Topping unavailable! Better luck next time! :(")
false
}
}
}``````

``````object PizzaStore extends App {
val pizza = new Pizza
println(s"You have ordered \${pizza.getName}")
println(s"You have to pay Rupees \${pizza.getPrice}")
}``````

``````trait Topping {
def getName() : String

def getPrice() : Double

}``````

``````class ToppingDecorator(val topping : Topping) extends Topping {
var nextTopping : Topping = topping

def getName() : String = nextTopping.getName()

def getPrice() : Double = nextTopping.getPrice()

def addTopping() : Topping = this
}``````

Hope you liked the blog. Thanks for reading!

References:

• http://www.genericclass.com/java/decorator-design-pattern
• https://www.scala-lang.org/old/sites/default/files/FrederikThesis.pdf
Topics:
decorator design pattern, decorator pattern, design patterns, java, scala, tutorial, uml

Comment (1)

Save
{{ articles[0].views | formatCount}} Views

Published at DZone with permission of Nancy Jain , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.