Does the Single Responsibility Principle work in O

2020-02-01 02:17发布

I am struggling to understand how the Single Responsibility Principle can me made to work with OOP.

If we are to follow the principle to a tee, then are we not left with many classes, many of which may just have one method each?

If we don't follow the principle exactly, then what is the point in the principle?

7条回答
Emotional °昔
2楼-- · 2020-02-01 02:38

A class shall handle one topic, that's its single responsibility. A class Car could contain methods like startEngine() or attributes like weight:

class Car
{
    int weight;
    void startEngine();
    void stopEngine();
}

Of course you can break up this class more. For example by defining a class for the engine:

class Engine
{
    start();
    stop();
}

class Car
{
    Engine engine;
    int weight;
}

But you shall not define seperate classes for starting and stopping the engine like:

class EngineStop
{
    stop();
}

class EngineStart
{
    start();
}

class Car
{
    EngineStart engineStart;
    EngineStop engineStop;
    int weight;
}

The principle says to break up classes as reasonable as possible to achieve abstractness. Abstracting too deep violates this principle.

查看更多
The star\"
3楼-- · 2020-02-01 02:45

Single Responsibility term was introduced by Rober C. Martin. Main motto of this principle is a reason to change. A class or module should have one, and only one, reason to be changed.The major benefit of this principle is that is makes the class more robust. Changes in one class or module doesn't break the other part.

It prevents an object from becoming a God Object that is an example of Anti-Pattern. It also prevents from Ravioli code (A source code with lots of tiny, tightly-coupled objects).

Hence, Single responsibility principle is an important part of good source code design in OOP.

查看更多
我只想做你的唯一
4楼-- · 2020-02-01 02:50

This is one of the common misunderstanding of Single Responsibility Principle that a class should have only one function. A class is responsible for only one thing doesn't mean a class should have only one function. Single Responsibility Principle means logically separate your functionality into different classes instead of mixing up things.

One very simple example is, say if you are creating a Calculator you can add all functionalities purely for the calculator in the same class (add, subtract, multiply, divide etc.). You don't need to create a separate class for each of the functionality of calculator. But if you want to print calculated result to a printer then don't write the logic of printing to printer inside calculator class because printing to the printer is not the responsibility of calculator class. Create a separate class for the printer and write printing related logic in that class. If you are writing printing functionalities inside Calculator class then you are violating Single Responsibility Principle.

查看更多
一夜七次
5楼-- · 2020-02-01 02:51

Before you go into decomposing responsibilities, you should have an idea on what the class is supposed to do. It's supposed to do that thing and only that thing as Matt mentioned in his answer. Implement that idea in the simplest way possible just to make it work. That's when you go about decomposing the code you wrote into responsibilities.

The important thing to consider when you break down your code is to make sure that it is readable after breaking it down.

查看更多
6楼-- · 2020-02-01 02:53

I tend to think of the Single Responsibility Principle (and really a lot of the rules of thumb, patterns, and models) as a general way of thinking about problems. It's meant to guide you toward improvements to your code, but not necessarily prescribe exactly what a solution looks like. Because ultimately software design is a wicked and subjective problem.

I don't think that SRP should always lead to a bunch of single function classes. Though these may sometimes occur if you have some complex logic that you want to abstract away. In general, though, you should trend toward classes with highly cohesive functionality where methods:

  1. Are all related to the same abstraction(s)

    • Don't have a mish-mash of functionality that isn't related to each other in the same class.
    • Keep things like business logic separate from UI separate from I/O
  2. Share common dependencies

    • Related functionality usually has related dependencies.
    • I usually consider breaking up a class when I start getting more than around 7 dependencies. Often there's a class I can extract to move a few of the dependencies into a more cohesive unit.
  3. All operate at the same level of abstraction

    • Your abstractions should be hierarchical; some classes should orchestrate other classes to accomplish a workflow. Other classes will handle the specific implementation-level details.
    • Avoid having orchestration-style classes getting into the nitty-gritty. You don't want detailed operations to live side by side (in the same function or even the same class) with abstractions of more complex operations.

You actually want to try to group as much functionality as you can, while maintaining the above conditions and keeping in mind how understandable your abstraction is. Ultimately the goal of the Single Responsibility Principle is to help manage complexity and make the code more understandable.

查看更多
SAY GOODBYE
7楼-- · 2020-02-01 02:58

I quote Robert C. Martin from his book Clean Code:

The Single Responsibility Principle (SRP) states that a class or module should have one, and only one, reason to change. This principle gives us both a definition of responsibility, and a guidelines for class size. Classes should have one responsibility—one reason to change.

查看更多
登录 后发表回答