DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Design Patterns for GenAI Creative Systems in Advertising
  • From APIs to Actions: Rethinking Back-End Design for Agents
  • Why SAP S/4HANA Landscape Design Impacts Cloud TCO More Than Compute Costs
  • Lambda-Driven API Design: Building Composable Node.js Endpoints With Functional Primitives

Trending

  • Alternative Structured Concurrency
  • Jakarta EE 12: Entering the Data Age of Enterprise Java
  • RAG Is Not Enough: Advanced Retrieval Architectures Using Vertex AI Search on GCP
  • Architecting Zero-Trust AI Agents: How to Handle Data Safely
  1. DZone
  2. Coding
  3. Languages
  4. How to Use the Decorator Design Pattern in Scala

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!

By 
Nancy Jain user avatar
Nancy Jain
·
Jul. 26, 18 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
21.0K Views

Join the DZone community and get the full member experience.

Join For Free

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:

image (1)

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
    pizza.addNewTopping("Cheese")
    pizza.addNewTopping("Onion")
    println(s"You have ordered ${pizza.getName}")
    println(s"You have to pay Rupees ${pizza.getPrice}")
}


trait Topping {
    def getName() : String

    def getPrice() : Double

    def addTopping() : Topping
}


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
Design Scala (programming language)

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

Opinions expressed by DZone contributors are their own.

Related

  • Design Patterns for GenAI Creative Systems in Advertising
  • From APIs to Actions: Rethinking Back-End Design for Agents
  • Why SAP S/4HANA Landscape Design Impacts Cloud TCO More Than Compute Costs
  • Lambda-Driven API Design: Building Composable Node.js Endpoints With Functional Primitives

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook