当前位置:网站首页>05. Law of Demeter LOD
05. Law of Demeter LOD
2022-07-22 18:45:00 【One duck, one duck】
1. Definition
- It means that an object should have the least knowledge of other objects , also To know the principle at least (Least Knowledge Principle,LKP), Minimize the coupling between classes .
- Dimitar's principle emphasizes only communicating with friends , Don't talk to strangers . Appears in the member variable 、 Method input 、 All classes in the output parameters can be called member friend classes , The class that appears inside the method body does not belong to the friend class .
2. Case study
Case scenario : Now let's design a permission system ,Boss Need to see the number of courses currently posted online . Now ,Boss To find the TeamLeader Go and count ,TeamLeader Tell the statistics to Boss.
2.1 Initial code
- Course
public class Course {
}
- TeamLeader
public class TeamLeader {
public void checkNumberOfCourse(List<Course> courseList){
System.out.println(" The number of courses published so far is :"+courseList.size());
}
}
- Boss
public class Boss {
public void commandCheckNumber(TeamLeader teamLeader){
// simulation Boss Page by page, page by page ,TeamLeader Real time statistics
List<Course> courseList = new ArrayList<Course>();
for (int i = 0; i < 20; i++) {
courseList.add(new Course());
}
teamLeader.checkNumberOfCourse(courseList);
}
}
- LDLTest
public class LDLTest {
public static void main(String[] args) {
Boss boss = new Boss();
TeamLeader teamLeader = new TeamLeader();
boss.commandCheckNumber(teamLeader);
}
}
From the above code functions have been implemented , The code doesn't seem to have a problem . But according to the Demeter principle ,Boss Just want results , You don't need to follow Course Generate direct communication . and TeamLeader Statistics need to quote Course object .Boss and Course It's not a friend , As you can see from the class diagram below
2.2 Optimize the code
- TeamLeader
public class TeamLeader {
public void checkNumberOfCourse(){
// simulation Boss Page by page, page by page ,TeamLeader Real time statistics
List<Course> courseList = new ArrayList<Course>();
for (int i = 0; i < 20; i++) {
courseList.add(new Course());
}
System.out.println(" The number of courses published so far is :"+courseList.size());
}
}
- Boss
public class Boss {
public void commandCheckNumber(TeamLeader teamLeader){
teamLeader.checkNumberOfCourse();
}
}
Class diagram again ,Course and Boss There's no connection anymore .
边栏推荐
猜你喜欢
04.接口隔离原则(Interface Segregation Principle)
[STM32] STM32 SDIO SD card read / write test (III) -- SD_ Init card stage of init
【Audio】基于STM32 I2S移植WM8978 Audio Codec驱动
枚举对象中属性
【Nordic】nRF52810 OTA升级(二)–DFU如何使用
TCP与UDP及三次握手,四次挥手
包装类和字符串的方法
Mail Informer
24 条 SaaS 思考
[FatFs] FAT32 file system protocol summary (Theory + Practice)
随机推荐
DOM添加
H5 no scaling on the mobile terminal
[STM32] STM32 SDIO SD card read / write test (III) -- SD_ Init card stage of init
3. Transbot modifies the display resolution
Codeforces Round #805 (Div. 3)(8/8)
【SDIO】SD2.0协议分析总结(二)-- SD卡识别&数据传输过程
OSI model, tcp/ip model
Codeforces Round #806 (Div. 4)(7/7)
Internet download manager2022 intelligent win latest version Downloader
Go 语言重要知识点:字符串、UTF-8 编码、rune
debug glance(by quqi99)
PHP实现删除一维数组中某一个值
[Nordic] nrf52810 OTA upgrade (II) – how to use DFU
TCP and UDP, three handshakes and four waves
【STM32】STM32 SDIO SD卡读写测试(四)-- SD_Test之Transfer Mode阶段
总结20220211
PHP开发中csrf攻击的简单演示和防范
Add the GD Library under centos7.5, and then the MySQL expansion library. There is no problem without the configuration of MySQL expansion. There is no MySQL expansion in phpinfo
Laravel 解决【1045】Access denied for user ‘homestead‘@‘localhost‘ (usin g password: YES)
ecshop 需要修改权限的文件夹及文件? 报错目录不可写入