One approach is to have a secure override method, and then have a public method that does not support forwarding, which calls the override. Anytime a return value for a function in a derived class needs to change, override the overridden method that calls the new overridden method, which returns a more refined type, and also obscures the earlier version of the public function to the one that uses the new override. If vb.net allowed one class to redefine and shade the same member, everything would be much cleaner, but there would be no way to do this.
Public Class CarFactory Protected Overridable Function DerivedMakeCar() as Car ' make a car End Function Public Function MakeCar() as Car Return DerivedMakeCar() End Function End Class Public Class FordFactory Inherits CarFactory Protected Overrides Function DerivedMakeCar() As Car Return DerivedMakeFord() End Function Protected Overridable Function DerivedMakeFord() As Ford ' Make a Ford End Function Public Shadows Function MakeCar() As Ford Return DerivedMakeFord() End Function End Class
A simpler alternative in some cases might be to have a publicly redefined function MakeCar () that always returns an object of type Car , but FordFactory also has a MakeFord () function that returns Ford.
The overridden MakeCar () function will be NotOverridable and just call MakeFord. In a sense, the latter approach might be cleaner, but if there is a general naming convention (for example, factories have a MakeProduct method that returns the most derived type), it can be useful to use Shadows.
supercat
source share