What are the Solid principles that developers should follow?

What is the Solid principle?

In object-oriented programming, rules designed to make it easier for developers to create programs that are easy to read and maintain.

Meaning of Solid
S: SRP, Single Responsibility Principle
O: OCP, open-close principle
L: LSP, Liskov substitution principle
I: ISP, interface separation principle
D: DIP, Dependency Inversion Principle

This time, I would like to explain ``SRP, the Single Responsibility Principle'' and ``OCP, the Open and Close Principle.''

What is the single responsibility principle?

When I looked into it, it was explained as follows
-One class must have only one responsibility.
・When a part of the software specifications is changed, the specifications affected by the change are
Must be a specification for that class
・There should not be more than one reason to change the class.
In other words, if you chew
・Let one program (class) perform only one function.

Jutoku knife is convenient, but in the world of programming there is no need for Jutoku knife.
If a four-dimensional pocket existed in the real world, there would be no need for a jutoku knife.
When you need a bottle opener, you don't have to take the Jutoku knife out of your four-dimensional pocket and use the Jutoku knife bottle opener, just take out the special bottle opener.

▼Actual source code NG pattern



Output result
→PC:100000 yen

If there is an issue to "make it possible to calculate the tax-included price of the product" and another issue to "change the display", both issues will result in modifications to the RegistShowCommodity class.

Divided into the following 3 classes
RegistCommodity class that registers products
CalcCommodity class that calculates product prices
ShowCommodity class that displays prices

concrete code


Output result
■CommodityName :PC
■CommodityPrice:110000 yen

Single Responsibility Principle Summary

By separating the classes, you can eliminate conflicts between files caused by multiple programmers modifying a single class.
Now that the classes to be changed are clear, it is easier to understand the scope of impact after modification.

In game development, character movement and gravity are each a separate class.
There were many designs in which the class was given to the character.

What is the open-close principle?

When I looked into it, it was explained as follows
-must be open to expansion;
-Must be closed to modification
In other words, if you chew
・If any functionality is added, the existing code will be
To be able to respond by adding objects without making any changes
・When a bug is fixed, fix only the object with the bug.
Rubbing to finish the correspondence

▼Actual source code NG pattern
If you want to add a new type of status to this Mario (such as Metal Mario), you will need to make changes to the attack method and miss method.


▼Specific response method
Solve using interfaces
・Mario interface


・Mario's own class


・Class that manages Mario's state


Summary of open-close principles

You can now extend the functionality of the attack and miss methods without making any changes.
On the other hand, if you need to change the behavior of jump (such as Mario with wings), you will need to make changes to the existing source code. In this case, it is still an OCP violation.

It is not realistic to assume every case and close it perfectly, and if the behavior of jump does not change, the source code will become unnecessary complicated.

Rather than trying to respond to every request, a realistic policy is to apply OCP only after actual changes occur for unpredictable areas.

Summary of S and O of Solid original work

It is unwise to apply principles including SRP when there are no signs of change.
It is nonsense to aim at applying principles.
Finding cohesive roles and separating them is the essence of software design.