VB.NET Interfaces

2019-03-16 08:44发布

问题:

I am not quite clear as to why or when to use Interfaces. Can someone post a complete, simple and small example of an Interface using VB.NET in a Console Application. How is it extensible?

回答1:

In short: Favor Composition over Inheritance

Interfaces are simply a common set of member definitions that you want one or more classes to support. The key is that you have to provide the functionality explicitly when you implement an interface.

You can achieve similar results using inheritance since two sub-classes can inherit fully-functional members from the base. But the downside of inheritance is that your sub-classes end up having a hard dependency on the base class.

Consider the following classes:

Public Class Car
   Publc Sub OpenDoor(ByVal key As MyKey)
      Console.WriteLine("Access granted to car.")
   End Sub
End Class

Public Class House
   Public Sub OpenDoor(ByVal key as MyKey)
      Console.WriteLine("Access granted to house.")
   End Sub
End Class

You could say that these two classes are somewhat related because they both have an OpenDoor() method. You might be tempted to even create a base class to extract common functionality.

Public Class OpenableProperty
   Public Sub OpenDoor(ByVal key As MyKey)
      Console.WriteLine("Access granted to property.")
   End Sub
End Class

Public Class Car
   Inherits OpenableProperty
End Class

Public Class House
   Inherits OpenableProperty
End Class

You could then use this abstraction like this:

Public Class SecurityService
   Public Sub InspectProperty(ByVal item As OpenableProperty)
      Dim key As New MyKey()
      Console.WriteLine("Inspecting property...")
      item.OpenDoor(key)
   End Sub
End Class

However, relating a house to a car based solely on the fact that you can access them with a key is a pretty weak abstraction. Heck, even a can of beans can be openable!

But there are other points where relation might occur as well. For example, both a car and a house might have air conditioning:

Public Class Car
   Inherits OpenableProperty
   Public Sub TurnOnAirConditioning()
      Console.WriteLine("Cool breezes flowing in car!")
   End Sub
End Class

Public Class House
   Inherits OpenableProperty
   Public Sub TurnOnAirConditioning()
      Console.WriteLine("Cool breezes flowing in house!")
   End Sub
End Class

Should TurnOnAirConditioning() be extracted to the base class too? What does it have to do with being an OpenableProperty? Could a JewelrySafe class inherit from OpenableProperty without an AC? The better answer in this situation is to extract Interfaces and use these to compose the functionality in our classes rather than inherit:

Public Interface IOpenable
   Sub OpenDoor(ByVal key As MyKey)
End Interface

Public Interface ICoolable
   Sub TurnOnAirConditioning()
End Interface

Public Class Car
   Implements IOpenable, ICoolable
   Public Sub OpenDoor(ByVal key as MyKey) Implements IOpenable.OpenDoor()
      Console.WriteLine("Access granted to car.")
   End Sub
   Public Sub TurnOnAirConditioning() Implements ICoolable.TurnOnAirConditioning()
      Console.WriteLine("Cool breezes flowing in car!")
   End Sub
End Class

Public Class House
   Implements IOpenable, ICoolable
   Public Sub OpenDoor(ByVal key as MyKey) Implements IOpenable.OpenDoor()
      Console.WriteLine("Access granted to house.")
   End Sub
   Public Sub TurnOnAirConditioning() Implements ICoolable.TurnOnAirConditioning()
      Console.WriteLine("Cool breezes flowing in house!")
   End Sub
End Class

Public Class JewelrySafe
   Implements IOpenable
   Public Sub OpenDoor(ByVal key as MyKey) Implements IOpenable.OpenDoor()
      Console.WriteLine("Access granted to jewelry safe.")
   End Sub
End Class

Then your abstractions can be consumed as such:

Public Class SecurityService
   Public Sub InspectProperty(ByVal item As IOpenable)
      Dim key As New MyKey()
      Console.WriteLine("Inspecting property...")
      item.OpenDoor(key)
   End Sub
End Class

Public Class ThermostatService
   Public Sub TestAirConditioning(ByVal item as ICoolable)
      Console.WriteLine("Testing Air Conditioning...")
      item.TurnOnAirConditioning()
   End Sub
End Class

The SecurityService could then be used to inspect the Car, House, and JewelrySafe, while the ThermostatService could be used only to test the AC of the Car and House.

Sub Main()
   Dim securityService As New SecurityService()
   Dim thermostatService As New ThermostatService()

   Dim house As New House()
   Dim car As New Car()
   Dim jewelrySafe As New JewelrySafe()

   With securityService
      .InspectProperty(house)
      .InspectProperty(car)
      .InspectProperty(jewelrySafe)
   End With

   With thermostatService
      .TestAirConditioning(house)
      .TestAirConditioning(car)
   End With
End Sub

Which should produce the following results:

Inspecting property...
Access granted to house.
Inspecting property...
Access granted to car.
Inspecting property...
Access granted to jewelry safe.
Testing Air Conditioning...
Cool breezes flowing in house!
Testing Air Conditioning...
Cool breezes flowing in car!


回答2:

Consider this simple interface:

Public Interface IWeightedValue
    Public ReadOnly Property Weight As Double
    Public ReadOnly Property Value As Double
End Interface

Without even writing any more code, I can start dealing with this concept in other parts of my code. For instance:

Public Function GetWeightedAverage(ByVal points As IEnumerable(Of IWeightedValue)) As Double
    Dim totalWeight As Double = 0.0
    Dim totalWeightedValue As Double = 0.0

    For Each point As IWeightedValue in points
        totalWeight += point.Weight
        totalWeightedValue += (point.Weight * point.Value)
    Next

    Return totalWeightedValue / totalWeight
End Function

And voila -- now for any class I write, if I just make it implement IWeightedValue then I can calculate the weighted average for a collection of instances of this class.