开发者

Can an interface be implemented across an aggregate/composite class in vb.net?

开发者 https://www.devze.com 2022-12-25 16:36 出处:网络
VB.NET .NET 3.5 I have an aggregate class called Package as part of a shipp开发者_JAVA百科ing system.Package contains another class, BoxType .BoxType contains information about the box used to ship t

VB.NET .NET 3.5

I have an aggregate class called Package as part of a shipp开发者_JAVA百科ing system. Package contains another class, BoxType . BoxType contains information about the box used to ship the package, such as length, width, etc. of the Box.

Package has a method called GetShippingRates. This method calls a separate helper class, ShipRater, and passes the Package itself as an argument. ShipRater examines the Package as well as the BoxType, and returns a list of possible shipping rates/methods.

What I would like to do is construct an Interface, IRateable, that would be supplied to the helper class ShipRater. So instead of:

Class ShipRater
    Sub New(SomePackage as Package)
    End Sub
End Class

we would do:

 Class ShipRater
    Sub New(SomeThingRateable as IRateable)
    End Sub
End Class

However, ShipRater requires information from both the Package and its aggregate, BoxType. If I write an interface IRateable, then how can I use the BoxType properties to implement part of the Interface? Is this possible?


The interface wouldn't need to know anything about how you aggregate the calls (it could be assumed that not all Rateable items need to agregate calls). Leave the aggregation (via delegation) calls up to Package:

Public Interface IRateable
    Function GetRate() As Double
End Interface

Public Class Package Implements IRateable
    Dim _boxType As New BoxType()

    'Rest of implementation'
    Public Function GetRate() As Double Implements IRateable.GetRate
        Return _boxType.Rate()
    End Function
End Class


Sure. Just delegate required calls to an aggregated BoxType. Sorry for C#:

class Package : IRateable
{
    private float weight;
    private BoxType box;

    Size IRateable.Dimensions
    {
        get { return box.Dimensions; }
    }

    float IRateable.Weight
    {
        get { return weight; }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号