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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
  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!

Nancy Jain user avatar by
Nancy Jain
·
Jul. 26, 18 · Tutorial
Like (6)
Save
Tweet
Share
18.25K 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.

Popular on DZone

  • DZone's Article Submission Guidelines
  • Top Three Docker Alternatives To Consider
  • The Quest for REST
  • How To Create and Edit Excel XLSX Documents in Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: