views:

178

answers:

4

I strongly believe that, reading code and reading good code is key to great programming. If not one of the many.

I had been facing some problems in visualizing and having a "feel" of using inheritance to better my code architecture.

Can somebody give me some link to good code to emulate, where folks have used inheritance in an absolute "kung-fooey ruthless" manner [in a good way]

+1  A: 

An example of good usage of inheritance would be the .NET framework classes. You can download the MONO project to gain access to some source code. If you want to better your code architecture, invest some time in studying architectural design patterns.

Seventh Element
Thanks SE, if the MS has not made the code public of framework classes, I will have a look at the Mono Project. Thanks for the reco
Soham
+1  A: 

Here is a personal example where I've made use of inheritance to greatly benefit a development situation:

I needed to develop a asp.net (c#) form control set, which would allow both standard web forms (bunch of form fields with submit button), as well as a secure version which talks to a web service to submit the information over SSL.

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

BaseWebControl

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

BaseFormControl

  • inherits from BaseWebControl, gaining all of its underlying functionality
  • handles base form functionality, such as dynamically adding fieldsets, marking required fields, adding submit button, etc. etc.
  • contains a label and associated control index for easy lookups
  • marked as an abstract class, with abstract method called SubmitForm. This method is not defined on this class, however it is called by the submit button click event. This means that any specific form control class that inherits from this base class can implement the abstract SubmitForm functionality as needed.

EmailFormControl

  • Inherits from BaseFormControl, so it gains all underlying functionality above without any duplication
  • contains very little, except overrides the abstract method SubmitForm, and generates an email based on the form fields.
  • all other control functionality and event handling is dealt with by the base class. In the base class when the submit button is clicked and handled, it calls this specific implementation of SubmitForm

SecureFormControl

  • Again inherits from BaseFormControl, so it gains all underlying functionality above without any duplication
  • In its implementation of SubmitForm, it connects to a WCF web service and passes the information in over SSL.
  • no other functionality is required because base class handles the rest.

In stripped down code form, the general outline is as such:

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 bunch of methods, properties, etc. SecureFormControl and EmailFormControl each have about 40 lines. All other functionality is shared and controlled by the base class. This promotes:

  • maintainability
  • efficiency
  • flexibility
  • consistency

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

And the list goes on.

Phew that was a lot of typing. Hope this helps give you a good example. This was one instance where I found inheritance to be a key success point in a project.

KP
KP, I am from India, and there is a colloquial praise for brilliance out here. Its : Jiyo! Jiyo! [roughly translated means, live long, live long] :D I must say, thanks a bunch for such a great code piece, and yeah, Jiyo! Jiyo! :D Unfortunately I can upvote it only once!
Soham
KP, may I ask a few other questions on this? When you initialize the form, how do you instantiate will it be an EmailFormControl or SecureFormControl? I have an impression, that its determined by the user whether SSL submission has to be used or otherwise. Am I right?
Soham
Hi Soham, yes you are on the right track. Basically when we need to create a new form based on the framework, if security of information is a concern, we create a new form control that inherits from SecureFormControl. Its purpose then is to simply add controls specific to that form's needs. The base functionality takes care of the rest. If we only need a simple email form, then the new specific form inherits from EmailFormControl. Again we simply define fields in this form, and the rest is taken care of. Hope this helps, and thanks for the kind words!
KP
+1  A: 

I agree with the recommendation to look at the .NET base class library, as it has excellent examples of abstraction via both inheritance and interfaces. The goal is to insulate consumer code from having to care about the details of a particular implementation. The WinForms designer works with Controls, but it has no idea what specific kinds of Controls will be implemented. It doesn't care, because inheritance abstracts away the unnecessary details. LINQ works similarly with IEnumerable; it doesn't really matter what's being enumerated, as there are algorithms you can write that work with anything enumerable. Both are excellent examples of abstraction used well.

Dan Bryant
+1  A: 

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

Hard to disagree.

Actually the qestion is pretty hard - becouse there is some alternatives to inheritance, such as composite reuse principle, so sometimes it's very hard to diside if inheritance is used in "kung-fooey ruthless" manner or there ware some better way to implement the same wich will make code esier to understand/test/make it lossely coupled and so on. In my humble opinion Enterprise Library Application validation block whith it's Microsoft.Practices.EnterpriseLibrary.Validation.Validator class with all it's descendants is a very good example of inheritance, becouse

  • concept of validation is easy to understand
  • there is good example how to find common in objects of with pretty different nature (i.e. OrCompositeValidator/DateTimeRangeValidator/ObjectCollectionValidator)
  • many of us tried to implement something more or less like this, so this background will give more quality for understanding
  • this is clear(for me, but I can be wrong:) that inheritance has no alternatives there

You can download source code from codeplex.

er-v
Thanks er-v. Thanks a lot. I will give it a serious look into it.
Soham
But has MS made the library public?
Soham
er-v
Thanks, will take a look
Soham