A look at the SOLID principles of OOD [Part 1 of 2]

by Brad February 01, 2010 00:17

No matter where you turn in the blogosphere, chances are you will eventually come across a post on the SOLID  principles of OOD. As such, I quite often refer to the principles in discussions with other developers as if it were common knowledge. Quite often, however, I am asked to elaborate on these principles.SOLID_6EC97F9C

What is SOLID?

SOLID is an acronym for a set of principles of object oriented class design. It stands for:

  • Single Responsibility Principle (SRP)
  • Open Closed Principle (OCP)
  • Liskov Substitution Principle (LSP)
  • Interface Segregation Principle (ISP)
  • Dependency Inversion Principle (DIP)

What is so good about them?

These principles combined, help to manage code dependencies by guiding the developer to write cleaner, well structured code to avoid OO anti-patterns and spaghetti coding. Adhering to these principles results in maintainable, flexible, robust and reusable code.

Breaking down the principles:

The Single Responsibility Principle states:

“A class should have one, and only one, reason to change” OR “There should never be more than one reason for a class to change”

A class should have one responsibility, and one responsibility only. One way of identifying whether or not you have violated this principle is by describing to yourself out aloud what the class does. If you hear the word “and” in there, chances are you have violated this principle. For example, if you designed a class that calculates a list of mathematical equations and emails the results to a recipient, you would have violated this principle. The class has two reasons to change (or responsibilities).

If the customer decides he no longer wants to receive the information via email, instead sending an SMS over the mobile network, then there is a reason to change. If the customer decides they want to change the way the equations are calculated, there is another reason to change. Perhaps the most extreme anti-pattern to this principle is the God object anti-pattern whereby a single class does almost everything imaginable in the system.

The SRP can be related with cohesion. A class that conforms to the SRP will be highly cohesive, as each method in the class will be closely related to its sole purpose. A class that does too many things is likely to have low cohesion.

The Open Closed Principle states:

“You should be able to extend a classes behaviour, without modifying it” OR “Software entities should be open for extension, but closed for modification”

At a glance, the principle may seem contradictory at first. How do you “extend” behaviour without modifying code? Quite easily, actually. Inheritance after all, is one of the fundamental designs of object oriented languages. As Martin puts is, abstraction is key. Inherit from an abstract class, mark methods as virtual, allow your class to be extended by it’s subclasses.

However, while it may seem the principle heavily promotes inheritance, it is always good to remember that it is best to “favour composition over inheritance”. It is also important to remember not to violate the Liskov Substitution Principle – as we will see later on.

In the next post we will look at the remaining principles of SOLID OOD.

Transitioning from request/reply to publish/subscribe. Don’t let the man get you down.

by Brad January 30, 2010 02:22

Building distributed systems is hard. Justifying your design to your colleagues who are operating on another wavelength is harder.

A couple years ago I was contracting to a small software house. I was part of team working on a government project involving WCF, K2 Blackpearl and a winforms application. The winforms application was a sort-of thick client housing a fair amount of business logic across a number of forms. Some forms were rather large and complex, others were simple and trivial. I use the description “sort-of” as a larger portion of the business logic was contained within the web services.

PuzzleThe architecture took a very basic n-tier approach. Your standard application calling into a web service, which in turn passed the request down the layers to the Business/Workflow and DAL. The idea behind the application was that the user would see a list of tasks on their main form, they could open a task and it would be exclusively locked to the user. The user could then perform actions based on the current step in the workflow, and progress the task through the stages of the system. Opening a task required about 3-4 web service calls. Due to the reliance on the K2 workflow system, these calls could take up to a couple of seconds each to process! Obviously, for perceived performance they were run asynchronously where they could be.

The biggest problem with this approach was with the design of the main form. Because it displayed point-in-time data, it was always stale. A refresh would be required to update to a more recent point-in-time, but this could take up to 30 seconds at a time to complete.

This could lead to a problem. If user a on computer a opens up task 1, user b on computer b had no way of knowing that this task was locked exclusively to the user without an explicit refresh of the data in the main form. If user b tried opening this task, a web service call would be made and the task would be checked for lock, which involved touching a number of systems and going through the K2 workflow process. No big deal, they can move on to the next task. But what if it too is exclusively locked to someone else? There begins the cycle of wasted resources because of stale data. Somewhat inefficient, no?

It may work for a small amount of simultaneous users, where the system state doesn’t change very rapidly, but the requirements were for the system to handle 200 users at any one time. So what is the solution?

Well, my colleague and I, both students of Udi Dahans Advanced Distributed Systems Design Course, suggested using a publish/subscribe architecture. We proposed an event based architecture. Rather than wasting resources by querying the services based on stale data, we keep the UI as up-to-date as possible by publishing changes to each client (subscriber). This means, when user a on computer a opens up task 1, user b  on computer b is notified, and the corresponding task is removed from user b’s  work list. This way, the tasks user b sees in his/her list are tasks that aren’t exclusively checked out to another user. It was a perfect scenario for the pattern.

Unfortunately, it took all of 5 minutes for our suggestion to be shot down. Why? Because it wasn’t the “Microsoft way”. An excuse we hear all too often.

Tags: , ,

Comments Disabled

by Brad January 25, 2010 15:10

Due to an overwhelming attack of spam bots, I have disabled comments. Unfortunately, even though comments were moderated, the bots kept on spamming.

Sorry to anyone who may have posted geniune comments that were deleted.

Tags:

Command Query Responsibility Segregation - Udi Dahan

by Brad January 25, 2010 15:06

Udi presented at Victorias .NET dev SIG

http://www.vimeo.com/8944337

Thanks to Mark Harris and Simon Segal for the recording.

Tags:

[Plug] http://www.iDevForfun.com

by Brad January 20, 2010 22:59

Jason Vella has started a blog. Check it out at http://www.iDevForfun.com

Tags:

Let’s take a break from our regular scheduled program

by Brad January 14, 2010 00:04

arduinoduemilanoveBusy busy busy. It seems the more free time I theoretically have, the busier I am. Confused? So am I…

One of the bigger consumers of my time lately has been my new obsession. It’s a geeky obsession, and it has consumed both my brother and my friend in the process. It is the open source Arduino Controller. More specifically, the “Duemilanove” (meaning “2009” in Italian). This little platform houses an embedded Atmega328 processor running at a blisteringly fast 16mhz with an enormous 2KB of ram!

Ok, so it certainly isn’t the fastest device on the block, but it has one of the best communities and 3rd party support out there! It is truly amazing what off the shelf expansions you can buy for this device. (for what it’s worth, I have bought all my prototyping gear from LittleBirdElectronics)

What can you do with it? Well, that’s the amazing part. A hell of a lot! There are home made UAVs shooting fireworks while streaming real time video to the pilots video glasses, there are laser harps, there are endless applications. What are my plans? You will just have to wait and see…

That’s all I have for now – well, I could go one but I am off to (re)learn good ol’ C and prepare for tomorrows meetings. Hopefully it is enough to wet your tastebuds :)

Tags:

Domain Events revisited

by Brad January 04, 2010 21:37

A few months ago I blogged about the Domain Event pattern. You can read more about it here.

Since this time, I have revised the pattern to incorporate .NET 4.0 features, specifically the Parallel Extensions (previously mentioned here)

You can download the revised framework here.

Included is a very basic demo application, so you get the idea. The output should look similar to this.

image

ITCompiler.Examples.DomainEvents.zip (23.95 kb)

.NET 4.0 Parallel Programming Samples

by Brad December 10, 2009 08:30

They can be found here.

Enjoy! :)

Tags:

Elevator Review: Clean Code [Robert C. Martin]

by Brad December 02, 2009 10:04

cleancodeClean code is one of those books you wish every developer on your team had a chance to read at least once. It not only emphasises the importance of clean code, but it provides reasoning and clear examples for every point made. The book starts off at a rather low level, discussing small segments of code covering naming of variables and methods, keeping methods short, commenting etc. gradually moving outwards to larger segments such as class design, before moving on to the more abstract principles such as SRP, DRY (and other principles that form the SOLID principles of OOD). 

The book flows quite well, however I started to lose interest towards the final chapters (JUnit Internals, Refactoring Serial Date) before being pulled back in by Smells and Heuristics.

When reading the book, some advice must be taken with a grain of salt, as sections such as Interface naming and implementation differ to the general standard used in .NET, and you may not quite agree with everything you read.

These points aside. The book provides excellent examples and justification for every point made. It is a must read for every developer. After all, nothing is more frustrating (and costly!) than working with a mountain of messy, unorganised code.

 

Rating: 4/5 (Great!)

Visual Studio 2010 Beta 2 running in a Windows 7 VM

by Brad November 26, 2009 21:22

This is what I see:image

Notice anything missing? Where do I set my project name? The text box exists, I just can’t see it! Well, according to Winston(thanks for the tip!) the solution is to turn off, or scale back the hardware acceleration. To do this, you need to navigate to Control Panel\Appearance and Personalization\Display\Screen Resolution.

Click on Advanced settings

image

Troubleshoot tab\Change settings and then scale it back. I found this worked for me.

image

The end result, well, there isn’t much eye candy anymore, but at least now I can see what I am typing.

image

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen | Modified by Mooglegiant

About

This blog is about the how, rather than the why of software development. The information provided here is provided on an as-is basis and is provided with no warranty or liability for damages resulting from its use.

RecentComments

Comment RSS

Blogroll

Download OPML file OPML

Visitor Locations