[I made a small update that D.Code suggested... He didn't like my inexact coding (comments like “I think“), so I updated this].
I came up with this analogy while trying to help Jamie (Hugh -- my supervisor-- has assigned her to work with me on some stuff... she's done some ASP work before, but very little ASP.Net; and probably she has never worked on a project that was designed with OOP principles in mind). I realized yesterday morning that she may be a little fuzzy on inheritance and I also came up with this analogy (it's not a great analogy, but if your struggling with what inheritance is or want a way to explain it to others, this may help).
You have started a business building condos. Before building your first condo, you realize that you will be building a complex with living units that will all be very similar. You will have 2 bedroom units and 1 bedroom units. You realize that maybe if you outsource the unit construction you could maybe save yourself some time when it came time actually place the individual condo units into the building (if the condos were already built). You contract with Company by the name of CoreLibrary to build your base condos -- 2 bedroom and 1 bedroom varieties; you've chosen CoreLibrary because they already build 1 and 2 bedroom condos for other companies. One of the first things you realize is that CoreLibrary builds a very basic condo unit; while their units are well constructed, they leave a number of things open ended (for instance, they don't put any kitchen cabinets or faucets in the condo unit; they also don't add any kind of locks on the unit nor do they paint the unit.
Now you realize that you are back to square one, because while you saved yourself some time, you realize that more work could be done by someone else. You need to have locks installed. The problem is that you have wanted to add some special high security stuff using key cards (as well as a hidden key pad if you forget your key). Since you're units are already pre-built, you realize that you could ship right to the manufacturer (whose name is CustomClass1) of your locks/security electronics and they can go ahead and add to your units the necessary locks, etc.
Now the Kitchen is still not a thrilling idea; you'll still have to do more work then you planned. Of course (by now you already realize), you will find someone else to handle the kitchen. The only catch is that you want this company (called CustomClass2) to use one of two different types of faucets from your chosen vendor: a handle lift, and the traditional 2 knobs. The most important thing is that these 2 different faucets provide hot and cold water.
Finally, you decide to have CustomClass3 (a contracting firm) to paint the walls of the condo units.
Now you can simply place your units hook them up to electricity, water, etc. and go about your business. There is one more thing that happens. You've let people like my associate Jamie come in and buy a condo before you have completed construction and are allowing her to customize the condo however she wants (Jamie wants Granite counter tops, BTW); this work should be fairly simple because you only need to do what she wants, and nothing else (because the rest of the unit is done)... BTW, you'll also charge Jamie 3 times the cost because you had all that work to do <grin />
I know that's a nice story but what does it have to do with inheritance. Let's apply this to an ASP.Net site. You are building a bunch of pages. You start out by building based on the base webform. You quickly realize that security is going to be a concern, so you decide to build a new page class called SecurePage. Secure Page looks like this (very crude mock up of VB to follow):
Public Class BasePage
Protected Sub PageLoad(Sender as object, e as System.EventArgs) handles Web.UI.Page.Page_Load
' Check Security if I'm not the login page
' Bunch of other security functions
Now you can inherit all your pages from this one and get instant security (Note: that wasn't a complete example). I'll come back later and give you a more complete example. Next you realize that you want to include some functions for navigation, but you have 2 different types of navigation (like you had different faucets). You can simply build a new page that inherits from BasePage called BaseNav1Page and BaseNav2Page. Depending on the Navigation that you want you can inherit from one of these (I'm not going to show you the implementation).
Finally you have a background color scheme that you always want applied, you could simply derive a new set of classes called BaseBlueNav1Page, BaseGreenNav1Page, BaseBlueNav2Page, and BaseGreenNav2Page. Now you can simply inherit a new web from from any of these and you get instant color scheme (I know this last one was a dumb example).
So now when you have to add the Granite Counter Tops or the odd GUI element that the users have to have most of the dirty work is done.
(I hope that helps someone... I have more to write on this topic, but I have to get ready for work).
| posted on Wednesday, January 12, 2005 8:52 AM