VB.NET Interfaces

I do not quite understand why and when to use interfaces. Can anyone post a complete, simple and small example of an interface using VB.NET in a console application. How is it extensible?

+6
class interface
source share
2 answers

In short: use composition over inheritance

Interfaces are simply a common set of element definitions that you want to support with one or more classes. The key is that you must explicitly provide functionality when implementing the interface.

You can achieve similar results using inheritance, since two subclasses can inherit fully functional elements from the database. But the disadvantage of inheritance is that your subclasses end up being heavily dependent 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 

We can say that these two classes are somewhat related to each other, because both of them have the OpenDoor () method. You may 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 

Then you can use this abstraction as follows:

 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, associating a house with a car, based solely on the fact that you can access them with a key, is a rather weak abstraction. Hell, even can beans can be opened!

But there are other points at which the relationship can occur. For example, both a car and a house can 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 retrieved into the base class? What does this have to do with being OpenableProperty? Can JewelrySafe class inherit from OpenableProperty without AC? The best answer in this situation is to extract the interfaces and use them to create functionality in our classes, rather than inheritance:

 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 used 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 

Then the SecurityService could be used to inspect cars, houses and jewelry, while ThermostatService could only be used to check the AC of the car and houses.

 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 

This should lead to 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! 
+29
source share

Consider this simple interface:

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

Without even writing more code, I can begin to understand this concept in other parts of my code. For example:

 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 that I write, if I just implement its IWeightedValue , then I can calculate the weighted average for a collection of instances of this class .

+2
source share

All Articles