Glenn Stovall

month

April 2011

35 posts

Dependency Inversion Principle (SOLID Part 5)

Part of my SOLID programming principles series.

What is the Dependency Inversion Principle?

The Dependency Inversion Principle can be summed up in one simple quote “design for the interface, not the implementation”. The idea is that software is built by making simple objects and functions, which are then used by more complex objects and functions. This principle is a way to make it so that higher-level components are not dependent on any particular lower level component, thereby making it easier for parts of a program to be extended or switched out.

Benefits

The main benefit of this is another level of modularity in your programs. once an object is no longer dependent on another, either of the two objects can be replaced.

Applying the Dependency Inversion Principle

This is an example from 2 projects I recently had for my data structures class at the University of Georgia. The first project involved parsing a file of text, and providing a word histogram, which would display a list of words from the file, in alphabetical order, along with the number of times that word showed up. These words would need to be stored in a binary search tree. Here is a simplified, psuedocode example of the project:

class BinarySearchTree {
    public getHistogram() {
        //returns histogram
    }
}

class HistogramGUI { private BinarySearchTree wordTree;

  public getHistogram() {
       return this.wordTree.getHistogram();
  }

}

my next project was to again generate a Histogram from a text file, but this time using a 2-3-4 tree. Now I could have re-used code from the previous project, but now I have to re-write the HistogramGUI class to use a different kind of tree. Who knows what my next project is going to be? maybe I will have the same problem yet again, but using some other kind of data structure. The HistogramGUI currently has a depenency on the other class of BinarySearchTree. If we add an interface for being able to generate histograms, I can remove this depenecy, and make the HistogramGUI class more re-usuable.

Here’s the new structure to support both projects:

interface IHistogram {
    public getHistogram();
}

class HistorgramGUI {
  private IHistogram histogramGenerator;

   public setHistogramGeneration(IHistogram generator) {
      this.histogramGenerator = generator;
   }

    public getHistogram() {
     this.histogramGenerator.getHistogram();
   }
} 

class binarySearchTree implements IHistogram {
    public getHistogram() {
        //returns histogram
    }
}

class 234Tree implements IHistogram {
    public getHistogram() {
        //returns histogram
    }
}

now you see that I can use the histogramGUI class with any class that implements IHistogram. If my next project called for the same problem with a B-Tree or a Red-Black tree or a simple array, it no longer matters to the histogramGUI class, as they are all now interchangeable. This gives you all the benefits of composing more complex objects from simpler ones, but allows you more flexibility. the histogramGUI class does not (and should not) care about how the data is generated.

Apr 28, 20113 notes
#programming #software development #oop #solid
Amazon's $23 Million Dollar Book → michaeleisen.org

If you are going to tackle a problem like this algorithmically, make sure you have sanity checks.

Apr 27, 20112 notes
#programming #algorithms #amazon
“Use all of the preceding information to sketch a graph of f(x) When you’re finished, enter a “1” in the box below.” —my overly trusting math homework
Apr 27, 20110 notes
#math #homework
In Reponse to Moving away from writing on Tumblr

in reponse to adamjmorgan and geeksathome :

I definitely plan on still using Tumblr in some regard, probably having blog posts I write show up here, either as a link or as full text. There’s also a lot of content that I think wouldn’t make sense for a full-on blog post, but would make sense as a tumblr post.

Overall though, the more I think about it Tumblr just isn’t cut out for long-form writing, and I’m thinking I may to focus on doing that elsewhere.

Apr 27, 20110 notes
New Temporary Homepage → glennstovall.com

I’ve been busy, but I’ve been meaning to update my homepage for a long time. Finally had time to fix some server issues and at least get a temporary one up. Hopefully after finals I can have a more full-fledged site up.

Which brings me to a question I’ve been thinking about: should I move my blogging to a self-hosted solution, or should I stick with Tumblr?

Apr 27, 20112 notes
Reblog if you are a programmer

joethought:

I’d like to follow as many of you as I reasonably can.

You might also want to check out the #programming and #software development tags, good way to find a lot of other interesting tumblrs

Apr 26, 201141 notes
Mathmatical Quine → en.wikipedia.org

A quine is a program that is writes its own source code. Who would have thought that there was a math function that could do the same thing?

Apr 26, 20115 notes
#math #quine
What do you wish school would have taught you about programming?

You can learn a lot from computer science programs at schools, but I feel like a lot of programs leave students ill-prepared for the real world. What are some topics or skills you wish were covered at your school that were not?

Apr 20, 20114 notes
#computer science #programming
Play
Apr 18, 201110 notes
#programming #software development #devlopers #DEVELOPERS
“Non-programmers don’t care about the aesthetics of software in the same way non-plumbers don’t care about the aesthetics of plumbing – they just want their information in the right place or their hot water to work. (Although it’s fair to say they appreciate decent boiler controls.)” —Dan North
Apr 15, 20111 note
“Shouldn’t I get more equity because it was my idea? No. Ideas are pretty much worthless. It is not worth the arguments it would cause to pay someone in equity for an idea. If one of you had the idea but you both quit your jobs and started working at the same time, you should both get the same amount of equity. Working on the company is what causes value, not thinking up some crazy invention in the shower.” —Joel Spolsky
Apr 14, 20110 notes
Simple Algorithims → algorithms.openmymind.net

Love the idea, and I love the site design.

Apr 14, 20110 notes
#programming #algorithims #computer science
J2 Labs: A bunch of floating point errors → j2labs.tumblr.com

j2labs:

Python
$ python
>>> 0.1 + 0.2
0.30000000000000004
Javascript
$ node
> 0.1 + 0.2
0.30000000000000004
Ocaml
$ ocaml
# 0.1 +. 0.2;;
- : float = 0.300000000000000044
Java

Code

public class FPError { public static void main(String args[]) { System.out.println(0.1+0.2); }
}

Running…

If you understand how floating point math works, you would understand why these are not “errors”

Apr 14, 20114 notes
Play
Apr 13, 20112 notes
#funny #children #dinosaurs
Apr 12, 201133 notes
Maker's Schedule, Manager's Schedule → paulgraham.com

remosiegwart:

Great article by Paul Graham

I’ve heard this put another way that helps a lot of people understand: Creative work is a lot like sleep. It takes time to get into REM, the really deep, most restful form of sleep. If you wake someone up in the middle of REM asleep, when they go back to sleep they don’t go back to REM, they have to start the whole process over again.

Apr 12, 20112 notes
Tim McNamara: Quick Tips for Developing for the Mobile Web → timmcnamara.co.nz

timclicks:

Here, we’re looking at building websites that can cater for both ‘normal’ users as well as mobile devices.

Prepare for interruptions

Your application is not the only thing that your user will be doing. The most basic element of crafting a GUI application, that’s different from the web is making…

Nice tips. Another great bonus to using -webkit is that it is used on android browsers, as well as Google chrome too.

Apr 12, 20112 notes
Does this man have a claim to 50% of Facebook? → businessinsider.com
Apr 12, 20112 notes
#facebook #lawsuit
Apr 12, 20110 notes
#pics #funny #lol #dogs #scrabble
Grammy Awards to add Video Game Categories → 1up.com
Apr 12, 20110 notes
#gaming #music #grammys
Next page →
2012 2013
  • January 8
  • February 3
  • March 10
  • April 5
  • May 2
  • June 1
  • July
  • August
  • September
  • October
  • November
  • December
2011 2012 2013
  • January 22
  • February 10
  • March 19
  • April 5
  • May 11
  • June 10
  • July 9
  • August 16
  • September 13
  • October 13
  • November 6
  • December 7
2010 2011 2012
  • January 27
  • February 48
  • March 32
  • April 35
  • May 47
  • June 33
  • July 2
  • August 17
  • September 8
  • October 9
  • November 7
  • December 4
2010 2011
  • January
  • February
  • March
  • April
  • May
  • June
  • July
  • August
  • September
  • October
  • November
  • December 7