Monday, October 31, 2011

Principles of software design

Every software application, whether you apply design patterns or not, you should adhere to the design principles. Applying design principles makes your application robust and easy to maintain. Also design principles must be learned before you can embark on a journey to learn design patterns.  Allow me to introduce you to design principles with the so called SOLID design principles.

S:   Single Responsibility Principle
Like the name suggests, what this means is that every object should be built for one reason only. You should avoid writing multi purpose classes. For example in the .net class library, the string functions are in its own single purpose System.String class and the math functions are in System.Math class. 

O:  Open-Closed Principle
A class should be open for extension and closed for modification. This means once a class has had a stable release, the existing functionality should never be modified. It it however okay to add new functionality.

L:   Liskov Substitution Principle
This principle states that you should be able to use a derived class in place on the parent class and  the derived class should behave the same as its parent class.

I:    Interface Segregation Principle
This principle is tells you to separate interfaces based on their responsibility. Do not have one huge interface for everything. For example imagine the IEnumerable interface also made you implement a lot of methods that you don't need for a foreach loop. Thats obviously an unwanted inconvenience, if all you want is to apply a foreach loop. This is avoided by having only the methods required for a foreach loop as a part of the IEnumerable interface.

D:   Dependency Inversion Principle (or the Inversion of Control Principle )
The Dependency Inversion Principle states when one class uses another class, both the code in both classes can be totally independent of each other and should only depend on interfaces (or abstract classes).
There are three ways to achieve this, namely
  • Constructor Injection
  • Method Injection
  • Property Injection

The  Dependency Inversion Principle is a concept that will take a little more reading to understand. I would strongly recommend checking out these two articles, which explains this concept in a lot more detail.

No comments:

Post a Comment

Comments will appear once they have been approved by the moderator