Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 749619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:27:19+00:00 2026-05-14T14:27:19+00:00

I was studying the Decorator Pattern as documented in GOF . Please, help me

  • 0

I was studying the Decorator Pattern as documented in GOF.

Please, help me understand the Decorator Pattern. Could someone give a use-case example of where this is useful in the real world?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:27:20+00:00Added an answer on May 14, 2026 at 2:27 pm

    Decorator pattern achieves a single objective of dynamically adding
    responsibilities to any object.

    Consider a case of a pizza shop. In the pizza shop they will sell few pizza varieties and they will also provide toppings in the menu. Now imagine a situation wherein if the pizza shop has to provide prices for each combination of pizza and topping. Even if there are four basic pizzas and 8 different toppings, the application would go crazy maintaining all these concrete combination of pizzas and toppings.

    Here comes the decorator pattern.

    As per the decorator pattern, you will implement toppings as decorators and pizzas will be decorated by those toppings’ decorators. Practically each customer would want toppings of his desire and final bill-amount will be composed of the base pizzas and additionally ordered toppings. Each topping decorator would know about the pizzas that it is decorating and it’s price. GetPrice() method of Topping object would return cumulative price of both pizza and the topping.

    EDIT

    Here’s a code-example of explanation above.

    public abstract class BasePizza
    {
        protected double myPrice;
    
        public virtual double GetPrice()
        {
            return this.myPrice;
        }
    }
    
    public abstract class ToppingsDecorator : BasePizza
    {
        protected BasePizza pizza;
        public ToppingsDecorator(BasePizza pizzaToDecorate)
        {
            this.pizza = pizzaToDecorate;
        }
    
        public override double GetPrice()
        {
            return (this.pizza.GetPrice() + this.myPrice);
        }
    }
    
    class Program
    {
        [STAThread]
        static void Main()
        {
            //Client-code
            Margherita pizza = new Margherita();
            Console.WriteLine("Plain Margherita: " + pizza.GetPrice().ToString());
    
            ExtraCheeseTopping moreCheese = new ExtraCheeseTopping(pizza);
            ExtraCheeseTopping someMoreCheese = new ExtraCheeseTopping(moreCheese);
            Console.WriteLine("Plain Margherita with double extra cheese: " + someMoreCheese.GetPrice().ToString());
    
            MushroomTopping moreMushroom = new MushroomTopping(someMoreCheese);
            Console.WriteLine("Plain Margherita with double extra cheese with mushroom: " + moreMushroom.GetPrice().ToString());
    
            JalapenoTopping moreJalapeno = new JalapenoTopping(moreMushroom);
            Console.WriteLine("Plain Margherita with double extra cheese with mushroom with Jalapeno: " + moreJalapeno.GetPrice().ToString());
    
            Console.ReadLine();
        }
    }
    
    public class Margherita : BasePizza
    {
        public Margherita()
        {
            this.myPrice = 6.99;
        }
    }
    
    public class Gourmet : BasePizza
    {
        public Gourmet()
        {
            this.myPrice = 7.49;
        }
    }
    
    public class ExtraCheeseTopping : ToppingsDecorator
    {
        public ExtraCheeseTopping(BasePizza pizzaToDecorate)
            : base(pizzaToDecorate)
        {
            this.myPrice = 0.99;
        }
    }
    
    public class MushroomTopping : ToppingsDecorator
    {
        public MushroomTopping(BasePizza pizzaToDecorate)
            : base(pizzaToDecorate)
        {
            this.myPrice = 1.49;
        }
    }
    
    public class JalapenoTopping : ToppingsDecorator
    {
        public JalapenoTopping(BasePizza pizzaToDecorate)
            : base(pizzaToDecorate)
        {
            this.myPrice = 1.49;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am studying CUDA based on the Visual Studio 2008. Could you please tell
i am self-studying asp.net and was trying some small projects to help understand the
I've been studying this: https://github.com/mikechambers/ExamplesByMesh/blob/master/JavaScript/QuadTree/src/QuadTree.js and I believe I understand the general idea about
Studying compilers course, I am left wondering why use registers at all. It is
After studying TCP/UDP difference all week, I just can't decide which to use. I
I am studying css and I do not understand why some OTHER text and
I'm studying how to use ZeroMQ together with EventMachine. To test things out, I
While studying for the SCJP 6 exam, I ran into this question in a
I keep studying this flow of the Facebook's bigpipe technique but I have this
Studying some code from a codeigniter tut, the following preg_match pattern has me baffled:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.