Edit: First response was closure with a link. I visited the link and it did not answer my question. It used and example with methods not properties. In addition it shows wrapping 1 time. I am trying to do multiple times. Perhaps there are better examples or posts out there but I have not found them.
Trying to understand decorator pattern. The definition I have is its purpose is to "Attach additional responsibilities to an object dynamically,. Decorators provide a flexible alternative to sub-classing for extending functionality". Have looked at lots of samples and usually there is some virtual method that is overridden in all of the decorators, so I'm probably going about this the wrong way. I am trying to add properties. The code seems like it creates a hierarchy of wrapped objects, but I am not understanding the getting and setting of the properties. If I set them immediately after decorating, and then access them, they are available.
The example is a bicycle. A base bicycle has a color and size. I would like to be able to add optional pedals, and/or optional front suspension, and/or optional rear suspension by wrapping the bicycle with different combinations of decorators, and then be able to access all the bicycle, front suspension, rear suspension, and pedal properties. So the bicycle is 'decorated' (or is wrapped the right terminology?) multiple times. I am trying to avoid creating creating classes for all of the different combinations of options. Thanks in advance.
In this code the error occurs in the last statement in the Main() method.
void Main()
{
IBicycle mountainBike=new Bicycle();
mountainBike=new FrontSuspensionOption(mountainBike);// decorate with front suspension
mountainBike=new RearSuspensionOption(mountainBike); // decorate with rear suspension
mountainBike=new PedalOption(mountainBike);// decorate with optional pedals
// assign properties
mountainBike.Color="Black";
mountainBike.Size="Medium";
// trying to assign Front suspension
mountainBike.FrontSuspension="RockShox"; // error, but works if the property is assigned
// immediately after wrapping?
}
public interface IBicycle
{
string Size{get; set;}
string Color{get; set;}
}
public class Bicycle : IBicycle
{
public Bicycle(){}
public string Size{get; set;}
public string Color{get; set;}
}
public abstract class BicycleDecorator : IBicycle
{
private IBicycle decoratedBicycle;
protected BicycleDecorator(IBicycle bicycle)
{
decoratedBicycle=bicycle;
}
protected IBicycle DecoratedBicycle{get; private set;}
public string Size{get; set;}
public string Color{get; set;}
}
public class FrontSuspensionOption:BicycleDecorator
{
public FrontSuspensionOption(IBicycle bicycle):base(bicycle){}
string FrontSuspension {get; set;}
}
public class RearSuspensionOption:BicycleDecorator
{
public RearSuspensionOption(IBicycle bicycle):base(bicycle){}
string RearSuspension {get; set;}
}
public class PedalOption:BicycleDecorator
{
public PedalOption(IBicycle bicycle):base(bicycle){}
string Pedal {get; set;}
}