请先看下边得代码:
'Interface Command
Public Sub Execute(): End Sub
'TestCommand
Implements VB6DPFrameWork.Command
Public Count As Integer
Private Sub Command_Execute()
Count = 1
End Sub
'CommandWrapper
Private mSelf As TestCommand
Private mInterface As VB6DPFrameWork.Command
Public Sub Dispose()
Set mSelf = Nothing
Set mInterface = Nothing
End Sub
Public Sub Instance()
If mInterface Is Nothing Then
Set mInterface = New TestCommand
Set mSelf = mInterface
End If
End Sub
Public Function Interface() As VB6DPFrameWork.Command
Instance
Set Interface = mInterface
End Function
Public Function Self() As TestCommand
Instance
Set Self = mSelf
End Function
'TestCase
Public Sub Test_Wrapper(oTestResult As TestResult)
With oTestResult
Dim w As New CommandWrapper
w.Interface.Execute
.AssertEqualsLong w.Self.Count, 1
Set w = Nothing
End With
End Sub
Public Sub Execute(): End Sub
'TestCommand
Implements VB6DPFrameWork.Command
Public Count As Integer
Private Sub Command_Execute()
Count = 1
End Sub
'CommandWrapper
Private mSelf As TestCommand
Private mInterface As VB6DPFrameWork.Command
Public Sub Dispose()
Set mSelf = Nothing
Set mInterface = Nothing
End Sub
Public Sub Instance()
If mInterface Is Nothing Then
Set mInterface = New TestCommand
Set mSelf = mInterface
End If
End Sub
Public Function Interface() As VB6DPFrameWork.Command
Instance
Set Interface = mInterface
End Function
Public Function Self() As TestCommand
Instance
Set Self = mSelf
End Function
'TestCase
Public Sub Test_Wrapper(oTestResult As TestResult)
With oTestResult
Dim w As New CommandWrapper
w.Interface.Execute
.AssertEqualsLong w.Self.Count, 1
Set w = Nothing
End With
End Sub
TestCommand继承了Command接口,如果采用
dim t as command的方式,将不能访问Count属性。
如果采用
dim t as testcommand方式,将不能访问Execute方法。
这样用起来就很难受,当然,如果在TestCommand中实现一个Public的Execute当然可以,但是就不好了,也可以声明多个对象,然后进行赋值,这样也不好,为了解决这个问题,使用CommandWrapper也许就是一个好的选择了,至少使用起来要好一点。