Before I give you basic scenarios, let me prepare the first platform. Here are 5 child classes and one parent class.
Public MustInherit Class Animal
Public Property Legs As Integer
End Class
Public Class Cow
Inherits Animal
Public Property AverageMilkQuantity As Integer
End Class
Public Class Buffalo
Inherits Animal
Public Property AverageMilkQuantity As Integer
End Class
Public Class Camel
Inherits Animal
Public Property AverageMilkQuantity As Integer
End Class
Public Class Lion
Inherits Animal
Public Property MeatEatingQuantity As Integer
End Class
Public Class Tiger
Inherits Animal
Public Property MeatEatingQuantity As Integer
End Class
In above code, you can see parent class as “Animal” and 5 child classes which are inheriting parent class. Not all animals have the same properties. Some animals sub classes have different properties. For example, Dairy milk producer animals have different properties than meat eater animals.
Now our platform is ready. I will show you two scenarios.
Scenario no. 1:
Dim animals As New List(Of Animal)
Dim oCow As New Cow
oCow.Legs = 4
oCow.AverageMilkQuantity = 15
Dim oBuffalo As New Buffalo
oBuffalo.Legs = 4
oBuffalo.AverageMilkQuantity = 20
Dim oCamel As New Camel
oCamel.Legs = 4
oCamel.AverageMilkQuantity = 25
Dim oTiger As New Tiger
oTiger.Legs = 4
oTiger.MeatEatingQuantity = 5
Dim oLion As New Lion
oLion.Legs = 4
oLion.MeatEatingQuantity = 7
animals.Add(oCow)
animals.Add(oBuffalo)
animals.Add(oCamel)
animals.Add(oTiger)
animals.Add(oLion)
'For Milk Producer '
For Each oAnimal As Animal In animals
Dim MilkQuantity As Integer = 0
If TypeOf oAnimal Is Cow Then
MilkQuantity = DirectCast(oAnimal, Cow).AverageMilkQuantity
ElseIf TypeOf oAnimal Is Buffalo Then
MilkQuantity = DirectCast(oAnimal, Buffalo).AverageMilkQuantity
ElseIf TypeOf oAnimal Is Camel Then
MilkQuantity = DirectCast(oAnimal, Camel).AverageMilkQuantity
End If
Next
In this code, you can see in the last “For each” loop, we have to repeat the same stuff again and again unnecessarily. To solve this problem, we will create an Interface. We can create an abstract class, but it is unnecessary because we are not going to write any kind of implementation code. So the best solution is to create an Interface. Here is the Interface, changed child classes and changed “For each” loop.
Public Interface IMilkProducer
Property AverageMilkQuantity As Integer
End Interface
Public Class Cow
Inherits Animal
Implements IMilkProducer
Public Property AverageMilkQuantity As Integer Implements IMilkProducer.AverageMilkQuantity
End Class
Public Class Buffalo
Inherits Animal
Implements IMilkProducer
Public Property AverageMilkQuantity As Integer Implements IMilkProducer.AverageMilkQuantity
End Class
Public Class Camel
Inherits Animal
Implements IMilkProducer
Public Property AverageMilkQuantity As Integer Implements IMilkProducer.AverageMilkQuantity
End Class
'For Milk Producer '
For Each oAnimal As Animal In animals
Dim MilkQuantity As Integer = 0
If TypeOf oAnimal Is IMilkProducer Then
MilkQuantity = DirectCast(oAnimal, IMilkProducer).AverageMilkQuantity
End If
Next
Isn’t it great? It reduced number of code lines of "For each" loop and makes the code easy to use and maintainable. Interfaces are like “plug and play” instrument. You can create interfaces with closely related properties or methods. Classes have to implement those interfaces to make code easy to use and maintainable.
Scenario no. 2:
Now imagine that you want to manipulate some methods of particular group like “MilkProducer”. When somebody request “getMilkQuantity”, you want to put some logic and then you want to return “MilkQuantity”. How will you do that? Here is the simple code, which will solve this issue.
Public Class MilkProducer
Dim objMP As IMilkProducer
Sub New(ByVal oMilkProducer As IMilkProducer)
objMP = oMilkProducer
End Sub
Public Function getMilkQuantity() As Integer
'Her You can manipulate '
If TypeOf objMP Is Cow Then
'This is just an example '
objMP.AverageMilkQuantity = 10
End If
Return objMP.AverageMilkQuantity
End Function
End Class
'For Milk Producer '
For Each oAnimal As Animal In animals
Dim MilkQuantity As Integer = 0
If TypeOf oAnimal Is IMilkProducer Then
Dim mp As New MilkProducer(oAnimal)
MilkQuantity = mp.getMilkQuantity
End If
Next
Isn’t it cool? Let me know if you have any questions. I will try my best to provide answers to your queries. I hope this will help you understand when and why we need to create Intefaces.
No comments:
Post a Comment