装饰一个构造函数有参数的类?

c00*_*0ke 1 design-patterns decorator

我正在使用装饰器模式,并使用具有参数的构造函数来装饰类.

下面是班级装饰的构造者;

Public Sub New(ByVal repository As ISchedulingRespository)

  Me.repository = repository

  End Sub
Run Code Online (Sandbox Code Playgroud)

因为我的装饰器类继承了装饰类,我需要声明它的构造函数如下;

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

        MyBase.New(repository)
        Me.instance = instance
   End Sub
Run Code Online (Sandbox Code Playgroud)

因此,当我创建装饰器类时,我传递了类装饰器的实例以及要装饰的类所需的参数.这可以在下面看到;

 Dim schedulingServiceDecorator As New SchedulingServiceEventDecorator(schedulingService, schedulingRepository)
Run Code Online (Sandbox Code Playgroud)

这对我来说似乎不正确.我错过了这种模式的东西吗?

我无法在装饰器构造函数中传递装饰类,但是在此模式上看到的每篇文章都将要装饰的类的实例传递给装饰器.

这是修改模式以满足您的需求吗?

非常感谢

Ben*_*ngs 7

Decorator模式中,您应该从装饰类的接口继承,然后在构造函数中传递实现.看起来你是从具体SchedulingService类继承而来的.