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 the Dimitt principle in Java?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you what the Dummett principle in Java is, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. What is the Dimitt principle?

An object should have the least understanding of other objects.

The closer the relationship between class and class is, the greater the degree of coupling is.

The Demeter Principle rule is also called the least known principle, that is, the less a class knows about the class it depends on, the better. In other words, no matter how complex the dependent class is, try to encapsulate the logic inside the class. No information is disclosed except for the public method provided.

Demeter's rule has a simpler definition: communicate only with direct friends.

Direct friends: each object will have a coupling relationship with other objects, as long as there is a coupling relationship between two objects, we will say that the two objects are friends. There are many ways to couple, such as dependence, association, combination, aggregation and so on. Among them, we say that the classes in the member variables, method parameters, and method return values are direct friends, while the classes that appear in local variables are not direct friends. In other words, it is best not to appear inside the class in the form of local variables.

two。 A code case of violating the Demeter principle

I have written very detailed comments in the code. If you have any questions, you can point them out.

Package com.szh.principle.demeter; import java.util.ArrayList;import java.util.List; / * * * / / class Employee {private String id; public void setId (String id) {this.id = id;} public String getId () {return id;}} / / College employee class class CollegeEmployee {private String id Public void setId (String id) {this.id = id;} public String getId () {return id;}} / / Management class class CollegeManager {/ / returns all employees of the school public List getAllEmployee () {List list = new ArrayList (); for (int I = 1; I)

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