Single Responsibility Principle Simple Example
Published: 2020-03-02 03:58:24

(Cover Photo by David McEachan from Pexels)

Good Day, guys!
Single Responsibility Principle is the first part of the SOLID principle. In this article, I’ll try to explain the Single Responsibility Principle in with a simple example. So, we can get the philosophy of it. Then we can implement it in our daily coding activity.

However, as a formality. Firstly, I will state it’s the definition from Wikipedia. Even though it is a little bit abstract, but it is still worth to get a theoretical definition of the Single Responsibility Principle.

In Wikipedia, The Single Responsibility Principle means:

“The single responsibility principle is a computer programming principle that states that every module or class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class, module or function. All its services should be narrowly aligned with that responsibility”

We were all beginner and maybe it needs a little extra effort to understand the definition above.

So, let’s jump to our illustration of the Single Responsibility Principle.

Let’s say that once upon a time, there was a pharaoh. He wanted to build a pyramid. He had a great and smart commander. He ordered his commander to build one.

Then the commander started to design the pyramid, he made the blueprint of the building structure. After that, he contacted the material supplier to provide all material that he needed. After ensuring all work well, he started to manage the labour to build the pyramid.

From this illustration above, we know how the Single Responsibility Principle was violated. Since the commander has all responsibility, from the design, arrange material supply and manage the labour. Of course, it was still possible to run. However, it wasn’t very adaptive and flexible. Moreover, it can cause high complexity.

Afterwards, the commander read a paper from Uncle Bob (Robert C Martin) about software design principle. And he interested to implement the Single Responsibility Principle. He hired several managers, and breakdown the responsibilities to departments. Each manager led a department and responsible for specific jobs.

![managers image]
(https://images.pexels.com/photos/3183150/pexels-photo-3183150.jpeg)
(Photo by fauxels from Pexels)

Yeah, the commander implemented a basic form of Single Responsibility Principle. Now, do you get the picture of the Single Responsibility Principle? Give me your own words to define it. And please do not stuck to Wikipedia definition, but we make it as a reference.

Next, let’s try to translate it to simple pseudocode. 

Before implementing the Single Responsibility Principle
```C#
Public class main(){
	//Design works
	decimal pyramidLength;
	decimal pyramidWidth;
	decimal pyramidHeight;
	decimal totalAreaPyramid = CalculatePyramidArea(pyramidLength, pyramidWidth, pyramidHeight);

	decimal materialSupply = CalculateMaterialSupply(totalAreaPyramid);
	
	int totalLabor = CalculateTotalLabor(totalAreaPyramid);

	return Build(totalAreaPyramid, materialSupply, totalLabor);
}
```
After implementing the Single Responsibility Principle
```C#
Public class main(){
	//Design responsibility
	BuildingBluePrint PyramidDesign = new BuildingBluePrint(BuildingTypeEnum.Pyramid, pyramidLength, pyramidWidth, pyramidHeight);
	
	//Material supply responsibility
	BuildingMaterial PyramidMaterial = new MaterialBuilding(PyramidDesign);
	
	//Labor management responsibility
	BuildingContstructionLabor PyramidLabor = new PyramidLabor(PyramidDesign);
	
	//Let’s build the pyramid
	Return Build(PyramidDesign, PyramidMaterial, PyramidLabor);
}
```
The code above is not working code, I just try to give the picture of the Single Responsibility Principle. Please let me know if you have any feedback, both for my explanation or for may pseudo code.
Next, I will continue with my second article about the Open Closed Principle.

Thanks.