views:

145

answers:

1

I am using the decorator pattern and am decorating a class with a constructor that has parameters.

Below is the constructor of the class decorating;

Public Sub New(ByVal repository As ISchedulingRespository)

  Me.repository = repository

  End Sub

Because my decorator class inherits from the decorated class I need to declare it's constructor as follows;

 Public Sub New(ByVal schedulingService as SchedulingService, ByVal repository As ISchedulingRespository)

        MyBase.New(repository)
        Me.instance = instance
   End Sub

So when I create the decorator class I pass in an instance of the class decoratoring as well as the parameter required for the class going to decorate. This can be seen below;

 Dim schedulingServiceDecorator As New SchedulingServiceEventDecorator(schedulingService, schedulingRepository)

This does not seem correct to me. Am I missing something with this pattern?

I could simply not pass in the decorating class in the decorator constructor, but every article seen on this pattern passes an instance of the class being decorated, into the decorator.

Is this a case of modifying a pattern to meet your needs?

Many Thanks

+4  A: 

In the Decorator Pattern, you are supposed to inherit from the interface of the decorated class, and then pass the implementation in the constructor. It looks like you are inheriting from the concrete SchedulingService class.

Ben Lings
+1, the Decorator does not inherit from the decorated class but from the interface. The decorated class is supposed to be passed in the constructor because you use the interface and only the constructor is implementation specific.
Daff
Thanks alot so simple! cheers
c00ke