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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Security by Design: Building Full-Stack Applications With DevSecOps
  • Mastering Advanced Aggregations in Spark SQL
  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  • Thermometer Continuation in Scala

Trending

  • Using Python Libraries in Java
  • Bridging UI, DevOps, and AI: A Full-Stack Engineer’s Approach to Resilient Systems
  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks
  • Proactive Security in Distributed Systems: A Developer’s Approach
  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
20.8K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Security by Design: Building Full-Stack Applications With DevSecOps
  • Mastering Advanced Aggregations in Spark SQL
  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  • Thermometer Continuation in Scala

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!