Skip to content

SOLID principles

July 24, 2013

Object oriented has some principles that when you follow them the non-functional attributes of your codes will be enhanced. SOLID is an acronym for 5 principles what introduced by Robert C Martin and acronym was create by Michael Feathers.

  • Single Responsibility Principle : Every class should have just One responsibility and encapsulate its responsibility entirely. The Most important reason of this principle is that there must be just one reason for changing a class. For example when you want to read a config file for database connection information and connect to database you must have two separate class, First for config file reading and the other for connection to database.
  • Open Close Principle: Every class should be open for extending and closed modification. When you write a class and you are sure then new features and enhancements must be added with extending class not with modification source code. So your code must be extendable. In the other word just modify your code for errors, new features and enhancements need new class that extends current class.
  • Liskov Substitution Principle: If S is a subtype of  T, then objects of type T may be replaced with objects of type S without altering any part of your code.
  • Interface Segregation Principle: No client should be force to depend on methods it does not use. ISP means your interfaces must be small and specific for a goal.
  • Dependency Inversion Principle: High level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend upon details. Details should depend upon abstractions. When you are designing an application it is a bad idea that you create dependency between high level and low modules, instead of this model of design, try to design dependencies with specifications then your high level modules just depends on your specification and you can substitute your modules implementations very easy. There are many solutions for Dependency Inversion, Plugin, Service Locator and Dependency Injection are some these patterns.

 

I want to start to explain some of important design patterns in my next posts and it is one the reasons that i explained SOLID principles. There are some other principles you may like to know as (YAGNI , GRASP , KISS , … ) 

About these ads

From → Object Oriented

5 Comments
  1. Reblogged this on Leivan's Blog and commented:
    Add your thoughts here… (optional)

  2. A simple and rich way to show the fundamentals of OO! Great article!

Trackbacks & Pingbacks

  1. Open Close Principle | tech geek
  2. Default methods an approach to extend legecy code | Just another java blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: