当前位置:网站首页>Single responsibility principle
Single responsibility principle
2022-06-28 14:12:00 【Hua Weiyun】
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边栏推荐
猜你喜欢

DevEco Studio 3.0编辑器配置技巧篇

基于ASP的勤工俭学管理系统

做一个墨水屏电子钟,炫酷!

Work study management system based on ASP

CVPR再起争议:IBM中稿论文被指照搬自己承办竞赛第二名的idea

How to count dimensions of foreign trade E-mail Promotion

一个bug肝一周...忍不住提了issue

外贸邮件推广怎么统计维度

Nature子刊 | 绘制植物叶际菌群互作图谱以建立基因型表型关系

Reading notes of Mr. toad going to see a psychologist
随机推荐
增额终身寿险有哪些产品可以买呢?
Four methods of thread termination
The company leader said that if the personal code exceeds 10 bugs, he will be dismissed. What is the experience?
设计一个有getMin功能的栈
2022中式烹調師(高級)試題及在線模擬考試
PC Museum - familiar and strange ignorant age
A bug liver a week I can't help mentioning issue
MySQL从库Error:“You cannot ‘Alter‘ a log table...“
Arcgis 矢量中心点生成矩形并裁剪tif图像进行深度学习样本训练
Summary of 2021 computer level III database
Make an ink screen electronic clock, cool!
G: maximum flow problem
Other domestic mobile phones failed to fill the vacancy of Huawei, and apple has no rival in the high-end mobile phone market
原生JS 实现页面元素的拖动 拖拽
【中移芯昇】5. spi接口测试tf卡
Kubernetes in-depth understanding of kubernetes (I)
Foreign trade SEO Webmaster Tools
Solving Hanoi Tower problem
Multi dimensional monitoring: the data base of intelligent monitoring
外贸邮件推广怎么统计维度