当前位置:网站首页>单一职责原则
单一职责原则
2022-06-28 14:09:00 【华为云】
单一职责原则
定义
单一职责原则(SRP),就一个类而言,应该仅有一个引起它变化的原因。
简单来说,就是让一个类只干一部分事情,这样可以降低耦合性,提高复用性,提高可读性,降低由变更引起的风险。
【普通代码】
package pers.keafmd.accumulate.designmode.singleresponsibility;/** * Keafmd * * @ClassName: FreshmenSelected * @Description: 新生入学 * @author: 牛哄哄的柯南 * @date: 2022-06-27 19:39 */public class FreshmenSelected { public void distributeBooks(){ System.out.println("分发书籍。。。"); } public void allocateDormitory(){ System.out.println("分配宿舍。。。"); } public static void main(String[] args) { System.out.println("开始安排新生入学工作:"); FreshmenSelected freshmenSelected = new FreshmenSelected(); freshmenSelected.distributeBooks(); freshmenSelected.allocateDormitory(); System.out.println("工作结束!"); }}效果:
开始安排新生入学工作:分发书籍。。。分配宿舍。。。工作结束!Process finished with exit code 0【单一职责原则】

StudyMembers
package pers.keafmd.accumulate.designmode.singleresponsibility;/** * Keafmd * * @ClassName: StudyMembers * @Description: 学习委员 * @author: 牛哄哄的柯南 * @date: 2022-06-27 19:46 */public class StudyMembers { public void distributeBooks(){ System.out.println("学习委员负责:分发书籍。。。"); }}LifeCommittee
package pers.keafmd.accumulate.designmode.singleresponsibility;/** * Keafmd * * @ClassName: LifeCommittee * @Description: 生活委员 * @author: 牛哄哄的柯南 * @date: 2022-06-27 19:47 */public class LifeCommittee { public void allocateDormitory(){ System.out.println("生活委员负责:分配宿舍。。。"); }}ClassMonitor
package pers.keafmd.accumulate.designmode.singleresponsibility;/** * Keafmd * * @ClassName: ClassMonitor * @Description: 班长 * @author: 牛哄哄的柯南 * @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("班长开始给不同的班干部分配工作,共同完成新生入学工作:"); studyMembers.distributeBooks(); lifeCommittee.allocateDormitory(); System.out.println("工作结束!"); } public static void main(String[] args) { System.out.println("开始安排新生入学工作:"); ClassMonitor classMonitor = new ClassMonitor(); classMonitor.assignWork(); }}效果:
开始安排新生入学工作:班长开始给不同的班干部分配工作,共同完成新生入学工作:学习委员负责:分发书籍。。。生活委员负责:分配宿舍。。。工作结束!Process finished with exit code 0边栏推荐
- CVPR再起争议:IBM中稿论文被指照搬自己承办竞赛第二名的idea
- Websocket automatically disconnects in 1 minute
- Why can't Bert completely kill the BM25??
- RSLO:自监督激光雷达里程计(实时+高精度,ICRA2022)
- Reverse a stack with recursive functions and stack operations only
- Navicat Premium 16 永久破解激活工具及安装教程(亲测可用)
- 如何设计数据可视化平台
- 中国内地仅四家突围 联想智慧颐和园荣获 “2022年IDC亚太区智慧城市大奖”
- [binary tree] allocate coins in the binary tree
- Nature子刊 | 绘制植物叶际菌群互作图谱以建立基因型表型关系
猜你喜欢

To be the Italian Islander? Liuqiangdong cashed out 6.6 billion yuan in two months and made a one-time 560million "emergency transfer" to buy the European maritime Palace

Zhongang mining focuses on the fluorine chemical industry and lays out the new energy industry chain

Websocket automatically disconnects in 1 minute

开源社邀您参加OpenInfra Days China 2022,议题征集进行中~

木兰开放作品许可证1.0面向社会公开征求意见

Euler equation: a truly perfect formula in the history of mathematics!

Connected to rainwater series problems

Open source invites you to participate in openinfra days China 2022. Topic collection is in progress ~

Deveco studio 3.0 editor configuration tips

Special test for cold and hot start of app
随机推荐
First knowledge of exception
VPS是干嘛用的?有哪些知名牌子?与云服务器有什么区别?
Yii2 connects to websocket service to realize that the server actively pushes messages to the client
PC Museum - familiar and strange ignorant age
[binary tree] allocate coins in the binary tree
PostgreSQL超越MySQL
木兰开放作品许可证1.0面向社会公开征求意见
Nature | mapping the interaction map of plant foliar flora to establish genotype phenotype relationship
Who is the main body of the waiting insurance record? Record in the local network security, right?
NFT digital collection system development (3D modeling economic model development case)
Reading notes of Mr. toad going to see a psychologist
NPOI导出Excel并下载到客户端
有效提高绩效面谈的10个关键点
众昂矿业着眼氟化工产业,布局新能源产业链
Reverse a stack with recursive functions and stack operations only
2021计算机三级数据库大题总结
Multi dimensional monitoring: the data base of intelligent monitoring
Black apple installation tutorial OC boot "suggestions collection"
中国内地仅四家突围 联想智慧颐和园荣获 “2022年IDC亚太区智慧城市大奖”
Explanation of sprintf function in C language