Open Close Prinicple

Second SOLID principle, Open for extension and close for changes!

Wow how it is possible something can be open and close! I’m surprised! But it is possible you should design


public void Draw(Shape shape)
{
var point = CalculateStartPoint(shape);
if(shape is Rectangle)
{
DrawRectangle(point, shape);
}
else if (shape is Circle)
{
DrawCircle(point, shape);
}
}

Now think about you have new Shapre called Triangle so to make the Draw function working you need to change Draw function! So to extend your application you need to CHANGE EXISTING code which is violating OCP. So how it should be then?


public void Draw(Shape shape)
{
var point = CalculateStartPoint(shape);
shape.Draw(point);
}

Interesting, so I should add ‘Draw’ method to ‘Shape’ abstract and all implementation of ‘Shape’ should implement ‘Draw’. I like it now! So whenever you have ‘if’ or ‘switch’ based on ‘type’ of object there is a smell of OCP violation.

Ok but it is really hard to achieve this, many legacy code or in many situation it is really hard to make this working! That is correct so remember refactor it as much as possible to make sure there is no more room exists. This is engineering, finding balance and best approach!

Single Responsibility Principle

I had a problem to understand this for a long long time! You can’t believe how hard is it when you don’t understand the principle and try to adapt it! Mission impossible! Recently I spent some time again to read more about OOP principles and first step of SOLID is ‘S’.. Single Responsibility introduced by Uncle Bob. So let’s break it down:

Responsibility: Reason to change!!!

Single: One

Rephrase: One Reason to change!

Easy so what is a reason then? ACTOR!

OMG you just add more vocabulary!! What is Actor?

OK so we need to understand little a bit more of UML, Action means: An Actor models a type of role played by an entity that interacts with the subject (e.g., by exchanging signals and data), but which is external to the subject.

I don’t understand give me an example!!

Think below class as an example:

rich-domain-model

Employee class above has couple of functions. Is this class violating SRP? Yes! Why?

1. Load and Save functions, who will ask you to change them? DBAs

2. CalculatePay, CalculateHoliday functions, who will ask you to change them? Accountants

3. HoursWorked, HourlyRate functions, who will ask you to change them? Auditors

So three different “Actor”s exists in our example!

WOW! fascinating!

Now we understand that “Actor” and “Responsibility” is related to context. In some other context may be a class has one responsibility and in other context it has more than one. So for making your architecture and make sure you are not violating SRP you need to understand USE CASE and ACTOR and in REFACTOR process we can make decision about are we violating SRP!

Enjoy REFACTORING! And remember detecting violation SRP you need to understand user’s need and have good logic.

What is OOP

I recently started to think about what is really OOP. I thought it is a tool to model real word stuff! But this is what all programming is about! So rephrase it: OOP help you to model real word EASIER! But it is that correct? I don’t think that is comprehensive what that mean EASIER! I struggled for a while and end up with what is the meaning of EASIER! It solve the DEPENDENCY PROBLEM!!!!
Yay that is it!! Dependency of class and properties, class and other classes, class and interface! This seems silly but this is very important. The reason is now we think about dependency as a core or our design not the properties of each object. Now we think top to bottom! If you think about properties and method of classes FIRST you are thinking about details which is changing alot in our design and do not think enough about TOP LEVEL RELATIONSHIP. So how should I do more attention to TOP LEVEL design? Easy with TDD! So you start with top level which is class and you think about dependencies! And this good design will lead you for good design. I will start write about what is whole thing about with example! Keep in touch!