is it possible to implement strategy pattern with different parameters in VB.net? For example I have the following code:
Public Interface InterfaceDataManipulation
Sub updateMyData()
End Interface
How to implemente updateMyData in the class that implements above interface class with different parameter, for example in class x :
Public Class X
Implements InterfaceDataManipulation
Public Sub updateMyData(ByVal x as String)
Console.Writeline(x)
End Sub
End Class
But开发者_运维知识库 the IDE raises an error "class x must implement updateMyData on interface InterfaceDataManipulation"
By adding a parameter you're not implementing the interface - The idea of the interface is that people can use your class by only knowing about the interface -so your sub with parameter wouldn't match their expectations.
There are probably many ways of skinning this cat, but these are a few of the options:
- Don't use the interface
- Implement your version of updateMyData as an overload, but you should still implement the original without the parameter aswell
- Pass in x as a property to your class, that the updateMyData method can then use, while still having a signature that matches the interface.
Public Class X
Implements InterfaceDataManipulation
Public Sub updateMyData(ByVal x as String) Implements InterfaceDataManipulation.updateMyData
Console.Writeline(x)
End Sub
End Class
The method signature needs to be appended with the method it is implementing in the interface as show above.
精彩评论