单一职责原则:

基本介绍:

  对类来说,即一个类应该只负责一项职责(不代表类中只能有一个方法)。如类A负责两份不同的职责,职责1,职责2当职责1需求变更而改变A时,可能造成职责2执行错误,所以需要将类A的粒度分解为A1,A2。

案例:

方式一:

 2 
 3 /**
 4  * @auther cheng
 5  * @create 2020-08-12 22:16
 6  * 单一职责原则
 7  */
 8 public class SingleResponsibility1 {
 9 
10     public static void main(String[] args) {
11 
12         Vehicle vehicle = new Vehicle();
13         vehicle.run("汽车");
14         vehicle.run("摩托车");
15         vehicle.run("轮船");
16 
17     }
18 
19 }
20 //方式一:
21 //交通工具类
22 //存在的问题:
23 //1.在run方法中,违反了单一职责原则
24 //2.解决的方案:根据交通工具运行环境的不同,分解成不同的类即可
25 class Vehicle{
26     public void run(String vehicle){
27         System.out.println(vehicle + "在公路上跑...");
28     }
29 }

汽车在公路上跑...
摩托车在公路上跑...
轮船在公路上跑...  职责2执行错误

复制代码

存在的问题:

1.在run方法中,违反了单一职责原则

2.解决方案:根据交通工具运行环境的不同,分解成不同的类即可

 

方式二:

 1 package cn.rabcheng.singleresponsibility;
 2 
 3 /**
 4  * @auther cheng
 5  * @create 2020-08-12 22:16
 6  * 单一职责原则
 7  */
 8 public class SingleResponsibility2 {
 9 
10     public static void main(String[] args) {
11 
12         RoadVehicle roadVehicle = new RoadVehicle();
13         AirVehicle airVehicle = new AirVehicle();
14         WaterVehicle waterVehicle = new WaterVehicle();
15 
16         roadVehicle.run("汽车");
17 
18         airVehicle.run("飞机");
19 
20         waterVehicle.run("轮船");
21 
22     }
23 
24 }
25 
26 //方式二:
27 //1.遵守单一职责
28 //2.但是这么做改动很大,即将类分解且修改客户端(main方法)
29 //3.改进:直接修改Vehicle类,改动代码会比较少
30 class RoadVehicle{
31     public void run(String vehicle){
32         System.out.println(vehicle + "在公路上跑...");
33     }
34 }
35 
36 class AirVehicle{
37     public void run(String vehicle){
38         System.out.println(vehicle + "在天上上飞...");
39     }
40 }
41 
42 class WaterVehicle{
43     public void run(String vehicle){
44         System.out.println(vehicle + "在水上上跑...");
45     }
46 }
47 
48 汽车在公路上跑...
49 飞机在天上上飞...
50 轮船在水上上跑...
复制代码

遵守单一职责,

但是这么做改动很大,即将类分解且改动客户端(main方法)

改进:直接修改Vehicle类,改动代码会比较少

 

方式三:

 1 package cn.rabcheng.singleresponsibility;
 2 
 3 /**
 4  * @auther cheng
 5  * @create 2020-08-12 22:16
 6  * 单一职责原则
 7  */
 8 public class SingleResponsibility3 {
 9 
10     public static void main(String[] args) {
11 
12         Vehicle2 vehicle = new Vehicle2();
13         vehicle.runRoad("汽车");
14         vehicle.runAir("飞机");
15         vehicle.runWater("轮船");
16 
17     }
18 
19 }
20 
21 //方式三:
22 //1.这种方法没有对原来的类做很大的修改,只是增加方法
23 //2.这里虽然没有在类级别上遵守单一职责原则,但是在方法级别遵守单一职责原则
24 class Vehicle2{
25     public void runRoad(String vehicle){
26         System.out.println(vehicle + "在公路上跑...");
27     }
28 
29     public void runAir(String vehicle){
30         System.out.println(vehicle + "在天上上飞...");
31     }
32 
33     public void runWater(String vehicle){
34         System.out.println(vehicle + "在水上上跑...");
35     }
36 }
37 汽车在公路上跑...
38 飞机在天上上飞...
39 轮船在水上上跑...

这里没有对原来的类做很大的修改,只是增加方法

虽然没有带类级别遵守单一职责原则,但是在方法级别遵守单一职责原则

 

注意事项和细节

  1. 降低类的复杂度,一个类负责一项职责(例如一个类负责一张数据库表的增删改查)
  2. 提高类的可读性,可维护性
  3. 降低变更引起的风险
  4. 通常情况下,当我们遵守单一职责原则,只有逻辑足够简单,才可以在代码级别违反单一职责原则,只有类中方法数量足够少,可以在方法级别保持单一职责原则
posted on 2020-08-12 22:54  -星星点灯丶  阅读(97)  评论(0编辑  收藏  举报