[Zen Reading Notes of design patterns] 005 _ six principles of design patterns (5): The dimit Principle

Source: Internet
Author: User

Preface

I remember a year ago, when I was looking at the design model, I was so confused. I didn't think about every concept, but I knew what the design pattern could do. Today, we can see that the [dimit principle] is inexplicable. You can call it the interface isolation principle. I can use one word to parse it. You can call it a single responsibility, I can see what the name is. What kind of principle do you have to call? It's confusing. It's similar to the lee's replacement principle, but there's at least two replicas for the Lee's replacement. Today, let's take a look at what this principle is.

Body

Demeter: the daughter of the god of the sky and the goddess of time. It is the goddess of abundance, agriculture and forestry. The god of the sky and the god of time also has a son of Zeus (the third generation God) and a son of haddes (the god of the underworld. This Zeus is married to dumit, that is, his second sister (close friend !), They gave birth to a daughter called Persephone. This Persephone was hijacked by her uncle hardes. Her uncle wants her to become a close friend !). Then our dimeter was heartbroken, and she went to the earth to find her daughter. I forgot to give life to the earth. The gods of Olympus are not happy. The Earth cannot do this, So I forced the god of the underworld to release Persephone. In fact, only the great gods can join in to speak to hudes, the Pluto, because hudes is the brother of Zeus and dimit. He is one of the four major creation gods (four major creation gods: hudes, posedo, Zeus, Hula, unfortunately, there is no nvwa ). Here, when the world was divided, the four creative gods decided by drawing lots. The most powerful Hades drew to the underworld, but he was not a god of death, in addition to robbing his niece to become a wife, he is doing well.

Well, it's too much. I have time to take a closer look at the mythical history of the West-The homic epic, and then write a blog to introduce it. Here, our Demeter is just a mother looking for her daughter. It does not have any special meaning. What does the Demeter principle mean?

An object should have a minimum understanding of other objects.

Maybe this sentence alone cannot be understood! After all, it's just one sentence. You understand it. Yes, you only know the meaning behind it. Let's take a closer look!

1. the fundamental intention of the Demeter law-decoupling

During the Second World War, the German god of war, the desert fox Rommel, was on vacation at home for a while. At this time, he was attacked by a British Uncle, Montgomery, who had worked hard in North Africa. Our head of State, Hitler, found Rommel and said to him, "Long, you have brought tens of thousands of soldiers to the death of Montgomery!

Now we have three classes. Let's look at the code. Here we will first remind you,There is a problem with class Hitler..

1 class Hitler {2 public: 3 void killmeng (Rommel L) {4 vector <German soldiers> list; // here our Heads of State are not idle in dealing with small soldiers, but we can see these soldiers in Hitler's world 5 for (INT I = 0; I <100000; I ++) {6 list. add (New German soldier (); 7} 8 L. gotokillmeng (list); 9} 10}; 11 12 class roumel {13 public: 14 void gotokillmeng (vector <German soldier> List) {15 for (INT I = 0; I <100000; I ++) {16 list. get (I ). gotonorthafrica (); 17} 18} 19}; 20 21 class German soldier {22 public: 23 void gotonorthafrica () {24 cout <"I am going to North Africa. "<Endl; 25} 26 };

From the above comments, we can see that our Head of State can directly operate on the command Rommel, and we have not done it myself! Here, our heads of State are coupled with small soldiers. In fact, there is no need to couple them together. If we want to kill the code about German soldiers in the class of head of State Hitler, who will be directly associated with these soldiers? Of course it is our Rommel, so the code of these soldiers should be put into the class of Rommel. The modified code is as follows:

1 class Hitler {2 public: 3 void killmeng (Rommel L) {// you can directly command Rommel to get 4 5 L. gotokillmeng (); 6} 7}; 8 9 class roymel {10 public: 11 void gotokillmeng () {12 vector <German soldier> list; 13 for (INT I = 0; I <100000; I ++) {// Our longmmel team up 14 lists. add (New German soldier (); 15} 16 for (INT I = 0; I <100000; I ++) {17 list. get (I ). gotonorthafrica (); 18} 19} 20}; 21 22 class German soldier {23 public: 24 void gotonorthafrica () {25 cout <"I am going to North Africa. "<Endl; 26} 27 };

2. Attention to the Demeter law (1) -- close association between classes should not be too close, and cohesion should be improved

This is easy to understand. I will not give an example of the Code. It is too late to go to bed. For example, although our heads of State can direct Rommel, the great god of Rommel also has his own private space! The Head of State must not be involved in the private life of others. How can we control the Head of State's involvement in Rommel's private life? We use our private keywords. In this way, we don't want to hide the methods used by the Head of State.

3. Attention to the dimit Law (2) -- Be Your Own

What does this mean? For example, if there is a method that can be put into Class A or Class B, what should we do? In fact, you don't have to worry about where to put it. The dimit rule gives us a solution:

If a method is put in this class, that is, the relationship between classes is not added, and there is no negative impact on this class, it is placed in the class.

4. Attention to the dimit rule (iii) -- use serialization with caution

Serialization is used to sequence object memory data so that it can be transmitted through different media. However, if our software system model is c/s and the client object class changes, the serialization will fail. If this happens, the entire software system fails. So pay attention to it. The author does not want us to use it, but to use it with caution. In fact, this has little to do with dumit.

Summary

The dimit law is easy to understand. Yes, it is very simple. But what kind of degree should we grasp in implementation? We need to make a summary of the project. In a word, the Design Pattern provides you with principles and patterns, but you cannot use them. Sometimes they will be too late.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.