当前位置:网站首页>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.
边栏推荐
- 剑指 Offer II 038. 每日温度
- Invalid bound statement (not found): com. example. mapper. TblUserRecordMapper. login
- How to implement cross domain requests
- Dimension and format of data
- 云原生——上云必读之SSH篇(常用于远程登录云服务器)
- Install pytoch geometric
- ES6 模块化
- ES6 modularization
- InputStream/OutputStream(文件的输入输出)
- AWT常用组件、FileDialog文件选择框
猜你喜欢
Sword finger offer II 038 Daily temperature
AWT common components, FileDialog file selection box
Leakage detection relay jy82-2p
buuctf-pwn write-ups (8)
509. 斐波那契数、爬楼梯所有路径、爬楼梯最小花费
Matlab remainder
js arguments参数使用和详解
My NVIDIA developer journey - optimizing graphics card performance
Experience weekly report no. 102 (July 4, 2022)
C language exercises (recursion)
随机推荐
Leakage detection relay jy82-2p
树形dp
A little understanding of GSLB (global server load balance) technology
Nexus 6p从8.0降级6.0+root
【无标题】
Abap:ooalv realizes the function of adding, deleting, modifying and checking
SQL injection SQL lab 11~22
[untitled]
How to determine whether an array contains an element
C实现贪吃蛇小游戏
Yiwen unlocks Huawei's new cloud skills - the whole process of aiot development [device access - ESP end-to-side data collection [mqtt]- real time data analysis] (step-by-step screenshot is more detai
Error CVC complex type 2.4. a: Invalid content beginning with element 'base extension' was found. Should start with one of '{layoutlib}'.
如何获取el-tree中所有节点的父节点
2022.7.2-----leetcode.871
The width of the picture in rich text used by wechat applet exceeds the problem
剑指 Offer II 038. 每日温度
AWT introduction
Inputstream/outputstream (input and output of file)
Fast power (template)
LayoutManager布局管理器:FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout、BoxLayout