[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Decorator Design Pattern - C#

Posted on 2011-10-21
7
Medium Priority
?
460 Views
Last Modified: 2013-12-16
Hi,

I have the code for "Decorator Design Pattern" in Java. Here is the article:

http://www.giantflyingsaucer.com/blog/?p=1084

Can you please convert the code to C# and make it working as expected ?

Thanks
0
Comment
Question by:milani_lucie
  • 4
  • 3
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37007462
The only thing you have to change is System.out.println to System.Console.Writeline. Everything else should be fine.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37007464
Sorry, one more change. Change extends to : (a colon).
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37007482
OK. I lied. One more change. The Override calls should be changed also. Remove @Override and insert override before the type in each call that has an Override attribute.
0
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 

Author Comment

by:milani_lucie
ID: 37007630
I did that. Still it is NOT giving me the desired result ! Here is the C# code : It has to show

Pizza: Tomato Sauce, Cheese
Pizza: Tomato Sauce, Cheese, Pepperoni, Olives

I have got:

Pizza: Tomato Sauce, Cheese
Pizza: Tomato Sauce, Cheese

Can you please FIX it ?

Thanks




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class Pizza
    {
        public String getOrder()
        {
            return "Pizza: Tomato Sauce, Cheese";
        }
    }

    public abstract class PizzaToppingDecorator : Pizza
    {
        public abstract String getOrder();
    }

    public class PepperoniTopping : PizzaToppingDecorator
    {
        private Pizza pizza;
        
        public PepperoniTopping(Pizza p)
        {
            this.pizza = p;
        }
        
        public override String getOrder()
        {
            return this.pizza.getOrder() + ", Pepperoni";
        }
    }

    public class OliveTopping : PizzaToppingDecorator
    {
        private Pizza pizza;
        
        public OliveTopping(Pizza p)
        {
            this.pizza = p;
        }
        
        public override String getOrder()
        {
            return this.pizza.getOrder() + ", Olives";
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Pizza pizza = new Pizza();
            Console.WriteLine(pizza.getOrder());

            // Now the customer adds pepperoni and olives
            
            pizza = new PepperoniTopping(pizza);
            pizza = new OliveTopping(pizza);
            Console.WriteLine(pizza.getOrder());
        }
    }
}

Open in new window

0
 

Author Comment

by:milani_lucie
ID: 37007654
Also i have got a warning at this line: public abstract String getOrder();

Can you please check why and what needs to be changed in that line ?

Thanks

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 37007805
Mark getOrder in the Pizza class as virtual and mark getOrder in PizzaToppingDecorator as override abstract.
0
 

Author Closing Comment

by:milani_lucie
ID: 37007985
Excellent ... That works really great !! Thanks for your help.

Thanks
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question