This is part 3 of a three part series on the Delegation design pattern. Part 1 - The General Case Delegation Pattern - tells you what the Delegation design pattern is and explains a version designed to be as general as possible. Part 2 - Inheritance as an alternative to the Delegation design pattern - introduces an example to contrast with Delegation. This final part of the series finally shows the same "general case" template using the more real world problem introduced in part 2.
Public Interface IEmployee
Sub bookSale(ByVal n As String,
ByVal s As String, ByVal a As String, ByVal t As String
)
Sub provideSupport(ByVal n As String,
ByVal i As String, ByVal t As String, ByVal d As String
)
End Interface
Public Class doSales
Implements IEmployee
Public Sub Sale(ByVal n As String,
ByVal s As String, ByVal a As String, ByVal t As String
) Implements IEmployee.bookSale
frmActivityLog.lstActivityLog.Items.Add(
"Booking sale of: " & s &
" - Amount: " & a)
Time.logTime(n, t)
End Sub
Public Sub Support(ByVal n As String,
ByVal i As String, ByVal t As String, ByVal d As String
) Implements IEmployee.provideSupport
frmActivityLog.lstActivityLog.Items.Add(
"Surcharge Time of 10 minutes since sales is providing support")
t &= " + 10 Minutes"
frmActivityLog.lstActivityLog.Items.Add(
"Providing Support for item: " & i &
" - Time Required: " &
" Support Description: " & d)
Time.logTime(n, t)
End Sub
End Class
Public Class doSupport
Implements IEmployee
Public Sub Sale(ByVal n As String,
ByVal s As String, ByVal a As String, ByVal t As String
) Implements IEmployee.bookSale
frmActivityLog.lstActivityLog.Items.Add(
"Surcharge Time of 20 minutes since support is providing sales")
t &= " + 20 Minutes"
frmActivityLog.lstActivityLog.Items.Add(
"Booking sale of: " & s &
" - Amount: " & a)
Time.logTime(n, t)
End Sub
Public Sub Support(
ByVal n As String,
ByVal i As String, ByVal t As String, ByVal d As String
) Implements IEmployee.provideSupport
frmActivityLog.lstActivityLog.Items.Add(
"Providing Support for item: " & i &
" - Time Required: " & t &
" Support Description: " & d)
Time.logTime(n, t)
End Sub
End Class
Public Class Work
Implements IEmployee
Private e As IEmployee
Public Sub Sale(
ByVal n As String,
ByVal s As String, ByVal a As String, ByVal t As String
) Implements IEmployee.bookSale
e.bookSale(n, s, a, t)
End Sub
Public Sub Support(ByVal n As String,
ByVal i As String, ByVal t As String, ByVal d As String
) Implements IEmployee.provideSupport
e.provideSupport(n, i, t, d)
End Sub
Public Sub ToSales()
e = New doSales()
End Sub
Public Sub ToSupport()
e = New doSupport()
End Sub
End Class
Public Class Time
Shared Sub logTime(
n As String, ByVal t As String
)
frmActivityLog.lstActivityLog.Items.Add(
"Logging time for: " & n &
" - Time Logged: " & t)
End Sub
End Class
The inherited method logTime becomes a shared subroutine here and every class that needs it just calls it. The role is assigned to an employee object e in either doSales or doSupport. Now, the tasks are completely independent and a role always does a task in a certain way. (For example, if you didn't allow sales persons to act in a support role, you could log an error instead of logging the time.)
Here's the code that uses these classes in a Windows form.
Dim w As New Work()
Private Sub btnDelegateSalesRole_Click(
sender As System.Object, e As System.EventArgs
) Handles btnDelegateSalesRole.Click
w.Sale(txtEmpName.Text,
txtProduct.Text, txtAmount.Text, txtTime.Text)
End Sub
Private Sub btnDelegateSupportRole_Click(
sender As System.Object, e As System.EventArgs
) Handles btnDelegateSupportRole.Click
w.Support(txtEmpName.Text,
txtProduct.Text, txtTime.Text, txtDescription.Text)
End Sub
Private Sub btnDelegateSalesAndSupportRole_Click(
sender As System.Object, e As System.EventArgs
) Handles btnDelegateSalesAndSupportRole.Click
If rbtSales.Checked Then
w.Sale(txtEmpName.Text,
txtProduct.Text, txtAmount.Text, txtTime.Text)
lblSupportContinue.Visible = True
rbtSupport.Checked = True
Else
w.Support(txtEmpName.Text,
txtProduct.Text, txtTime.Text, txtDescription.Text)
lblSupportContinue.Visible = False
rbtSales.Checked = True
End If
End Sub
Private Sub lblSupportContinue_Click(
sender As System.Object, e As System.EventArgs
) Handles lblSupportContinue.Click
btnDelegateSalesAndSupportRole_Click(sender, e)
End Sub
Private Sub rbtSales_CheckedChanged(
sender As System.Object, e As System.EventArgs
) Handles rbtSales.CheckedChanged
If rbtSales.Checked Then w.ToSales() Else w.ToSupport()
End Sub
If both sales and support are being performed, the Sale method is called first, the RadioButton control is changed, and then the Support method is called. But notice that no role is passed directly. The correct code is called as a result of the role that is assigned. Also notice that the event code is called as a simple subroutine in this code. To do that, all you have to do is pass an acceptable sender and e parameter. This article tells you more about them: The VB.NET sender and e Event Parameters
--------
Click Here to display the illustration
--------
