Introduction
I think coding has a soul, just like everyone has a belief.So how to manifest belief and how to highlight the soul depends on its inherent principles.Recently, I have learned the six principles of design mode, and have some insights, so I want to make a summary and record.In this paper, the Dimitar Principle (LoD) is described in detail, and the specifications it defines are illustrated with examples, which are expected to be helpful.The blog s I've sorted out so far appear very frequently for interviews.You can click on the link: http://blog.csdn.net/u012403290
Technical Points
1. High cohesion and low coupling
This is the rule that people all pursue in software development. Generally speaking, high cohesion means that each part of a class is very close to each other; low coupling means that between classes and classes, modules and modules have a high degree of independence, the less the relationship between them, the better, how to consider packaging a classEven one module is the core of high cohesion and low coupling.
2. The concept of friends
In the Dimitt principle, the concept of "friend" is used to determine whether a class you write meets that principle.In a class, its member variables, preconditions and postconditions of methods (mentioned in the previous blog post) are its "direct friends". Here's a list:
package com.brickworkers;
/**
*
* @author Brickworker
* Date:2017 April 6, 2000, 1:52:07 p.m.
* Description of Class China.java: Dimitt Principle Chinese side
* Copyright (c) 2017, brcikworker All Rights Reserved.
*/
public class China {
private India india; //India is a friend of China
//China demands South Korea to stop building Sadd
public void forceCommend(Korea korea){ //Korea is also a friend of China
THAAD thaad = THAAD.getThaad();//China says: I want to stop the deployment of Sad
korea.stopThaad(thaad); //Korea said: Okay, this Sadd I won't build
}
}
In the appeal code, Chinese friends have India and South Korea.Only member variables, method input and return are the most direct friends of this class.
3. Friend Classes
Refer to the code for the second point, where China and South Korea are direct friends, but China and Sad are not.If China wants to contact Sadd, it needs to contact Sadd through Korea. You can't just disassemble Sadd.South Korea, on the other hand, acted as a friend at that time.
2. Dimitt Principle
Also known as the Least Know Principle (LKP), it refers to the relationship between classes. We define it as a "friend" and try not to involve classes other than direct friends.In the blogger's understanding (just personal understanding), Dimitar's principle is mainly divided into two parts, one part is self-strengthening, turning itself into a highly cohesive, strictly controlling its own public attributes, with the feeling of "half-shaded pipa". The second part is to achieve low coupling between itself and the surrounding classes."Don't talk to strangers."
An example of Dimitt
I have devised an example that reflects Dimit's principle: South Korea wants to build Sad, and China strongly condemns and suppresses it.So there are at least three classes, the Chinese class, which requires South Korea to stop the implementation of Sad; the Korean class, which can start and stop the implementation of Sad; and the Sad class, which indicates the construction of Sad.See the examples below.
A counterexample
The Chinese side requires South Korea to stop the deployment of Sadd:
package com.brickworkers;
/**
*
* @author Brickworker
* Date:2017 April 6, 2000, 1:52:07 p.m.
* Description of Class China.java: Dimitt Principle Chinese side
* Copyright (c) 2017, brcikworker All Rights Reserved.
*/
public class China {
private India india; //India is a friend of China
//China demands South Korea to stop building Sadd
public void forceCommend(Korea korea){ //Korea is also a friend of China
THAAD thaad = THAAD.getThaad();//I want to stop deploying this Sad
korea.stopThaad(thaad); //Korea said: Okay, this Sadd I won't build
}
}
The Korean side can control Sadd's starting and stopping deployments:
package com.brickworkers;
/**
*
* @author Brickworker
* Date:2017 April 6, 2000, 1:53:47 p.m.
* Description of Class Korea.java: Dimitt Principle Korea
* Copyright (c) 2017, brcikworker All Rights Reserved.
*/
public class Korea {
//Start construction in Sad
public void startThaad(THAAD thaad){
thaad.setAcreage(10000);
thaad.setArea("Seoul, South Kerean");
thaad.setHeight(1000);
}
//Stop building Sad
public void stopThaad(THAAD thaad){
thaad.setAcreage(0);
thaad.setArea("");
thaad.setHeight(0);
}
}
Sadds, indicating the construction of Sadds:
package com.brickworkers;
/**
*
* @author Brickworker
* Date:2017 April 6, 2000, 1:54:46 p.m.
* Description of class THAAD.java: Dimit principle Sade
* Copyright (c) 2017, brcikworker All Rights Reserved.
*/
public class THAAD {
private static THAAD thaad = null;
private String area;//Location of construction
private int height;//Height of construction
private int acreage;//Area built
private THAAD() { //Sade is unique and does not allow initialization with constructors
}
public static THAAD getThaad(){ //Simple singleton, not multi-threaded; just a simple introduction
if(thaad != null)
return thaad;
else{
return new THAAD();
}
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getAcreage() {
return acreage;
}
public void setAcreage(int acreage) {
this.acreage = acreage;
}
}
Okay, let's start to analyze the Chinese cube. First of all, in the Chinese cube, its "direct friend" is India (in this case, an outsider, which is not helpful) and Korea, which has nothing to do with the Sade class.But in the mandatory approach, China says I want to stop this Sadd.It's not really necessary. Instead, it increases the coupling between classes, and Sadd is the one who doesn't stop which one (not because it violates Dimitt's principle)?So China just needs to tell South Korea, "You give me a stop for Sadd deployment."In fact, South Korea already knows what to do.So next, let's modify it like this:
Examples after modification
Chinese square class, removing the coupling with Sade:
package com.brickworkers;
/**
*
* @author Brickworker
* Date:2017 April 6, 2000, 1:52:07 p.m.
* Description of Class China.java: Dimitt Principle Chinese side
* Copyright (c) 2017, brcikworker All Rights Reserved.
*/
public class China {
private India india; //India is a friend of China
//China demands South Korea to stop building Sadd
public void forceCommend(Korea korea){ //Korea is also a friend of China
korea.stopThaad(); //Korea said: Okay, this Sadd I won't build
}
}
Korean side class, start and stop methods to make some modifications:
package com.brickworkers;
/**
*
* @author Brickworker
* Date:2017 April 6, 2000, 1:53:47 p.m.
* Description of Class Korea.java: Dimitt Principle Korea
* Copyright (c) 2017, brcikworker All Rights Reserved.
*/
public class Korea {
//Start construction in Sad
public void startThaad(){
THAAD thaad = THAAD.getThaad();
thaad.setAcreage(10000);
thaad.setArea("Seoul, South Kerean");
thaad.setHeight(1000);
}
//Stop building Sad
public void stopThaad(){
THAAD thaad = THAAD.getThaad();
thaad.setAcreage(0);
thaad.setArea("");
thaad.setHeight(0);
}
}
Sade class does not need to change, so it will not be a lot of harmony.First, in the course of negotiations between China and South Korea, there is no need to explain Sadd at all. Just tell South Korea that you stop this thing, and then South Korea will execute it.China also does not need to talk to Sadds, which conforms to Dimitt's principle.
How to improve class cohesion
Previously, I also mentioned that, according to my own understanding, in fact, the Dimitar principle also has the feeling of "being alone" (why do I understand it so well and refer to a lot of data, several of which refer to the design of the class itself), that is, to strengthen the cohesion of the class itself, how to strengthen the cohesion of the class, possiblyYou don't have any concepts. Let me give you another example:
Continuing with the example above, there are three steps for South Korea to stop Sadd: 1. Government discussion decision 2. Determine start-up units 3. Remove the original deployment, then the code shows that:
package com.brickworkers;
/**
*
* @author Brickworker
* Date:2017 April 6, 2000, 1:53:47 p.m.
* Description of Class Korea.java: Dimitt Principle Korea
* Copyright (c) 2017, brcikworker All Rights Reserved.
*/
public class Korea {
//Discuss and determine
public void discuss(){
//Government discussion
}
//Designate implementation units
public void appoint(){
//Specify construction units
}
//Start Removal
public void remove(){
//Remove original deployment
}
}
Then, when China orders it to stop, South Korea needs to do so:
package com.brickworkers;
/**
*
* @author Brickworker
* Date:2017 April 6, 2000, 1:52:07 p.m.
* Description of Class China.java: Dimitt Principle Chinese side
* Copyright (c) 2017, brcikworker All Rights Reserved.
*/
public class China {
//China demands South Korea to stop building Sadd
public void forceCommend(Korea korea){
korea.discuss();
korea.appoint();
korea.remove();
}
}
This is the general logic, does it seem a bit cumbersome and redundant?Moreover, for the Korean design, too many underlying methods have been exposed, so for the Korean class, what is the feeling of "half-masked pipa"?So, if we use private s to package the methods in the Korean class, is it much better to provide an integrated method?See the following modified Korean code:
package com.brickworkers;
/**
*
* @author Brickworker
* Date:2017 April 6, 2000, 1:53:47 p.m.
* Description of Class Korea.java: Dimitt Principle Korea
* Copyright (c) 2017, brcikworker All Rights Reserved.
*/
public class Korea {
//Discuss and determine
private void discuss(){//Set to private
//Government discussion
}
//Designate implementation units
private void appoint(){//Set to private
//Specify construction units
}
//Start Removal
private void remove(){//Set to private
//Remove original deployment
}
public void removeThaad(){//Pack a separate one for the outside
this.discuss();
this.appoint();
this.remove();
}
}
Then, when China orders the Korean side, it only needs to issue one directive:
package com.brickworkers;
/**
*
* @author Brickworker
* Date:2017 April 6, 2000, 1:52:07 p.m.
* Description of Class China.java: Dimitt Principle Chinese side
* Copyright (c) 2017, brcikworker All Rights Reserved.
*/
public class China {
//China demands South Korea to stop building Sadd
public void forceCommend(Korea korea){
korea.removeThaad();//Issue only one instruction
}
}
So this part of the code above illustrates the improvement of aggregation within Korea n classes, the lowest possible encapsulation method, and the least exposure to other public classes.
Okay, basically I've shown you what I understand about Dimiter's principle here. Everyone's understanding and opinions will be different. Everyone needs to understand it.However, as I wrote in my previous blog post, excessive adherence to rules can be a sign of "sticking to conventions".Imagine that if you follow the Dimitar principle very closely, and you have a lot of friends, your projects will become very complex, and the specifications are specifications, depending on the situation.
If you have any questions, you can contact me: