单一职责原则

Posted linbq1911

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单一职责原则相关的知识,希望对你有一定的参考价值。

1.定义:不要存在多于一个导致类变更的原因

2.一个类/接口/方法只负责一项职责

3.优点:降低类的复杂度、提高类的可读性,提高系统的可维护性、降低变更引起的风险

4.实例目录package

技术分享图片

5.实例UML类图

技术分享图片

6.代码

 1 package com.geely.design.principle.signleresponsibility;
 2 
 3 public class Bird {
 4 
 5     public void mainMoveMode(String birdName){
 6         if("鸵鸟".equals(birdName)){
 7             System.out.println(birdName + "用脚走");
 8         }else{
 9             System.out.println(birdName + "用翅膀飞");
10         }
11     }
12 }
1 package com.geely.design.principle.signleresponsibility;
2 
3 public class FlyBird {
4     public void mainMoveMode(String birdName){
5         System.out.println(birdName + "用翅膀飞");
6     }
7 }
1 package com.geely.design.principle.signleresponsibility;
2 
3 public class WalkBird {
4     public void mainMoveMode(String birdName){
5         System.out.println(birdName + "用脚走");
6     }
7 }
 1 package com.geely.design.principle.signleresponsibility;
 2 
 3 public class Test {
 4     public static void main (String[] args){
 5         /*Bird bird = new Bird();
 6         bird.mainMoveMode("大雁");
 7         bird.mainMoveMode("鸵鸟");*/
 8 
 9         FlyBird flyBird = new FlyBird();
10         flyBird.mainMoveMode("大雁");
11         WalkBird walkBird = new WalkBird();
12         walkBird.mainMoveMode("鸵鸟");
13     }
14 }
1 package com.geely.design.principle.signleresponsibility;
2 
3 public interface ICourse {
4     String getCourseName();
5     byte[] getCourseVideo();
6 
7     void studyCourse();
8     void refundCourse();
9 }
1 package com.geely.design.principle.signleresponsibility;
2 
3 public interface ICourseContent {
4     String getCourseName();
5     byte[] getCourseVideo();
6 }
1 package com.geely.design.principle.signleresponsibility;
2 
3 public interface ICourseManager {
4     void studyCourse();
5     void refundCourse();
6 }
package com.geely.design.principle.signleresponsibility;
public class CourseImpl implements ICourseManager,ICourseContent{//ICourse
    public String getCourseName() {
        return null;
    }

    public byte[] getCourseVideo() {
        return new byte[0];
    }

    public void studyCourse() {

    }

    public void refundCourse() {

    }
}
 1 package com.geely.design.principle.signleresponsibility;
 2 
 3 public class Method {
 4     private void updateUserInfo(String userName, String address){
 5         userName = "geely";
 6         address = "beijing";
 7     }
 8     private void updateUserInfo(String userName, String ... properties){
 9         userName = "geely";
10         //address = "beijing";
11     }
12 
13     private void updateUserName(String userName){
14         userName = "geely";
15     }
16     private void updateAddress(String address){
17         address = "beijing";
18     }
19 
20     private void updateUserInfo(String userName, String address, boolean bool){
21         if(bool){
22             //todo something1
23         }else{
24             //todo something2
25         }
26         userName = "geely";
27         address = "beijing";
28     }
29 }

 

以上是关于单一职责原则的主要内容,如果未能解决你的问题,请参考以下文章

java设计模式1,单一职责原则

深入浅出设计模式——单一职责原则

设计模式原则之:单一职责原则SRP

七大设计原则之单一职责原则应用

手撸golang 架构设计原则 单一职责原则

单一职责原则