Public Interface ICommand Property Name as String Property Description As String Property ResultType as Type Function RunCommand(target as Device) As Object Function TryRunCommand(target as Device, Byref result as Object) AS Boolean Function BeginRunCommand(target as Device) as Task(Of Object) Function BeginTryRunCommand(target as Device) As Task(of Boolean) End Interface
Namespace Generic Public Interface ICommand(Of TResult) Function RunCommand(target as Device) as T Function BeginRunCommand(target as Device) As Task(Of T) End Interface End Namespace
Public MustInherit Class BaseCommand Implements ICommand
Public Function RunCommand(target as Device) As Object Implements ICommand.RunCommand Return InternalRunCommand(device) End Function
Public Function BeginRunCommand(target as Device) As Task(of Object) Implements ICommand.BeginRunCommand Return Task(Of Object).Factory.StartNew( Function() InternalRunCommand(target)) End Function
' Other boiler plate code goes here'
Protected MustOverride Function InternalRunCommand(target as Device) As Object
End Class
Namespace Generic Public Class BaseCommand(Of TResult) Inherits BaseCommand Implements ICommand(Of TResult)
Public Function BeginRunCommand(target as Device) As Task(of TResult) Implements ICommand(Of TResult).BeginRunCommand Return Task(Of TResult).Factory.StartNew( Function() OnRunCommand(target)) End Function
Protected NotOverridable Overrides Function InternalRunCommand(target as Device) As Object ' Re-route to the generic version' Return OnRunCommand(device) End Function
Protected MustOverride Function OnRunCommand(target as Device) As T End Class
câu trả lời hay nhất
我想我已经找到了一个很好的模式,它允许您使用通用函数覆盖此类函数的非通用版本,并且没有像我在 OP 中那样的任何困惑的包装函数。
Tôi là một lập trình viên xuất sắc, rất giỏi!