当前位置:网站首页>11. Dimitt's law
11. Dimitt's law
2022-07-04 06:21:00 【Muzi's day and night】
package dmitryRule;
import java.util.ArrayList;
import java.util.List;
/**
* Dimitar's law
* Origin of the problem : The closer a class is to a class , The greater the coupling , When a class changes , The greater the impact on the other class .
*
* Baidu Encyclopedia :
* Dimitar's law (Law of Demeter) It's also called the least known principle (Least Knowledge Principle Abbreviation LKP),
* That is, an object should know as little as possible about other objects , Don't talk to strangers . The English abbreviation is : LoD.
*
* Mode and meaning
* △ Demeter's law can be simply described as :talk only to your immediate friends. about OOD Come on ,
* It is also interpreted in the following ways : A software entity should interact with as few other entities as possible . Every software unit has the least knowledge of other units ,
* And limited to those software units closely related to the unit .
* △ The original purpose of Demeter's law is to reduce coupling between classes . Each class minimizes its dependence on other classes ,
* therefore , It is easy to make the functional modules of the system functionally independent , They don't exist ( Or a few ) Dependency relationship .
* △ Dimitri's law doesn't want a direct connection between classes . If there's a real need to connect , Also hope to convey through its friend class .
* therefore , One of the possible consequences of applying Dimiter's law is : There are a lot of mediation classes in the system ,
* The reason why these classes exist is to transfer the calling relationship between classes —— This increases the complexity of the system to a certain extent .
*
* Next, I design a company to pay The leader of the head office sends The leader of the branch company sends... To the staff of the branch company But the head office does not directly send Because they don't work together
* If you say transfer can I would say : Cash salary !!! Just an example Don't take the scene too seriously
*
*
*@author LiMing E-mail:[email protected]
*@date 2017 year 6 month 14 Japan Afternoon 4:02:20
*/
public class DmitryRule {
public static void main(String[] args) {
// The previous way of implementation What's wrong ?
// The head office leaders have directly contacted the employees of the branch companies !! Didn't find ?
/*
* List<Emp> emps2 = branchOfficeLeader.getEmps();
* for (Emp e : emps2) {
* System.out.println(e);
* }
*/
HeadOfficeLeader headOfficeLeader = new HeadOfficeLeader();
BranchOfficeLeader branchOfficeLeader = new BranchOfficeLeader();
headOfficeLeader.payOff(branchOfficeLeader);
/**
* The right way to open it is like this
*
*
* HeadOfficeLeader2 His payoff There is no direct contact with branch staff in the method Instead, let the branch leaders directly contact the branch employees
* This is the right way to open The head office leaders avoided direct contact with the employees of the branch companies It's through an intermediary ( Branch leader ) Come and get in touch
*
*/
HeadOfficeLeader2 headOfficeLeader2 = new HeadOfficeLeader2();
BranchOfficeLeader branchOfficeLeader2 = new BranchOfficeLeader();
headOfficeLeader2.payOff(branchOfficeLeader2);
}
}
/*
* Head office leaders
*/
class HeadOfficeLeader{
List<Emp> emps = new ArrayList<Emp>();
public List<Emp> getEmps(){
// The head office has 200 staff
for (int i = 0; i <200; i++) {
Emp emp = new Emp();
emp.setNo(" The head office _"+i);
emp.setName(" Liu "+i);
emp.setSalary(5600+i);
emps.add(emp);
}
return emps;
}
public void payOff(BranchOfficeLeader branchOfficeLeader){
List<Emp> emps = getEmps();
for (Emp e : emps) {
System.out.println(e);
}
List<Emp> emps2 = branchOfficeLeader.getEmps();
for (Emp e : emps2) {
System.out.println(e);
}
}
}
/*
* Head office leaders 666
*/
class HeadOfficeLeader2{
List<Emp> emps = new ArrayList<Emp>();
public List<Emp> getEmps(){
// The head office has 200 staff
for (int i = 0; i <200; i++) {
Emp emp = new Emp();
emp.setNo(" The head office _"+i);
emp.setName(" Liu "+i);
emp.setSalary(5600+i);
emps.add(emp);
}
return emps;
}
public void payOff(BranchOfficeLeader branchOfficeLeader){
List<Emp> emps = getEmps();
for (Emp e : emps) {
System.out.println(e);
}
// The leaders of the branch company pay the employees directly
branchOfficeLeader.payOff();
}
}
/*
* Branch leader
*/
class BranchOfficeLeader{
List<Emp> emps = new ArrayList<Emp>();
public List<Emp> getEmps(){
// The head office has 200 staff
for (int i = 0; i <200; i++) {
Emp emp = new Emp();
emp.setNo(" branch office _"+i);
emp.setName(" Li "+i);
emp.setSalary(8000+i);
emps.add(emp);
}
return emps;
}
public void payOff(){
List<Emp> emps = getEmps();
for (Emp e : emps) {
System.out.println(e);
}
}
}
/*
* The employee class Whether it's head office or branch office Employees are employees A common class id With branches _xxx The head office _xxx To express
*/
class Emp{
private String no;
private String name;
private double salary;
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return " Number :"+this.no +" full name :"+this.name+" salary :"+this.salary;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
边栏推荐
- Bicolor case
- Inputstream/outputstream (input and output of file)
- 27-31. Dependency transitivity, principle
- How does apscheduler set tasks not to be concurrent (that is, execute the next task after the first one)?
- QT get random color value and set label background color code
- Sword finger offer II 038 Daily temperature
- Considerations for testing a website
- JS how to convert seconds into hours, minutes and seconds display
- Invalid revision: 3.18.1-g262b901-dirty
- 微信小程序使用rich-text中图片宽度超出问题
猜你喜欢
JS arguments parameter usage and explanation
js arguments参数使用和详解
Sword finger offer II 038 Daily temperature
Win10 clear quick access - leave no trace
Arcpy 利用updatelayer函数改变图层的符号系统
Error CVC complex type 2.4. a: Invalid content beginning with element 'base extension' was found. Should start with one of '{layoutlib}'.
AWT常用组件、FileDialog文件选择框
ES6 modularization
ABAP:OOALV实现增删改查功能
C language exercises (recursion)
随机推荐
Design and implementation of redis 7.0 multi part AOF
SQL join, left join, right join usage
gslb(global server load balance)技术的一点理解
注释与注解
JSON web token -- comparison between JWT and traditional session login authentication
微信小程序使用rich-text中图片宽度超出问题
Invalid bound statement (not found): com. example. mapper. TblUserRecordMapper. login
Functions in C language (detailed explanation)
R统计绘图-随机森林分类分析及物种丰度差异检验组合图
树形dp
webrtc 快速搭建 视频通话 视频会议
APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
QT 获取随机颜色值设置label背景色 代码
buuctf-pwn write-ups (8)
How to use multithreading to export excel under massive data? Source code attached!
Gridview出现滚动条,组件冲突,如何解决
How to avoid JVM memory leakage?
ABAP:OOALV实现增删改查功能
如何避免 JVM 内存泄漏?
2022.7.2-----leetcode.871