decorator pattern help

R

rodchar

hey all,
i'm trying to use the decorator pattern example from a book but it's using
java for the syntax. below is my attempt in c# but not having any luck:

using System;

namespace Ch3Decorator
{
public class Computer
{
public string Description()
{
return "computer";
}
}

public abstract class ComponentDecorator : Computer
{
public abstract string Description();
}

public class Disk : ComponentDecorator
{
Computer c;
public Disk(Computer c)
{
this.c = c;
}
public new string Description()
{
return c.Description() + " " + "and a disk";
}
}

public class Program
{
static void Main(string[] args)
{
Computer c = new Computer();
c = new Disk(c);
Console.WriteLine("I'm getting a {0}", c.Description());
}
}
}

(color code format: http://pastebin.com/md08b089)

all i'm trying to get it to do is say "i'm getting a computer and a disk" to
demonstrate extending the Computer class.

thanks,
rodchar
 
R

rodchar

ok,

i took out the new keyword and put override in its place.
public class Program
{
static void Main(string[] args)
{
Computer c = new Computer();
c = new Disk(c);
Console.WriteLine("I'm getting a {0}", c.Description());
}
}

When i step through c.Description in the main it's going to the Computer
object to get the description. I thought it was supposed to go get the
description from Disk object since that was the one istantiated.

In the book, it says the result in the display is supposed to say

"I'm getting a computer and a disk"

but what is says for me is

"I'm getting a computer"
 
I

Ignacio Machin ( .NET/ C# MVP )

hey all,
i'm trying to use the decorator pattern example from a book but it's using
java for the syntax. below is my attempt in c# but not having any luck:

using System;

namespace Ch3Decorator
{
    public class Computer
    {
        public string Description()
        {
            return "computer";
        }
    }

    public abstract class ComponentDecorator : Computer
    {
        public abstract string Description();
    }

    public class Disk : ComponentDecorator
    {
        Computer c;
        public Disk(Computer c)
        {
            this.c = c;
        }
        public new string Description()
        {
            return c.Description() + " " + "and a disk";
        }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            Computer c = new Computer();
            c = new Disk(c);
            Console.WriteLine("I'm getting a {0}", c.Description());
        }
    }

}

(color code format:http://pastebin.com/md08b089)

all i'm trying to get it to do is say "i'm getting a computer and a disk"to
demonstrate extending the Computer class.

thanks,
rodchar

Hi the Description method is not virtual, that's why are getting a
"computeR"

if you change :
Computer c = new Computer();
c = new Disk(c);

to
Computer c = new Computer();
Disk d = new Disk(c);
d.Description() will be correct.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top