Da Wikipedia su Principio di responsabilità singola SoC
... 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. All its services should be narrowly aligned with that responsibility
class or module should have one, and only one, responsibility . As an example, consider a module that compiles and prints a report. Imagine such a module can be changed for two reasons. First, the content of the report could change. Second, the format of the report could change. These two things change for very different causes; one substantive, and one cosmetic. The single responsibility principle says that these two aspects of the problem are really two separate responsibilities, and should therefore be in separate classes or modules. It would be a bad design to couple two things that change for different reasons at different times.
The reason it is important to keep a class focused on a single concern is that it makes the class more robust. Continuing with the foregoing example, if there is a change to the report compilation process, there is greater danger that the printing code will break if it is part of the same class.
Esiste una regola o una linea guida generale su come definire la responsabilità dei metodi in classe?
Quando parliamo di oggetti inanimati, è facile vedere il report di compilazione e il rapporto di stampa sono due diverse responsabilità. Tuttavia, quando si tratta di animare oggetti come il cane, diventa meno evidente quali metodi siano diversi in natura.
Supponiamo di avere:
class dog {
public $breed;
public $age;
public $color;
function bark($loudness) (
...
)
function pee($amount) (
...
)
function run($speed, $distance, $direction) (
...
)
function growl() (
...
)
function drink() (
...
)
}
come puoi dire quale responsabilità dovrebbe essere in una classe separata?