
|
|
|
10 March 2008
|
Polymorphism and Interface
Polymorphism is sometimes difficult for junior programmers. In polymorphism, a
method can be defined differently according to different data types. You can use
abstract class or interface to define polymorphic methods. Here is a
simple Club House example using interface to create polymorphism.
A Club House applies different discount rates to different users for using its
facilities. As a result, all users need a discount rate for calculating the
actual fee (but the method of defining this rate is different).
The discount rate table may look like this:
Club House Facility Discount Rate (%)
|
Week Day |
Saturday |
Sunday |
| Life Member |
35% off |
25% off |
25% off |
| Ordinary Member |
30% off |
25% off |
20% off |
| Guest |
no discount |
no discount |
Member only |
If a program have 3 classes: LifeMember, OrdinaryMember and Guest all
implementing an interface with a DiscountRate Function getting the discount rate
of that class, this function is called polymorphic function. It is because the function
return different result for different types of users. The Interface and the 3 Classes coded in VB.Net
may look like this:
Public Interface IClubHouseUser
Function DiscountRate(ByVal d As Date) As Single
'Other functions not shown for simplicity
End Interface
Public Class LifeMember
Implements IClubHouseUser
Public Function DiscountRate(ByVal d As Date) As Single _
Implements IClubHouseUser.DiscountRate
Select Case d.DayOfWeek
Case DayOfWeek.Saturday, DayOfWeek.Sunday
Return 0.25
Case Else
Return 0.35
End Select
End Function
End Class
Public Class OrdinaryMember
Implements IClubHouseUser
Public Function DiscountRate(ByVal d As Date) As Single _
Implements IClubHouseUser.DiscountRate
Select Case d.DayOfWeek
Case DayOfWeek.Sunday
Return 0.2
Case DayOfWeek.Saturday
Return 0.25
Case Else
Return 0.3
End Select
End Function
End Class
Public Class Guest
Implements IClubHouseUser
Public Function DiscountRate(ByVal d As Date) As Single _
Implements IClubHouseUser.DiscountRate
Select Case d.DayOfWeek
Case DayOfWeek.Sunday
Throw New ApplicationException("Not Open to Guest")
Case Else
Return 0
End Select
End Function
End Class
The code using this polymorphic function may look like this:
Private Sub btnFee_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnFee.Click
Try
Dim user As IClubHouseUser
Select Case ComboBox1.Text
Case "Life Member"
user = New LifeMember
Case "Ordinary Member"
user = New OrdinaryMember
Case Else
user = New Guest
End Select
Dim fee As Single
Dim totalTime As Single = TimeValueInHour(txtEndHour.Text) _
- TimeValueInHour(txtStartHour.Text)
If totalTime < 0 Then
Throw New ApplicationException _
("Start Hour cannot be smaller than End Hour")
End If
fee = CSng(txtRate.Text) * totalTime * _
(1 - user.DiscountRate(DateTimePicker1.Value))
MessageBox.Show("Total Fee: " & fee.ToString("#,###.#0"))
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
You can see that the user.DiscountRate(bookDate) is the same for all types of users. If club house has new type of member in the future, e.g. Associate Member, Child Member, etc.
the function is unchanged because new type of user need to implement the IClubHouseUser interface.
You can also download the code from here.
|
|
|