SOLID Principles

This will be two parts post, I will talk about the SOLID principles and try to cover all of them from the way I understand SOLID,

First SOLID was introduced by Robert C. Martin (Uncle Bob), in his 2000 paper Design Principles and Design Patterns. Uncle Bob also well known for his book Clean Code, I studied this book in my CS-338 class, it was great book in changing the way of coding. and how to writ a clean code.

Going back to SOLID. SOLID is a shortening for 5 important design principles Object Oriented Programming

S — Single responsibility principle

O — Open/closed principle

L — Liskov substitution principle

I — Interface segregation principle

D – Dependency inversion principle

I will talk about the first tow principles in this post.

Single responsibility principle

It means that every class and module should be responsible or have responsibility of a single part of the software functionality. It is like do one thing and do it well. Uncle Bob defines a responsibility as a ‘reason to change’ which means that the class or the module don’t need more than one reason to be change.

For example lets look to this code:

This code violates the SOLID first principle, the method CreateBlog() has to do many responsibilities, create a Blog, and log an error in the DB and also log an error in the file. which is absolutely the opposite of the Single responsibility principle

class Author
{
void CreateBlog(Database db, string publishtText)
{
try
{
db.Add(publishtText);
}
catch (Exception ex)
{
db.LogError(“There is an Error: “, ex.ToString());
File.CheckAllText(“There is a Local Errorn in.txt”, ex.ToString());
}
}


Lets try to fix the problem in the code above.

class Blog
{
private ErrorLogger errorLogger = new ErrorLogger();

void CreateBlog(Database db, string publishtText)
{
try
{
db.Add(publishtText);
}
catch (Exception ex)
{
errorLogger.log(ex.ToString())
}
}
}

class ErrorLogger
{
void log(string error)
{
db.LogError(“There is an Error: “, error);
File.CheckAllText(“\There is a Local Errorn in.txt”, error);
}
}

We created two classes each one of them will handles one responsibility, and that will no longer violate the single responsibility principle.


Open/closed principle

It means that the software entities (classes, modules, functions, ….) has to be closed for modification however has to be open for extensions.

We know from the Object-Oriented Programming or OOP, the polymorphism , and I posted a blog explaining about polymorphism. In another word we need to create inheritance or implementing an interfaces in the code to make sure it is compliant with the open/closed principle

lets look to this code and see whether it is applying the Open/closed principle

class Blog
{
void CreateBlog(Database db, string publishtText)
{
if (publishtText.StartsWith(“!”))
{
db.AddAsTag(publishtText);
}
else
{
db.Add(publishtText);
}
}
}

What we want in this code is to do a exact one thing which is the every time a published Text start with the character ‘!’, the code is violates the open/closed principle. If in the future we needed wanted to include mentions all the published text starting with ‘#’, then modifying the class with an another ‘else if’ is a must added to the method().

Lets try to fix the problem in the code above.

class Blog
{
void CreateBlog(Database db, string publishtText)
{
db.Add(publishtText);
}
}

class TagText : Text
{
override void CreateBlog(Database db, string publishtText)
{
db.AddAsTag(publishtText);
}
}

It is much easier to create extended behavior to the Blog object by using inheritance and overriding the CreateBlog() method.

That is all I have for this week, and next week I will continue on the SOLID principles and talk about the other three principles .

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

Create your website at WordPress.com
Get started
%d bloggers like this: