SOLID Thoughts

Posted by GeekAgilistMercenary on Geeks with Blogs See other posts from Geeks with Blogs or by GeekAgilistMercenary
Published on Tue, 06 Apr 2010 16:23:35 GMT Indexed on 2010/04/07 1:33 UTC
Read the original article Hit count: 478

Filed under:

SOLID came up again in discussion.  What is SOLID?  Well, glad you asked, because I am going to elaborate on the SOLID Principles a bit.

Initial Concept
S Single Responsibility Principle
O Open/Closed Principle
L Liskov Substitution Principle
I Interface Segregation Principle
D Dependency Inversion/Injection Principle

The Single Responsibility Principle (SRP) is stated that every object should have a single responsibility and should be entirely encapsulated by the class.  This helps keep cohesion.  Here is a short example, starting with a basic class.

public class Car
{
    decimal Gas;
    int Doors;
    int Speed;
    decimal RampJumpSpeed;
}

Now I will refactor a little bit to make it a bit more SRP friendly.
public class Car
{
    decimal Gas;
    int Speed;
}
 
public class DuneBuggy : Car
{
    decimal RampJumpSpeed;
}
 
public class EconomyCar : Car
{
    int Doors;
}

What we end up with, instead of one class, is an abstract class and two classes that have their respective methods or properties to keep the responsibilities were they need to be.

The Open Closed Principle (OCP) is one of my favorites, which states simply, that you should be able to extend a classes behavior without modifying it.  There are a couple of ways one can extend a class, by inheritance, composition, or by proxy implementation. 

The Liskov Substitution Principle (LSP) states that a derived class must be substitutable for their base classes.

The Dependency Inversion Principle (DIP) states that one should depend on abstractions and not on concrete implementations.

Finally, the Interface Segregation Principle (ISP) states that fine grain interfaces should be client specific.

So hope that helps with kicking off a basic understanding of SOLID Principles.  I will be following this entry up with some new bits in the near future related to good software design and practice.

Original post.

© Geeks with Blogs or respective owner