码迷,mamicode.com
首页 > 其他好文 > 详细

单一职责原则

时间:2019-01-01 14:05:42      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:rop   man   equals   类图   todo   equal   prope   ref   else   

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 }

 

单一职责原则

标签:rop   man   equals   类图   todo   equal   prope   ref   else   

原文地址:https://www.cnblogs.com/linbq1911/p/10204593.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!