Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET Interfaces

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?

like image 975
PJ. Avatar asked May 05 '10 00:05

PJ.


People also ask

Does VB.NET have interfaces?

Classes contain code; interfaces do not. However, classes that implement an interface do contain code. Keep in mind that there are no instances of interfaces in VB . NET.

What is .NET interface?

An interface defines a contract. Any class or struct that implements that contract must provide an implementation of the members defined in the interface. An interface may define a default implementation for members. It may also define static members in order to provide a single implementation for common functionality.

What are interfaces examples?

An interface is a description of the actions that an object can do... for example when you flip a light switch, the light goes on, you don't care how, just that it does. In Object Oriented Programming, an Interface is a description of all functions that an object must have in order to be an "X".

What is the visual interface in Visual Basic?

However, in the context of Visual Basic it usually means what is commonly referred to as a Graphical User Interface or GUI which generally consists of one or more Forms that contain text boxes, labels, buttons, picture boxes, etc.


1 Answers

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!
like image 186
Technobabble Avatar answered Nov 15 '22 09:11

Technobabble