Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is Demeter's rule in Java?

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about what Dimitt's rule is in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Define

Law of Demeter (LoD) was proposed by lan holland at Northeastern University in the fall of 1987. It requires that an object should have the least knowledge about other objects, so it is also called the least knowledge principle.

Case requirement

One company, which has various departments, is now required to print out the ID of its employees and the ID of its employees.

Option one

Define the company employee class

/ * * employees * @ author:liyajie * @ createTime:2022/2/8 11:52 * @ version:1.0 * / @ Datapublic class CompanyEmployee {private String id;}

Define company management classes

/ * author:liyajie * @ createTime:2022/2/8 11:51 * @ version:1.0 * / public class CompanyManager {public List getCompanyEmployee () {List list = new ArrayList (); for (int I = 0; I < 5; iTunes +) {CompanyEmployee companyEmployee = new CompanyEmployee (); companyEmployee.setId ("Company employee, id is" + I) List.add (companyEmployee);} return list;}}

Define the department employee class

/ * * Department employees * @ author:liyajie * @ createTime:2022/2/8 11:53 * @ version:1.0 * / @ Datapublic class DempartEmployee {private String id;}

Define department management classes

/ * author:liyajie * @ createTime:2022/2/8 11:52 * @ version:1.0 * / public class DepartmentManager {/ * get department employee information * @ author:liyajie * @ date: 2022-2-8 12:29 * @ param * @ return java.util.List * @ exception: * @ update : * @ updatePerson: * * / public List getDempartEmployee () {List list = new ArrayList () For (int I = 0; I < 5; iTunes +) {DempartEmployee dempartEmployee = new DempartEmployee (); dempartEmployee.setId ("departmental employees, id is + I); list.add (dempartEmployee);} return list;} public void pringAllEmployee (CompanyManager companyManager) {List dempartEmployeeList = getDempartEmployee () For (DempartEmployee dempartEmployee: dempartEmployeeList) {System.out.println (dempartEmployee.getId ());} System.out.println ("- -"); List companyEmployeeList = companyManager.getCompanyEmployee (); for (CompanyEmployee companyEmployee: companyEmployeeList) {System.out.println (companyEmployee.getId ()) }}}

Define test classes

/ * author:liyajie * @ createTime:2022/2/8 11:53 * @ version:1.0 * / public class Test1 {public static void main (String [] args) {new DepartmentManager () .test (new CompanyManager ());}}

View the result

Option 2

There are only two differences between this scheme and plan 1, which are as follows: the modified company management class and the new method of printing the id of company employees.

/ * author:liyajie * @ createTime:2022/2/8 11:51 * @ version:1.0 * / public class CompanyManagerNew {/ * query company employees * @ author:liyajie * @ date: 12:42 on 2022-2-8 * @ param * @ return java.util.List * @ exception: * @ update: * @ updatePerson: * * / public List getCompanyEmployee () {List list = new ArrayList () For (int I = 0; I < 5; iTunes +) {CompanyEmployee companyEmployee = new CompanyEmployee (); companyEmployee.setId ("company employee, id is" + I); list.add (companyEmployee);} return list Id * @ author: liyajie * @ date: 12:41 on 2022-2-8 * @ param * @ return void * @ exception: * @ update: * @ updatePerson: * / public void printCompanyEmployee () {List list = getCompanyEmployee () For (CompanyEmployee companyEmployee: list) {System.out.println (companyEmployee.getId ());}}

Modified department management class

/ * author:liyajie * @ createTime:2022/2/8 11:52 * @ version:1.0 * / public class DepartmentManagerNew {/ * get department employee information * @ author:liyajie * @ date: 2022-2-8 12:29 * @ param * @ return java.util.List * @ exception: * @ update : * @ updatePerson: * * / public List getDempartEmployee () {List list = new ArrayList () For (int I = 0; I < 5; iTunes +) {DempartEmployee dempartEmployee = new DempartEmployee (); dempartEmployee.setId ("departmental employees, id is + I); list.add (dempartEmployee);} return list;} public void pringAllEmployee (CompanyManagerNew companyManagerNew) {List dempartEmployeeList = getDempartEmployee () For (DempartEmployee dempartEmployee: dempartEmployeeList) {System.out.println (dempartEmployee.getId ());} System.out.println ("-"); companyManagerNew.printCompanyEmployee ();}}

Test class

/ * author:liyajie * @ createTime:2022/2/8 11:53 * @ version:1.0 * / public class Test2 {public static void main (String [] args) {new DepartmentManagerNew () .test (new CompanyManagerNew ());}}

Result

Comparison and analysis

In the first scheme, the company employee id is printed in the method pringAllEmployee of the department management class DepartmentManager, and the class CompanyEmployee is used, which is neither the internal property of DepartmentManager nor the input and return parameters of the method, which violates the Demeter principle.

The second plan is to add a method to print the company employee id in the company management class, and provide it to the outside world, which is called in the method pringAllEmployee of the department management class DepartmentManager, which accords with the Demeter principle.

Thank you for reading! This is the end of the article on "what is the Dimitt Rule in Java". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report