当前位置:网站首页>Single responsibility principle
Single responsibility principle
2022-06-28 04:06:00 【The roaring Conan】
Principle of single responsibility
Definition
Principle of single responsibility (SRP), In terms of a class , There should be only one cause for it to change .
Simply speaking , Let a class do only part of the work , This reduces the coupling , Improve reusability , Improve readability , Reduce the risk caused by changes .
【 Common code 】
package pers.keafmd.accumulate.designmode.singleresponsibility;
/** * Keafmd * * @ClassName: FreshmenSelected * @Description: New students * @author: Cowherd Conan * @date: 2022-06-27 19:39 */
public class FreshmenSelected {
public void distributeBooks(){
System.out.println(" Distribute books ...");
}
public void allocateDormitory(){
System.out.println(" Assign dormitories ...");
}
public static void main(String[] args) {
System.out.println(" Start arranging for new students :");
FreshmenSelected freshmenSelected = new FreshmenSelected();
freshmenSelected.distributeBooks();
freshmenSelected.allocateDormitory();
System.out.println(" end-of-job !");
}
}
effect :
Start arranging for new students :
Distribute books ...
Assign dormitories ...
end-of-job !
Process finished with exit code 0
【 Principle of single responsibility 】

StudyMembers
package pers.keafmd.accumulate.designmode.singleresponsibility;
/** * Keafmd * * @ClassName: StudyMembers * @Description: Study commissar * @author: Cowherd Conan * @date: 2022-06-27 19:46 */
public class StudyMembers {
public void distributeBooks(){
System.out.println(" The study committee is responsible for : Distribute books ...");
}
}
LifeCommittee
package pers.keafmd.accumulate.designmode.singleresponsibility;
/** * Keafmd * * @ClassName: LifeCommittee * @Description: Life Commissioner * @author: Cowherd Conan * @date: 2022-06-27 19:47 */
public class LifeCommittee {
public void allocateDormitory(){
System.out.println(" The life committee is responsible for : Assign dormitories ...");
}
}
ClassMonitor
package pers.keafmd.accumulate.designmode.singleresponsibility;
/** * Keafmd * * @ClassName: ClassMonitor * @Description: Monitor of the class * @author: Cowherd Conan * @date: 2022-06-27 19:46 */
public class ClassMonitor {
private StudyMembers studyMembers;
private LifeCommittee lifeCommittee;
public ClassMonitor() {
studyMembers = new StudyMembers();
lifeCommittee = new LifeCommittee();
}
public void assignWork(){
System.out.println(" The monitor began to assign jobs to different class cadres , Work together to complete the enrollment of new students :");
studyMembers.distributeBooks();
lifeCommittee.allocateDormitory();
System.out.println(" end-of-job !");
}
public static void main(String[] args) {
System.out.println(" Start arranging for new students :");
ClassMonitor classMonitor = new ClassMonitor();
classMonitor.assignWork();
}
}
effect :
Start arranging for new students :
The monitor began to assign jobs to different class cadres , Work together to complete the enrollment of new students :
The study committee is responsible for : Distribute books ...
The life committee is responsible for : Assign dormitories ...
end-of-job !
Process finished with exit code 0
The above is the whole content of the principle of single responsibility
Copyright notice :
Original Blogger : Cowherd Conan
Blogger original link :https://keafmd.blog.csdn.net/
Personal blog links :https://www.keafmd.top/
If it helps you , Thank you for clicking on the like support below !
[ ha-ha ][ Huai Quan ]
come on. !
Joint efforts !
Keafmd
边栏推荐
- 电学基础知识整理(一)
- English grammar_ Adjective / adverb Level 3 - Comparative
- 11_刻意练习精讲
- GCD maximum common divisor
- Does the applet input box flash?
- 领歌leangoo敏捷看板工具新增导出卡片文档和粘贴共享脑图节点功能
- 第一个.net core MVC项目
- uni-app 如何根据环境自动切换请求的地址
- 基于正点原子stm32的mini板的TFTLCD显示实验
- What is the process of en 1101 flammability test for curtains?
猜你喜欢

How to write a software test report? Here comes the third party performance report template

Are the two flame retardant standards of European furniture en 597-1 and en 597-2 the same?

电学基础知识整理(二)

用Pycharm开发Flask框架设置debug模式没有效果的解决办法

Several ways of sharing printers in LAN

Leetcode: monotonic stack structure (Advanced)

【毕业季】研究生の毕业总结

Web APIs DOM event foundation dark horse programmer

美创数据安全管理平台获信通院“数据安全产品能力验证计划”评测证书

Chapter 1 Introduction to bash
随机推荐
Are the two flame retardant standards of European furniture en 597-1 and en 597-2 the same?
音频 scipy 中 spectrogram 的运作机制
Analysis of future teacher research ability under steam education framework
黑体辐射初探
使用信号分析器
等保2.0密码要求是什么?法律依据有哪些?
leetcode:494. All methods of adding and subtracting operators to the array to get the specified value
MySQL 主从复制、分离解析
How the uni app automatically switches the requested address according to the environment
2021年终总结及2022年展望
揭开SSL的神秘面纱,了解如何用SSL保护数据
错排兼排列组合公式
Pychart shares third-party modules among different projects
leetcode:494.数组中添加加减运算符得到指定值的所有方法
窗帘做EN 1101易燃性测试过程是怎么样的?
多项目设计开发·类库项目引入入门
Web APIs DOM event foundation dark horse programmer
机器人编程教育的市场竞争力
PostgreSQL 实现批量更新、删除、插入
电学基础知识整理(一)