The open and closed principle (OCP) and the single responsibility (SRP) do not understand the difference.

Many people on

use calculator examples to illustrate these two principles, but it seems that the difference is that OCP has an abstract class, while SRP does not inherit abstraction

compared with SRP, OCP, the examples of

1.OCP are all independent of each function. Doesn"t this accomplish a single responsibility?

2. Then it is said that OCP can be open to extension, and SRP can also do it. When other operations are needed, just add a corresponding class. It"s just that compared with OCP, it doesn"t inherit abstractions to constrain classes, so I don"t quite understand the difference here

.

3. And what does the so-called "modified closure" mean? I don"t quite understand. I don"t know which class I"m talking about. After all, even though it"s easy to extend (add) an operation class to OCP, you still have to modify it in the home page (add one level in if else or switch case)

.

4. Many materials say that they inherit abstraction, so they don"t understand the modification

.

A well-designed module for others to use, that person needs some new functions, how to use it, is to modify the existing module code? Or expand it by other means? The principle of opening and closing is that if your module is strong enough, you will not force others to change your source code. When using a module that is powerful enough, you should extend it to achieve your own function instead of changing other people's code.

single responsibility is easy to understand, for a component, multi-function is not necessary, the sofa is the sofa, the bed is the bed, the sofabed can only be an expedient measure.

the similarity between the two is "do not add entities if it is not necessary", do not change the code if there is no need to change the code, and do not add functions if you do not need to add functions. Through the splicing and combination of components to achieve the overall function, so that we can achieve the ideal of "high cohesion, low coupling".

of course, these two principles are contradictory in some way, how can the module be strong enough? Big and all is fine, but big and all is obviously in conflict with a single responsibility, so there is another trade-off. But don't worry, "trade-offs" are ubiquitous, and "choice is more important than effort" means the importance of trade-offs.


  1. SRP is easy to understand, and a class can have only one responsibility. OCP can be intuitively reflected in that the completed classes can not be modified, but can expand their own functions, and then join SRP, the performance of this expansion can not be more than one reason. That is, an abstract computing class, after the addition of the calculator is extended by creating a class inheritance, and then by creating a third class to expand multiplication, the caller chooses the implementation of any abstract class to obtain unlimited functions, and each written computing class does not need to be modified, which is the so-called OCP. However, when you try to expand a bank compound interest calculation class, you may violate SRP;
  2. as you said, OCP is achieved by adding corresponding classes, that is, adding multiplication calculation does not need to modify the original addition calculation class, addition calculation class is closed to modification. Computing an abstract class can be extended with a subclass that is constantly created, such as adding multiplicative computation, which means that the computational class is open to extension. By the same token, this does not affect that each subclass is a single job, and happens to reflect that these subclasses are well in line with these two principles.
  3. closed in the finger addition subclass, you don't need to worry about the problem with your addition algorithm after adding multiplication, it is closed.
  4. is the same as 1BI 2BI 3. New modules are added through inheritance, and specific word classes are selected by the caller.
Menu