CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jay Kimble -- The Dev Theologian

Philosophizing about the .Net religion

Interfaces are like faucets...

Yesterday, I ran out of time.  I had a great idea, but blogging before work sometimes doesn't leave me enough time to complete the full thought.  Yesterday I blogged about how OOP Programming can be like being a condo builder.  I want to expand on one point today that I just didn't have time to discuss.  Here's the area I want to zoom in on and restate a little:

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.

Let's restate this a bit (the italicized stuff is what we need make some changes to get to a slightly different point, and a slightly different idea).


The only catch is that you want this company (called CustomClass2) to include hookups for a kitchen faucet.  These hookups are for hot and cold water.  Later you will put the faucets in.  You have already determined that you have two really nice models that you will place in your condo units: the traditional to knob faucet with brass trim, and the single lift faucet with silver trim.  In order to place these faucets in the sink you need to be able to hook them up to the kitchen.  The real reason you have done this is so that you can let people like my co-worker Jamie (who are going to come in and buy a condo unit before it is done) to decide what type of kitchen faucet she wants (BTW, I think the silver will look nice with the granite counter tops).


Now let's take this a little more real world  In our example yesterday I laid out a chain of classes that you would inherit from to before you get your individual web form pages.  Just to recap we had: BaseBlueNav1Page, BaseGreenNav1Page, BaseBlueNav2Page, and BaseGreenNav2Page inheriting from BaseNav1Page and BaseNav2Page which in turn inherit from SecurePage which inherits from System.Web.UI.Page.  BaseNav1Page and BaseNav2Page are what we are really going to change.

Remember that BaseNav1Page and BaseNav2Page were pages that we inherited from the SecurePage.  Each of these pages had two different types of navigation controls (just like our kitchen has 2 possible types of faucets).  But what if we were able to create a way that we would only need one class and could simply provide a way at this level to hook up the navigation controls later (like we did with the faucets).  With interface programming we can.  For sake of the example let's just say that at this level we only need to be able to tell the navigation control to go back or to go home (and we need this because we actually call those functions in routines that exist in th BaseNav1Page and BaseNav2Page classes.  Here's what that might look like:

Interface INavControl
  Public sub GoHome()
  Public sub GoBack()
End Interface

Public Class NavPage
  Inherits SecurePage

  Protected _NavControl as INavControl

  ' Bunch of other stuff follows some of which call the GoHome and GoBack methods
End Class


Now in our ultimate class we would have something like this

Public Class OurPageClass
  Inherits GreenPage ' which in turn inherits NavPage, BTW

  ' ... Bunch of stuff here

  Protected Sub CallNavPageSubThatNeedsGoHome()
    _NavControl = myNavControl ' NavControl would be a UserControl, or a CustomControl, or some other class that Implements INavPage
   CallSubThatNeedsGoHome() ' At some point we'll need to explain scope, so simply trust me that this sub exists (in our incomplete pseudo class)
  End Sub

OK, so what does this buy us.  We have eliminated the BaseNav1Page and BaseNav2Page classes and replaced them with a single class BaseNavPage.  We will inherit our color schemes via BluePage and GreenPage classes.  We have done in 3 classes what originally took 6 classes!  A smaller hierarchy is always easier to understand.



Check out Devlicio.us!

This Blog

Syndication

News

CodeBetter.Com Home
Current Threat level
Terror Alert Level