Can anyone associate me with some code where there is a fantastic or enjoyable use of Inheritance

I firmly believe that reading code and reading good code is the key to excellent programming. If not one of many.

I had some problems rendering and "feeling" the use of inheritance for a better code architecture.

Can someone give me a link to a good role model where people used inheritance in the absolute “merciless” Kung Fu manner [in a good way]

+4
source share
4 answers

I strongly believe that reading code and reading good code is the key to great programming.

It’s hard not to agree.

Actually, the question is rather complicated - because there are some alternatives to inheritance, such as the compound principle of reuse, so it is sometimes very difficult to figure out if inheritance is used in a “kung fui ruthless” manner or if there is a better way to implement the same thing as will make esier code to understand / verify / do it with weight loss and so on. In my humble opinion, the Enterprise Library application validator with the Microsoft.Practices.EnterpriseLibrary.Validation.Validator class with all its descendants is a very good example of inheritance, because

  • The concept of verification is easy to understand.
  • there is a good example of how to find common in objects with a rather different nature (for example, OrCompositeValidator / DateTimeRangeValidator / ObjectCollectionValidator)
  • many of us tried to implement something more or less, so this background will provide more quality for understanding
  • It is clear (for me, but I could be wrong :) that inheritance has no alternatives there

You can download the source code from codeplex.

+1
source

An example of a good use of inheritance would be .NET clusters. You can download the MONO project to access some source code. If you want to improve your code architecture, spend some time learning about architectural design patterns.

+1
source

Here is a personal example when I used inheritance to greatly help the development situation:

I needed to develop an asp.net form control set (C #) that would allow using both standard web forms (empty form fields with a submit button) and a secure version that is negotiating with the web service about providing information via SSL .

Due to all the similarities between controls and concepts, I developed a number of classes:

BaseWebControl

  • customizable control base class that inherits from the System.Web.UI.WebControl class (part of the .NET framework
  • has custom properties and methods that are used in our application by all customizable controls (information about the state of control, etc.).

BaseFormControl

  • inherits from BaseWebControl, getting all the basic functions
  • handles the functionality of the basic form, such as dynamically adding fields, marking required fields, adding a submit button, etc. etc.
  • contains label and associated control index for easy search
  • designated as an abstract class, with an abstract SubmitForm method. This method is not defined in this class, however it is called by the event that the submit button is clicked. This means that any particular form control class that inherits from this base class can implement the abstract SubmitForm functionality as needed.

EmailFormControl

  • Inherits from BaseFormControl , so it gets all the basic functionality above without duplication.
  • it contains very little except to override the abstract SubmitForm method and generates an email based on the form fields.
  • all other management functions and event processing are processed by the base class. In the base class, when the submit button is pressed and processed, it calls this particular implementation of SubmitForm

SecureFormControl

  • It inherits from BaseFormControl , so it gets all the basic functionality above, without any duplication.
  • In its implementation of SubmitForm it connects to the WCF web service and transmits the information via SSL.
  • no other functions are required, because the base class handles the rest.

In a codeless form, the general scheme is as follows:

 public class BaseWebControl : System.Web.UI.WebControl { //base web control with application wide functionality built in } public abstract class BaseFormControl : BaseWebControl { //handles all 'common' form functionality //... //... //event handler for submit button calls abstract method submit form, //which must be implemented by each inheriting class protected void btnSubmit_Click(object sender, EventArgs e) { SubmitForm(); } protected abstract SubmitForm(); } public class EmailFormControl : BaseFormControl { protected override SubmitForm() { //implement specific functionality to email form contents } } public class SecureFormControl : BaseFormControl { protected override SubmitForm() { //connect to WCF web service and submit contents } } 

As a result of the above, BaseFormControl has about 1000 lines of code in a whole set of methods, properties, etc. SecureFormControl and EmailFormControl each have about 40 lines. All other functions are shared and controlled by the base class. This contributes to:

  • maintainability
  • efficiency
  • flexibility
  • consistency

Now I can create any type of web form, such as DataBaseFormControl , etc. etc. very easy. I can add new functionality to all forms by adding methods and properties to base classes, etc.

And this list goes on.

Fu, who printed a lot. Hope this helps give you a good example. This was one example where I found inheritance key to success in a project.

+1
source

I agree with the recommendation to take a look at the .NET base class library, as it has great examples of abstraction with both inheritance and interfaces. The goal is to isolate consumer code from the need to take care of the details of a particular implementation. WinForms designer works with controls, but does not know which specific controls will be implemented. This is unimportant because inheritance abstracts out unnecessary details. LINQ works similarly to IEnumerable; it doesn't really matter what is being listed, since there are algorithms that you can write that work with something enumerated. Both are excellent examples of using abstraction.

+1
source

Source: https://habr.com/ru/post/1307704/


All Articles