java design patterns study notes - Single Responsibility Principle

Single Responsibility Principle considerations and details

1. Reduce the complexity of the class, a class is only responsible for a duty
2. To improve readability, maintainability
3. Reduce the risk caused by changes
4. Under normal circumstances, we should abide by the principle of single responsibility, only logic is simple enough, it can violate the principle of a single duty at the code level, only a small number of class methods enough, we can maintain a single responsibility principle at the method level

Beginners wording

public class SingleResponsibility01 {
    public static void main(String[] args) {
        Vehicle vehicle =new Vehicle();
        vehicle.run("摩托车");
        vehicle.run("汽车");
        vehicle.run("飞机");
    }
}

//交通工具类
//方式1
//1.在方式1 的run方法中,违反了单一职责原则
//2.解决的方案非常的简单根据交通工具运行方式的不通,分解成不通类即可

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

Single Responsibility Principle 1.0 wording to comply

public class SingleResponsibility2 {
    public static void main(String[] args) {
        RoadVehicle roadVehicle = new RoadVehicle();
        roadVehicle.run("摩托车");
        roadVehicle.run("汽车");
        
        AirVehicle airVehicle = new AirVehicle();
        airVehicle.run("飞机");
    }
}

//方案2的分析
//1. 遵守单一职责原则
//2. 但是这样做的改动很大,即将类分解,同时修改客户端

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

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

Single Responsibility Principle 2.0 wording to comply

public class SingleRespnsibility3 {
    public static void main(String[] args) {
        Vihicle vihicle = new Vihicle();
        vihicle.run("汽车");
        vihicle.runAir("飞机");
        vihicle.runWater("轮船");
    }
}

//方式3的分析
//1. 这种修改方法没有对原来的类很大的修改,只是增加方法
//2. 这里虽然没有在类这个级别上遵守单一职责原则,但是在方法级别上,仍然是遵守单一职责

class Vihicle{
    void run(String vehicle) {
        System.out.println(vehicle + "在公路上运行。。。");
    }
    
    void runAir(String vehicle) {
        System.out.println(vehicle + "在天空上运行。。。");
    }
    
    void runWater(String vehicle) {
        System.out.println(vehicle + "在水里运行。。。");
    }
}

Analysis of Three wording of

1. The first way to write

The first writing is a writing novice commonly used. Writing simple, but the plane is running on the road is certainly not correct. In other words, now in charge of a class of two functions (run method, a run on the road it is a duty, while the aircraft is not running on the road, so the aircraft needed to achieve other duties).

2. The second wording

The second single written in compliance with the principles of responsibility, but every time you need different responsibilities, we need to create a class to be achieved, the cost is too high

3. The third wording

Although the wording of the third did not comply with the principle of responsibility in a single class on this level, but in compliance with the principle of responsibility on a single method, when a class method less suitable enough time

Guess you like

Origin www.cnblogs.com/windowsxpxp/p/11567513.html