摘要:(200-06-25) VB.NET 介面(Interface)
介面(Interface)
Constract(合約)
定義Method規劃
*可擴充 Aggregation(聚合)
介面 1
'介面合約(功能-程序(method or porperty)合約
Public Interface IWork
'定應工作合約規範預設為抽象
Sub working(ByVal thing As String)
End Interface
介面 2
Public Interface ISales
'業務推廣
Sub Promotion()
End Interface
實作介面類別1 : 實作 介面1
'實作介面
Public Class Employee
Implements IWork
'工作能力
Public Sub working(ByVal thing As String) Implements IWork.working
System.Console.WriteLine(String.Format("做...{0} 何時:{1:D}", thing, DateTime.Now))
End Sub
End Class
實作介面類別2 : 實作 介面1和介面2
Public Class KHHEmployee
'實做同樣的介面
Implements IWork, ISales
'工作
Public Sub working(ByVal thing As String) Implements IWork.working
System.Console.WriteLine(String.Format("KHH員工 做...{0} 何時:{1:D}", thing, DateTime.Now))
End Sub
'業務推廣
Public Sub Promotion() Implements ISales.Promotion
System.Console.WriteLine("KHH業務推廣!!")
End Sub
End Class
Agent :代理
Public Class Company
'Aggregation依存關係(Pull拉式)
'公司請過來人力仲介
Private _service As WorkingService = New WorkingService()
'屬性 取得Servicer
'Public ReadOnly Property Service() As WorkingService
' Get
' Return _service
' End Get
'End Property
'窗口Method(Proxy) 代理值行workingService.addEmployee()
Public Sub addEmployee(ByVal emp As IWork)
If (Not emp Is Nothing) Then
_service.addEmployee(emp)
End If
End Sub
'要求所有員工工作
Public Sub doWork()
'掃描每一個不同類型員工(沒有繼承關係)
For Each w As IWork In _service.Employees
'判斷特殊類型物件
If TypeOf w Is KHHEmployee Then
w.working("資訊工作...")
'實做業務介面
DirectCast(w, ISales).Promotion()
Else
w.working("工作...")
End If
Next
End Sub
End Class
主要類別
Public Class Company
'Aggregation依存關係(Pull拉式)
'公司請過來人力仲介
Private _service As WorkingService = New WorkingService()
'屬性 取得Servicer
'Public ReadOnly Property Service() As WorkingService
' Get
' Return _service
' End Get
'End Property
'窗口Method(Proxy) 代理值行workingService.addEmployee()
Public Sub addEmployee(ByVal emp As IWork)
If (Not emp Is Nothing) Then
_service.addEmployee(emp)
End If
End Sub
'要求所有員工工作
Public Sub doWork()
'掃描每一個不同類型員工(沒有繼承關係)
For Each w As IWork In _service.Employees
'判斷特殊類型物件
If TypeOf w Is KHHEmployee Then
w.working("資訊工作...")
'實做業務介面
DirectCast(w, ISales).Promotion()
Else
w.working("工作...")
End If
Next
End Sub
End Class
主程式
Module TestCompany
'主程式
Public Sub Main()
'建構公司物件->拉出WorkingService->List集合物件
Dim com As New Company()
'徵求員工 -問出內部參考WorkingService
'Dim service As WorkingService = com.Service
'加員工
com.addEmployee(New Employee())
com.addEmployee(New KHHEmployee())
com.addEmployee(New Employee())
'公司所有員工工作
com.doWork()
End Sub
End Module