The design principle is a summary of some experience guiding our code design , That is to say “ Heart method ”; Object oriented is our “ weapons ”; The design pattern is “ movements in martial arts or traditional opera ”.
Based on mental method , Use weapon moves to deal with complex programming problems .
Come on , Through a small scene in life , Learn this systematically together 6 Big design principles .
Intern cousin went to the company on the first day ...
I : Sister , How did it feel to go to the company on the first day ?
Younger female cousin : I feel that my colleagues are a little cold
I : What's the matter ?
Younger female cousin : When I went through the entry process , My colleague Zhang suddenly walked away with something , I saw another colleague talking about wechat , Just ask him to help me with , But he told me to wait for colleague Zhang to come back .
I : It was like this , First , He's not your direct contact , secondly , Maybe he has something to deal with himself .
This is not very much like our software development “ Dimitar's law ” Well ?
Each module should only know the limited knowledge of the modules closely related to it . Or say , Each module only with their own friends “ speak ”, Not with strangers “ speak ”. Also called “ Minimum knowledge principle ”.
From the description above , We can see that , Demeter's law consists of two parts , If we think of a module as a person , Then to achieve “ One should know the least about others ”, We should do these two things :
1、 Only communicate with direct friends ;
2、 Less understanding of friends .
Next, let's talk in detail about how to do these two points .
First , Let's take a look , What do you mean “ friend ”?
Friends in software development : The coupling relationship between two objects is called “ friend ”, There are usually dependencies 、 relation 、 Aggregation and combination, etc . Direct friends are usually related 、 Aggregation and composition , That is, the two objects are more closely related , Usually in member variables , Method in the form of parameters and return values .
Less understanding of friends
A class is public public The more properties or methods , The larger the scope of modification , The greater the risk spread caused by change .
Just like the previous scene , My cousin only knows that her direct counterpart is responsible for handling the entry procedures for new employees , But the specific operation , Cousin doesn't know .
Let's take a look at the scene of interaction between stars and fans :
1 // Fans 2 public class Fans { 3 4 public void shakeHands(){ 5 System.out.println(" Shake hands with idols "); 6 } 7 public void sign(){ 8 System.out.println(" Idols sign for me "); 9 } 10 public void groupPhoto(){ 11 System.out.println(" Take a picture with an idol "); 12 } 13 14 public void interaction(){ 15 shakeHands(); 16 sign(); 17 groupPhoto(); 18 } 19 } 20 21 // Idols 22 public class Star{ 23 private Fans f; 24 25 ... 26 27 public void interactWithFans(){ 28 // Now idols have to interact with fans , Normally , She just needs to call interaction() The method can , 29 // however , She found that Fans All methods are open , How to interact ? Probably : 30 f.shakeHands(); 31 f.sign(); 32 f.groupPhoto(); 33 34 // Or the following operations 35 f.sign(); 36 f.groupPhoto(); 37 38 // It may also be the following operations 39 f.interaction(); 40 } 41 }
You see , Fans as friends of stars , There are many ways of interaction , Make stars know too much about fans , In this way, stars have to spend a lot of energy thinking about how to interact with fans . that , there f What methods should the object expose ? Obviously , For Stars Star Come on , Just focus on the interaction with fans , And don't care how they interact , therefore , Just expose interaction() Method .
that , The above code should be modified to :
1 // Fans 2 public class Fans { 3 4 private void shakeHands(){ 5 System.out.println(" Shake hands with idols "); 6 } 7 private void sign(){ 8 System.out.println(" Idols sign for me "); 9 } 10 private void groupPhoto(){ 11 System.out.println(" Take a picture with an idol "); 12 } 13 14 public void interaction(){ 15 shakeHands(); 16 sign(); 17 groupPhoto(); 18 } 19 } 20 21 // Idols 22 public class Star{ 23 private Fans f; 24 25 ... 26 27 public void interactWithFans(){ 28 // Now , Idol, she just needs to interact with her fans , Whether it's a handshake 、 Signature or group photo . 29 f.interaction(); 30 } 31 }
You see , At this time, the interaction between stars and fans is much simpler .
informally , A class should know the least about the class it needs to couple or call , I knew you provided so much public Method , I call so many , I don't care about anything else .
Actually , The stars are devoted to art , So many daily affairs are handled by the economic man , Such as fans meeting , Business negotiation with media companies, etc . The agent here is the star's direct friend , Fans and media companies are indirect friends of stars .
Only communicate with direct friends
Now? , We were in the scene just now , Add one more agent human :
1 // Fans 2 public class Fans { 3 4 private void shakeHands(){ 5 System.out.println(" Shake hands with idols "); 6 } 7 private void sign(){ 8 System.out.println(" Idols sign for me "); 9 } 10 private void groupPhoto(){ 11 System.out.println(" Take a picture with an idol "); 12 } 13 14 public void interaction(){ 15 shakeHands(); 16 sign(); 17 groupPhoto(); 18 } 19 } 20 21 // Human agents 22 public class Broker { 23 private Fans f; // Responsible for the fan meeting 24 private Company c; // Responsible for business negotiation 25 ... 26 27 // Hold a fan interaction meeting 28 public void openInteraction() { 29 f.interaction(); 30 } 31 } 32 33 // Idols 34 public class Star{ 35 // The star's agent 36 private Broker b; 37 38 ... 39 40 // Fan interaction meeting , Managed by a broker 41 public void interactWithFans(){ 42 b.openInteraction(); 43 } 44 }
You see , The star has only one direct friend , That is, his agent , No need to know fans and companies . There's a benefit to doing this , It can simplify the communication between objects , Thus reducing dependence , Provide more flexibility , Of course, it can also provide some security .
summary
Between classes that should not have direct dependencies , Don't rely on ;
Between classes with dependencies , Try to rely on only the necessary interfaces ( That is, in the definition “ Limited knowledge ”).
All right. , Don't apply design principles to apply design principles , When we apply design principles , Be sure to analyze specific problems .
Reference resources
《 Big talk design patterns 》
《 Zen of design pattern 》







