Single responsibility principle (splitting responsibilities)

basic concepts:

       For a class, a class should only be responsible for one responsibility.

Things to do with a single responsibility:

  1. Reduce the complexity of the class, a class is only responsible for one responsibility
  2. Improve the readability and maintainability of the class
  3. Reduce the risks caused by changes

Also note:

        When the logic is simple enough, the single responsibility principle can be violated at the code level;

        When the number of methods in the class is small enough, the single responsibility principle can be maintained at the method level, otherwise the single responsibility principle of the class should be strictly observed (divide the responsibility into multiple classes)

Example: Main category, operating various vehicles

public class SingleResponsibility1 {
	
	public static void main(String[] args) {

		Vehicle vehicle = new Vehicle();
		vehicle.run("摩托车");
		vehicle.run("汽车");
		vehicle.run("飞机");
	}

}

Method one, the principle of single responsibility is not strictly adhered to

// 交通工具类
// 方式1
// 1. 在方式1 的run方法中,违反了单一职责原则
// 2. 解决的方案非常的简单,根据交通工具运行方法不同,分解成不同类即可
class Vehicle {
	public void run(String vehicle) {
		System.out.println(vehicle + " 在公路上运行....");
	}
}

Output: Here you can find that the plane is running on the highway....This is wrong!

The responsibilities of the Vehicle class here are too broad, and different vehicles should take different paths. Therefore, the Vehicle class does not follow the single responsibility principle.

So it should be revised.

 Method 2: Use categories to split responsibilities and share responsibilities to different categories

//方案2的分析
//1. 遵守单一职责原则
//2. 但是这样做的改动很大,即将类分解,同时修改客户端
//3. 改进:直接修改Vehicle 类,改动的代码会比较少=>方案3

class RoadVehicle {
	public void run(String vehicle) {
		System.out.println(vehicle + "公路运行");
	}
}

class AirVehicle {
	public void run(String vehicle) {
		System.out.println(vehicle + "天空运行");
	}
}

class WaterVehicle {
	public void run(String vehicle) {
		System.out.println(vehicle + "水中运行");
	}
}

At this time the main class needs to be changed a little

public class SingleResponsibility2 {

	public static void main(String[] args) {

		RoadVehicle roadVehicle = new RoadVehicle();
		roadVehicle.run("摩托车");
		roadVehicle.run("汽车");
		
		AirVehicle airVehicle = new AirVehicle();
		
		airVehicle.run("飞机");
	}

}

 

At this time, the single responsibility principle is strictly observed

The third method is to observe a single responsibility at the method level , but not to observe a single responsibility at the class level. Because the function of this class is simple enough

Note: Do not use if...else if...else... classes to distinguish responsibilities here, because this method is highly coupled

//方式3的分析
//1. 这种修改方法没有对原来的类做大的修改,只是增加方法
//2. 这里虽然没有在类这个级别上遵守单一职责原则,但是在方法级别上,仍然是遵守单一职责
class Vehicle2 {
	public void run(String vehicle) {
		System.out.println(vehicle + " 在公路上运行....");
	}
	
	public void runAir(String vehicle) {
		System.out.println(vehicle + " 在天空上运行....");
	}
	
	public void runWater(String vehicle) {
		System.out.println(vehicle + " 在水中行....");
	}
}

The main class also needs a little change

public class SingleResponsibility3 {

	public static void main(String[] args) {

		Vehicle2 vehicle2  = new Vehicle2();
		vehicle2.run("汽车");
		vehicle2.runWater("轮船");
		vehicle2.runAir("飞机");
	}
}

 

Guess you like

Origin blog.csdn.net/qq_41813208/article/details/102980174