Tuesday 15 July 2014

vb.net - Too many identical simple methods using an interface -


I have a few dozen classes that all implement the same interface and have the same implementation as many methods. Whenever I add a new class, I have to copy and paste a lot, how can I get less code duplication?

I have heard that you should put common code in an auxiliary class but a lot of these approaches are actually nothing simpler compared to doing so calling an auxiliary method is trivial.

Heritage wants to declare all these methods again, but it will make dirt for some classes which do not carry out the same implementation.

Example:

Similar to almost every category ...

  Public Sub-ThingWasDeleted (Talks as ByVal deletedThing) Iinterface.ThingWasDeleted Implementations MyThing is deletedThing then MyThing = nothing ends then End Sub   

... but sometimes different:

  Public Sub-ThingWasDeleted (ByVal deletedThing In terms of formulations) IInterface.ThingWasDeleted 'nothing is equal in every class but similar in the form of calling in general as a common assistant method just as:  
  Public ReadOnly Property DisplayName () String Implements IInterface.DisplayName As GE If you put these methods into an assist class, get the display name named end below    

Is not it such that it is messy (if not more) than the intangible base class, where you need the base class Can the functionality override?

For example:

  Public Returns of the MustInherit Classroom BaseClass Public ReadOnly Property DisplayName () Return to Reflection DisplayNameShared End End Ending Property Public Overridable Sub ThingWasDeleted (ByVal deletedThing MyThing is deletedThing then MyThing = nothing end then End Sub ending class   

This property provides a definition of all the inherited classes that can be used and inherit Override class and ThingWasDeleted

For example:

  Public Class Mealless Institutions Baseclass Removed Public Vice President Sub-Theodged (Vault removed as Thring) 'Something Neither Sub and Class   

On the other hand, if you have written a supporting class, then you have to define every method, and the developer (who may or may be) knows which method to change In addition, the existing functionality in the base (abstract) category Instead of using the option to use, each class you create must call each of the appropriate support methods.

Personally, I prefer pre option, the main reason is that inheriting classes do not have to make any calls to get the base functionality established in the base class, and any case-by- Depending on the case, they can override their requirement. On the contrary, placing them all in an auxiliary class means that you have to write at least code to call the necessary support methods in each category.

No comments:

Post a Comment