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

How to realize the Java Dimitt principle

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces the relevant knowledge of "how to realize the Java Dimit principle". The editor shows you the operation process through the actual case, the operation method is simple and fast, and the practicality is strong. I hope this article "how to realize the Java Dimitt principle" can help you solve the problem.

Demeter's principle

The Law of Demeter principle, also known as the least known principle (Least Knowledge Principle), can be simply said: talk only to your immediate friends, only communicate with your direct friends, do not talk to "strangers".

Mode and meaning

According to Dimitt's law, if two classes do not have to go directly to each other, then the two classes should not interact directly. If one of the classes needs to call another tired method, the call can be forwarded through a third party.

The first premise of Demeter's rule is that in the structural design of a class, each class should reduce the access rights of its members, that is, a class wraps its own private state and does not disclose fields or behaviors that do not need to be known to other classes.

Object-oriented design principles and the three major features of object-oriented are not contradictory at all. The fundamental idea of Dimitt's rule is to emphasize the loose coupling between classes. The weaker the coupling between classes, the more conducive to reuse, a class in weak coupling is modified, will not cause a fight to related classes, that is to say, the hiding of information promotes the reuse of software.

Advantages

Reduce coupling between objects

Be careful

In the division of classes, weakly coupled classes should be created. The weaker the coupling between classes, the more conducive it is to achieving the goal of reusability.

In the structural design of the class, the access rights of the class members should be reduced as much as possible.

In the design of a class, priority is given to setting a class as an immutable class.

Minimize the number of references to other objects on references to other classes.

Instead of exposing the property members of the class, you should provide the corresponding accessors (set and get methods).

Use serialization (Serializable) functionality with caution

Give an example

An example of the relationship between stars and brokers

Analysis: because stars devote themselves to art, many daily affairs are handled by brokers, such as meetings with fans, business negotiations with media companies, etc. The agents here are friends of stars, while fans and media companies are strangers, so it is suitable to use Dimitt's rule.

Public class LoDtest

{

Public static void main (String [] args)

{

Agent agent=new Agent ()

Agent.setStar (new Star (Kobe))

Agent.setFans (new Fans (leap soil))

Agent.setCompany (new Company (China Media Co., Ltd.)

Agent.meeting ()

Agent.business ()

}

}

/ / broker

Class Agent

{

Private Star myStar

Private Fans myFans

Private Company myCompany

Public void setStar (Star myStar)

{

This.myStar=myStar

}

Public void setFans (Fans myFans)

{

This.myFans=myFans

}

Public void setCompany (Company myCompany)

{

This.myCompany=myCompany

}

Public void meeting ()

{

System.out.println (myFans.getName () + "meet the star" + myStar.getName () + ".")

}

Public void business ()

{

System.out.println (myCompany.getName () + "and star" + myStar.getName () + "negotiate light business.")

}

}

/ / Star

Class Star

{

Private String name

Star (String name)

{

This.name=name

}

Public String getName ()

{

Return name

}

}

/ / fans

Class Fans

{

Private String name

Fans (String name)

{

This.name=name

}

Public String getName ()

{

Return name

}

}

/ / Media Company

Class Company

{

Private String name

Company (String name)

{

This.name=name

}

Public String getName ()

{

Return name

}

}

This is the end of the introduction to "how to realize the Java Dimitt principle". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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

Internet Technology

Wechat

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

12
Report