当前位置:网站首页>03. simple responsibility principle
03. simple responsibility principle
2022-07-22 18:45:00 【One duck, one duck】
1. Definition
It means that there should not be more than one cause of class change . One class 、 Interface 、 Method is responsible for only one responsibility .
Suppose we have a Class In charge of two duties , Once there is a requirement change , Modify one of the responsibilities Logic code , It is possible to cause the function of another responsibility to fail . thus , This Class There are two guides Cause of class change . How to solve this problem ? We're going to use two for two responsibilities Class To achieve , Decoupling . Later requirements change and maintenance will not affect each other . This design , Can reduce the complexity of the class , Improve the can read sex , carry high system system Of can dimension guard sex , drop low change more lead rise Of wind risk . total body Come on say Just yes One individual Class/Interface/Method Be responsible for only one duty .
2. advantage
- Reduce class complexity
- Improve the readability of the class
- Improve the maintainability of the system
- Reduce the risk of change
3. Single responsibility case of class
Here is a course example , Suppose the course has live broadcast class and recording class . Live class can't fast forward and fast backward , You can watch it over and over again , The functions and responsibilities are different
3.1 Initial version code
- Course class
public class Course {
public void study(String courseName){
if (" Live class ".equals(courseName)){
System.out.println(courseName + " Don't fast forward ");
}else{
System.out.println(courseName + " You can watch it over and over again ");
}
}
}
- Calling class
public class SRPTest {
public static void main(String[] args) {
Course course = new Course();
course.study(" Live class ");
course.study(" Recording and broadcasting course ");
}
}
3.2 Optimize
From the code above ,Course Class takes on two kinds of processing logic . If , Now we need to encrypt the course , that The encryption logic of live class and video class is different , You have to change the code . And modifying the code logic is bound to affect each other, easily causing uncontrollable risks . We separate and decouple our responsibilities , Look at code , Create two classes ReplayCourse and LiveCourse
- ReplayCourse
public class ReplayCourse {
public void study(String courseName){
System.out.println(courseName + " You can watch it over and over again ");
}
}
- LiveCourse
public class LiveCourse {
public void study(String courseName){
System.out.println(courseName + " Don't fast forward ");
}
}
- Calling code
public class SRPTest {
public static void main(String[] args) {
LiveCourse liveCourse = new LiveCourse();
liveCourse.study(" Live class ");
ReplayCourse replayCourse = new ReplayCourse();
replayCourse.study(" Recording and broadcasting course ");
}
}
4. Single responsibility case of interface
Business continues to grow , The course should be authorized . Students who don't pay can get the basic information of the course , Paid School Members can get video streams , That is, the right to learn . So there are at least two responsibilities at the control curriculum level . We can Separate presentation responsibilities from management responsibilities , Both implement the same abstract dependency . Design a top-level interface , establish ICourse Interface
4.1 initial
- ICourse Interface
public interface ICourse {
// Get basic information
String getCourseName();
// Get the video stream
byte[] getCourseVideo();
// Study the course
void studyCourse();
// refund
void refundCourse();
}
4.2 Optimize
This interface can be divided into two interfaces , Create an interface ICourseInfo and ICourseManager
- ICourseInfo
public interface ICourseInfo {
// Get basic information
String getCourseName();
}
- ICourseManager
public interface ICourseManager {
// Get the video stream
byte[] getCourseVideo();
// Study the course
void studyCourse();
// refund
void refundCourse();
}
- CourseImpl Implementation class
public class CourseImpl implements ICourseInfo,ICourseManager{
@Override
public String getCourseName() {
return null;
}
@Override
public byte[] getCourseVideo() {
return new byte[0];
}
@Override
public void studyCourse() {
}
@Override
public void refundCourse() {
}
}
5. The single responsibility of the method
5.1 initial
occasionally , We are lazy , A method is usually written as follows
public class Method {
private void modifyUserInfo(String userName,String address){
userName = "Mike";
address = "GuangZhou";
}
private void modifyUserInfo(String userName,String ... fileds){
}
private void modifyUserInfo(String userName,String address,boolean bool){
}
}
5.2 Optimize
obviously , above modifyUserInfo() There are many responsibilities in the method , You can modify userName, Can also be To modify address, Even more , Obviously not in line with a single responsibility . So let's make the following changes , Dismantle this method Into two
private void modifyUserName(String userName){
}
private void modifyUserAddress(String address){
}
6. Summary
After the modification , Easy to develop , It's easy to maintain . however , We will rely on projects in actual development , Combine , Aggregate these relationships , And the scale of the project , cycle , The level of technical personnel , Control the progress , Many categories do not meet a single responsibility . however , We are in the process of writing code , Try to keep interfaces and methods Single responsibility , It is very helpful for the later maintenance of our project .
边栏推荐
- 总结20220209
- 微信小程序本地访问本地TP5的路由接口正常,为何在手机上扫码预览获取不到数据?
- Method of getting node binding data data index by applet
- 数据湖简单记录
- PHP开发中csrf攻击的简单演示和防范
- 真的有必要定义VO,BO,PO,DO,DTO吗?
- Go 管道模式的实际例子——计算一系列文件的 md5 值
- [SDIO] SDIO, SD card, FatFs file system related article index
- Codeforces Round #805 (Div. 3)(8/8)
- Codeforces Round #799 (Div. 4)(8/8)
猜你喜欢
随机推荐
别让恐婚,扼杀你幸福!
DOM简介及查询
Codeforces Round #805 (Div. 3)(8/8)
debug glance(by quqi99)
creating vlan over openstack (by quqi99)
【SDIO】SD2.0协议分析总结(三)-- SD卡相关命令介绍
协议与端口
【FatFs】FAT32文件系统协议总结(理论+实践)
MySQL优化之强制索引的使用
总结20220121
call()和apply()
ecshop怎么在本地跑超级慢?
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/mysqli.so'
Fabric. JS centered element
Codeforces Round #806 (Div. 4)(7/7)
3、 Generics
wampserver搭建到腾讯云服务,localhost能访问到apache,域名也能访问到自带的iis服务器,但域名访问不了
Web novice zone
H5 no scaling on the mobile terminal
使用工厂的方法创建对象