views:

88

answers:

2

VB.NET .NET 3.5

I have an aggregate class called Package as part of a shipping 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?

A: 

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; }
    }
}
Anton Gogolev
I think I get what you are saying, but let me make sure. If I need the length of the box from BoxType, I would create a new property in Package that implements IRateable.GetBoxLength. That property would simply call BoxType.Length and return it?
Casey
Thanks Anton, no problem with C#. I'm going to give you the answer since you posted it first, I hope this is correct etiquette.
Casey
+2  A: 

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
Justin Niessner
@Jon Thanks for the conversion! My VB.NET Fu is a little rusty.
Justin Niessner
@Justin was thinking of an answer almost exactly the same as yours when yours already popped up. Thought that I might as well just convert it. :)
Jon Limjap
Thanks guys, I don't know why I was over thinking this so much. Ever have one of those days where you make everything harder than it is?
Casey