Joseph Bulger IV

Programming

The SRP Swiss Army Knife

by Joseph on Mar.07, 2009, under Programming

I read this blog a few weeks back, and while the principles that the author was trying to convey I don’t disagree with, I did have a hard time stomaching the idea that Swiss army knives made for bad OOP design.  Why?  Because I really like Swiss army knives!  I mean come on, what other device lets you have 100+ different kinds of knives, a compass, a light, a magnifying glass, and a USB drive all in one “disappears in your pocket” size container???

Ok so I know what most of you are thinking… I’m just PROVING that they violate SRP right?  Not actually.  In case you’re not familiar with the principle of SRP, check out this wiki.  The reason why I believe the Swiss army knife does NOT violate SRP is because my general rule of them is, if I can state the responsibility of the object in question in one single statement, then I have identified it’s SINGLE responsibility.  So I have to ask myself, what is the SINGLE thing that a Swiss army knife does?

Responsibility statement: A Swiss army knife is responsible for allowing it’s user to be able to use any tool that has been installed into it.

Short, simple, and to the point.  Now, that sounds all fine and dandy, but if I don’t implement the code the way I’ve dictated in my statement, then I will violate SRP, so how is it that I build a Swiss army knife while preserving my responsibility statement?  Enter code.

   1: public interface SwissArmyKnife
   2: {
   3:     ITool PullOutTool(ITool tool);
   4:     void PutAwayTool(ITool tool);
   5: 
   6:     IList Tools { get; }
   7: 
   8:     ITool ToolBeingUsed { get; set; }
   9: }

 

 

My interface defines any type of Swiss army knife I ever wish to make.  It’s only responsibility is to be able to use a tool that it has.  So then how do you use the Tool?  Well here’s how an ITool might look like:

   1: public interface ITool
   2: {
   3:     void Use();
   4: }

 

 

Now you can get any tool from the Swiss army knife and then use it.  Notice that it’s NOT the responsibility of the Swiss army knife to know HOW to USE the tool, only to get it out or put it away.

So what would violate SRP then?  Why did the author choose this particular product to harp on?  I think it was a matter of perspective really.  The article I read had numerous pictures of knives all decked out such as this one. Here is where I believe the real trickery is.  The author shows the pictures of all the swiss army knives with all the tools exposed.  If you really tried to use the knife like this, you would probably really screw up your hand!  That’s my point.  The knife is not of any use to anyone unless you’re using ONE tool at a time.  This is why SRP is not being violated.

You might take this a step further and say that you have a Swiss army Factory that would actually build the Swiss army knives, but that would be a discussion for another day.

Anyway, I hope everyone has found this interesting, till next time!

Leave a Comment :, , more...

Light up the world in Silverlight!

by Joseph on Mar.07, 2009, under Programming

I just finished watching a variety of videos from Mike Taulty about learning Silverlight.  These are exceptionally good videos and I would suggest anyone that is trying to learn Silverlight OR Windows Presentation Foundation (WPF) to take a look at them. 

Here are links to all the videos.

  1. Silverlight – Hello World
  2. Silverlight – Anatomy of an Application
  3. Silverlight – The VS Environment
  4. Silverlight – Content Controls
  5. Silverlight – Built-In Controls
  6. Silverlight – Width, Height, Margins, Padding, Alignment
  7. Silverlight – Using a GridSplitter
  8. Silverlight – Grid Layout
  9. Silverlight – StackPanel Layout
  10. Silverlight – Canvas Layout
  11. Silverlight – Databinding UI to .NET Classes
  12. Silverlight – Simple Styles
  13. Silverlight – Custom Types in XAML
  14. Silverlight – Binding with Conversion
  15. Silverlight – List Based Data Binding
  16. Silverlight – Simple User Control
  17. Silverlight – Templating a Button
  18. Silverlight – Resources from XAP/DLL/Site Of Origin
  19. Silverlight – Animations & Storyboards
  20. Silverlight – Uploads with WebClient
  21. Silverlight – Downloads with WebClient
  22. Silverlight – Calling HTTPS Web Services
  23. Silverlight – Calling Web Services
  24. Silverlight – Making Cross Domain Requests
  25. Silverlight – Using HttpWebRequest
  26. Silverlight – File Dialogs and User Files
  27. Silverlight – Using Sockets
  28. Silverlight – Using Isolated Storage
  29. Silverlight – .NET Code Modifying HTML
  30. Silverlight – Using Isolated Storage Quotas
  31. Silverlight – Calling JavaScript from .NET
  32. Silverlight – Evaluating JavaScript from .NET Code
  33. Silverlight – Handling HTML Events in .NET Code
  34. Silverlight – Handling .NET Events in JavaScript
  35. Silverlight – Calling .NET from JavaScript
  36. Silverlight – Displaying a Custom Splash Screen
  37. Silverlight – Passing Parameters from your Web Page
  38. Silverlight – Loading Media at Runtime
  39. Silverlight – Dynamically Loading Assemblies/Code
  40. Silverlight – Reading/Writing XML
  41. Silverlight – Multiple Threads with BackgroundWorker
  42. Silverlight – Insert/Update/Delete with the DataGrid
  43. Silverlight – Getting Started with the DataGrid
  44. Silverlight – Embedding Custom Fonts
Leave a Comment :, more...

  • Link with me

    Joseph Bulger
  • Call me

  • What I'm Doing...

    • Gmail Priority Inbox.... FINALLY, applying the spam heuristics engine (with a splash of user voting) to my inbox. email isn't so bad anymore 2 days ago
    • my wife just informed me that today is 90210.... 3 days ago
    • Today has been answered prayer after answered prayer. Thank you Jesus. 2 weeks ago
    • More updates...

    Powered by Twitter Tools

  • Looking for something?

    Use the form below to search the site:

    Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

    Visit our friends!

    A few highly recommended friends...

    Archives

    All entries, chronologically...