Socket
Book a DemoInstallSign in
Socket

github.com/filipeandrade6/go-design-patterns/structural/decorator

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/filipeandrade6/go-design-patterns/structural/decorator

v0.0.0-20210411165548-2322ad37e3b6
Source
Go
Version published
Created
Source

Decorator Design Pattern

Refactoring Guru

Decorator design pattern is a structural design pattern. It lets you provide additional functionality or decorates an object without altering that object.

It is better understood with an example. Imagine you are opening a pizza chain. You started with two kinds of pizzas

  • Veggie Mania Pizza
  • Peppy Tofu pizza

Each of the above pizza had its price. So you would create a pizza interface as below

package main

type pizza interface {
	getPrice() int
}

You need to also create two pizza struct with a getPrice function which will return the price. These two pizza structs implement the pizza interface as they define the getPrice() method

Later on, you started to offer toppings along with the pizza with some additional price for each of the topping. So the original base pizza now needs to be decorated with a topping. Imaging you added below two toppings to the menu

  • Tomato Topping
  • Cheese Topping

Also, remember that pizza along with the topping is also a pizza. Customer can choose their pizza in different ways. For eg

  • Veggie mania with tomato topping
  • Veggie main with tomato and cheese topping
  • Peppy Paneer pizza without any topping
  • Peppy Paneer pizza with cheese topping

So how would you design now given that you now have the toppings as well. Decorator pattern will come into picture. It can help additional functionality without actually modifying any of the existing structs. Decorator pattern recommends in this case to create separate structs for each of the topping available. Each topping struct will implement the pizza interface above and also have an embed and instance of pizza.

We now have separate structs for different types of pizza and separate struct for the types of topping available. Each of the pizza and topping has its own price. And whenever you add any topping to a pizza then price of that topping is added to the price of base pizza and that is how you get a resultant price.

So the decorator pattern let’s you decorate the original base pizza object without altering that pizza object. The pizza object knows nothing about toppings. It just knows its price and nothing else.

UML Diagram

Below is UML diagram for the decorator design pattern

uml diagram with example

The concrete component (Veggie Mania and Peppy Tofu here) and concrete decorator (Toppings here) implement the component interface (Pizza here). Also concrete decorator would embed an instance of component as well.

As in below example we have

  • The component is represented by pizza interface
  • The concrete component is represented by veggieMania and peppyPanner struct. They both implement the pizza interface
  • Concrete decorators are represented by cheeseTopping and tomatoTopping struct. They both implement the pizza interface. Also, they embed an instance of type pizza as well

FAQs

Package last updated on 11 Apr 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.