1. Basic introduction
In the case of classes, a class should be responsible for only one responsibility. If class A is responsible for two different responsibilities: Responsibility 1 and responsibility 2, when the requirements of responsibility 1 change and responsibility A changes, it may cause errors in the execution of responsibility 2. Therefore, the granularity of class A needs to be decomposed into A1, and A2 is responsible for responsibility 1 and responsibility 2 respectively
2. Application cases
For example, the different modes of transportation: cars run on roads, planes run in the air, ships run in water
-
Option 1: Design a vehicle class that runs with different vehicles
public class SingleResponsibility1 { public static void main(String[] args) { Vehicle vehicle =new Vehicle(); vehicle.run("Car"); vehicle.run("Plane"); vehicle.run("Ship"); }}class Vehicle{ public void run(String vehicle){ System.out.println(vehicle+"Running on the highway...."); }}Copy the code
Running results:
The car runs on the highway.... The plane is running on the highway.... The ship runs on the highway....Copy the code
It can be seen from the operation results: 1. Planes and ships should not run on roads, which violates the principle of single responsibility in the run method. 2
-
Different modes of transportation are divided into different classes
public class SingleResponsibility2 { public static void main(String[] args) { RoadVehicle roadVehicle = new RoadVehicle(); roadVehicle.run("Car"); AirVehicle airVehicle = new AirVehicle(); airVehicle.run("Plane"); WaterVehicle waterVehicle = new WaterVehicle(); waterVehicle.run("Ship"); }}class RoadVehicle { public void run(String vehicle) { System.out.println(vehicle + "Running on the highway...."); }}class AirVehicle { public void run(String vehicle) { System.out.println(vehicle + "Run in the sky...."); }}class WaterVehicle { public void run(String vehicle) { System.out.println(vehicle + "Running in water...."); }}Copy the code
Running results:
The car runs on the highway.... The plane moves in the sky.... Ships run in the water....Copy the code
It can be seen from the operation results that: 1. Different vehicles adopt different operation modes and comply with the principle of single responsibility. 2
-
Different means of transportation operate in the same class using different methods
public class SingleResponsibility3 { public static void main(String[] args) { Vehicle2 vehicle =new Vehicle2(); vehicle.run("Car"); vehicle.runAir("Plane"); vehicle.runWater("Ship"); }}class Vehicle2{ public void run(String vehicle){ System.out.println(vehicle+"Running on the highway...."); } public void runAir(String vehicle ){ System.out.println(vehicle+"Run in the sky...."); } public void runWater(String vehicle ){ System.out.println(vehicle+"Running in water...."); }}Copy the code
Running results:
The car runs on the highway.... The plane moves in the sky.... Ships run in the water....Copy the code
It can be seen from the running results: 1. This modification method does not modify the original class very much, but only adds methods, and calls different methods according to different implementation methods. 2
Why should we follow the single responsibility principle? For example, the cars running on the highway increase the demand for tail-number restrictions. We only need to modify the functions of the cars running on the highway without modifying the methods running in the sky or water. In this way, after the modification is completed, we only need to test the modified highway operation method to reduce the risks caused by the change.
3. Notes and details
- Reduce the complexity of classes so that each class has only one responsibility
- Improve readability and maintainability of classes
- Reduce the risk of change
- In general: the single responsibility principle should be followed, and only the logic is simple enough to violate the single responsibility principle at the code level; Only the number of methods in a class is small enough to maintain the single responsibility principle at the method level