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

Demeter's rule

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Personal original text:

Dimitt's rule (reading e-books on mobile phones)

The fifth of the six principles of design pattern: Demeter's rule.

Brief introduction

Name: Demeter's rule

English name: Law of Demeter

Nickname: the principle of least knowledge

Nickname: Least Knowledge Principle

Values: mom says she doesn't talk to strangers.

Personal introduction:

Each unit should have only limited knowledge about other units: only units "closely" related to the current unit. (each unit can only have limited knowledge about other units: only units that are closely related to the current unit) Each unit should only talk to its friends; don't talk to strangers. (each unit can only talk with its friends: not with strange units.) Only talk to your immediate friends. (only talk to your direct friends)

(from Wikipedia)

Or the mind is inspired to come to a short story. This story is more realistic, in fact, it is not a story, that is, what we often experience. Now the payment for knowledge has become very popular, and it has soared rapidly at the end of 18th. All kinds of training, reading and audio learning software are springing up like bamboo shoots after a spring rain. Let's take an example of reading.

One day, the design pattern teacher explained the Demeter rule, and the students heard it in the clouds. The teacher was afraid that the students had not mastered this knowledge, so they assigned an assignment to the students, which needed to be realized according to the Dimitt rule.

Homework is like this: usually in spare time, like to read some books, usually e-books, now our operation of reading is like this: wake up the phone, open the reading software, select books, and then read. A total of three steps, involving three things: mobile phones, software, books. Students use code to implement this process.

The next day in class, the students handed in their homework one after another, and the teacher casually saw two distinct examples, obviously, one good and one bad. The teacher explained these two examples to the students to give them a taste of Dimitt's rule.

Error example public class LODErrorTest {public static void main (String [] args) {Phone phone = new Phone (); phone.readBook ();}} / * error demonstration * / class Phone {App app = new App (); Book book = new Book ("Design pattern"); public void readBook () {app.read (book) } class App {public void read (Book book) {System.out.println (book.getTitle ());}} class Book {private String title; public Book (String title) {this.title = title;} public String getTitle () {return title;} public void setTitle (String title) {this.title = title;}}

Code:

LODErrorTest

The code has completed the process of reading, and it seems that the function has been realized. If you look closely, you will find that the logic of the code is wrong. What's wrong? Books and application objects are on the mobile phone, the reality is that we wake up the mobile phone, at this time there are no books, only when we open the reading software, there are books to read, without reading software, books can not be read. Therefore, the mobile phone has nothing to do with books, and books should not be in the mobile phone. The normal design is: there is reading software in the mobile phone, and there are books in the reading software, which is in line with the Dimitt rule. By definition: mobile phones and reading software are friends, reading software and books are friends, but friends of friends are not friends, that is, mobile phones and books are not friends, so they should not overlap and should be far away.

Think about reality: what will happen if you lack the step of code review at work? It will dig a pit for future generations, and it is a big pit, because it is not right with the logic in reality, and future generations do not know the business background at that time, so they can only look at the code to be familiar with it, and they will make mistakes step by step, so it is necessary to control the quality of the code step by step. because there are thousands of people in the code, it is impossible to require all the code style to be consistent, but at least the logic needs to be clear and easy to understand.

Correct example public class LODRightTest {public static void main (String [] args) {Phone2 phone2 = new Phone2 (); phone2.readBook ();}} / * correct demonstration * / class Phone2 {private App2 app2 = new App2 (); public void readBook () {app2.read ();}} class App2 {private Book2 book2 = new Book2 ("Design pattern") Public void read () {System.out.println (book2.getTitle ());} class Book2 {private String title; public Book2 (String title) {this.title = title;} public String getTitle () {return title;} public void setTitle (String title) {this.title = title;}}

Code:

LODRightTest

This code is in line with the Dimitt rule, there is reading software in the phone, there are books in the reading software, and there is no shadow of books on the phone. You don't need to elaborate on the correct code, but you can feel it with your heart.

Summary

The main point of view of Demeter's rule is high cohesion and low coupling. I understand it as: if it's yours, don't give it to others; if it's not yours, don't take it. The friend defined above also means the same thing.

At this point, the fifth principle, the six principles only left the last principle of opening and closing, put together six big knives can be ready to do 23 big men. I'm excited to think about it.

I hope the article will be helpful to you, the design pattern series will be constantly updated, interested students can follow the official account, the first time to get articles to push and read, can also communicate together, make friends.

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