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.
source share