Design Patterns - Seven Principles of the Law of Demeter (f)

The definition of Demeter

  Demeter (Law of Demeter, LoD) also called the principle of least knowledge (Least Knowledge Principle, LKP), produced in 1987, Northeastern University (Northeastern University) in the United States called Demeter (Demeter) research project, Ian put forward by the Netherlands (Ian Holland), was Butch (Booch) the popularity of one of the founders of UML, and later because of the classic "the Pragmatic programmer" (the Pragmatic programmer) mentioned and well known.

  The definition of the Law of Demeter is: only talk directly with your friends, not with "strangers" talk (Talk only to your immediate friends and not to strangers). The implication is: if two software entities without direct communication, then there should be a direct call each occurrence, the call can be forwarded through a third party. The aim is to reduce the coupling between classes, increasing the relative independence of modules.

  Demeter in the "friend" means: the current object itself, members of the current object, the current object of the created object, method parameters, such as the current object, the presence of these objects associated with the current object, aggregation or composition relationships can direct way to access these objects.

Demeter advantage of

  Demeter requirement limits the width and depth of communication between software entities, Demeter proper use will have the following two advantages.

    1. Reducing the coupling between the classes, it increases the relative independence of the module.
    2. Since the pro-reduction degree, thereby improving the scalability of the system and the multiplexing rate classes.


  However, excessive use of the system will make a large number of Demeter intermediary class, thereby increasing the complexity of the system, so reducing the efficiency of communication between modules. Therefore, when the need to weigh preclude the use of Demeter, ensuring high cohesion and low coupling at the same time, to ensure a clear structure of the system.

Implementation of the Law of Demeter

  It is seen from the definition and characteristics of Demeter, which emphasizes the following points:

    1. From the perspective of dependent persons, it should rely only dependent objects.
    2. From the perspective of those who are dependent on that method should be exposed exposure only.


  Therefore, in the use of Demeter to note the following 6:00.

    1. On the division of the class, you should create weakly coupled classes. Coupling between the class and class weaker, the more conducive to achieving the objectives reusable.
    2. In the structural design class, try to reduce access to class members.
    3. In the design category, priority will be a class set to the same class.
    4. In reference to other classes, the number of references to other objects to a minimum.
    5. Class attribute member is not exposed, but has a corresponding access unit (set and get methods).
    6. Use caution Serialization (Serializable) function.


Examples of the relationship between [Example 1] stars and brokers.

  Analysis: As the star threw himself into art, so many brokers responsible for handling daily affairs, such as the fans will be met, with the media company's business negotiation and so on. Here is the star broker friends, fans, and the media is a stranger, it is suitable to use Demeter, which class diagram shown in Fig.

           Stars and broker relationship diagram
                          1 star with the broker relationship diagram


Code is as follows:

  1. package principle;
  2. public class LoDtest{
  3. public static void main(String[] args){
  4.   Agent agent=new Agent();
  5.   Agent . setStar ( new new Star ( "Ruby" ));
  6.   Agent . setFans ( new new Fans ( "Fan Cheng Han" ));
  7.   Agent . setCompany ( new new Company ( "China Media Co., Ltd." ));
  8.   agent.meeting();
  9.   agent.business();
  10.   }
  11. }
  12.   //broker
  13. class Agent {
  14.   private Star myStar;
  15.   private Fans myFans;
  16.   private Company myCompany;
  17.   public void setStar(Star myStar){
  18.     this .myStar = Myst ;
  19.   }
  20.   public void setFans(Fans myFans){
  21.     this.myFans=myFans;
  22.   }
  23.   public void setCompany(Company myCompany){
  24.     this.myCompany=myCompany;
  25.   }
  26.   public void meeting(){
  27.     System the .out . Println (myFans . GetName () + "with the Stars" + mystar . GetName () + "met." );
  28.   }
  29.   public void business(){
  30.     System the .out . Println (myCompany . GetName () + "with the Stars" + mystar . GetName () + "business negotiation." );
  31.   }
  32. }
  33.   //Celebrity
  34. class Star{
  35.   private String name;
  36.   Star(String name){
  37.     this.name=name;
  38.   }
  39.   public String getName(){
  40.     return name;
  41.   }
  42. }
  43.   // Fans
  44. class Fans{
  45.   private String name;
  46.   Fans(String name){
  47.     this.name=name;
  48.   }
  49.   public StringgetName() {
  50.     return name;
  51.   }
  52. }
  53.   // media companies
  54. class Company{
  55.   private String name;
  56.   Company(String name){
  57.     this.name=name;
  58.   }
  59.   public String getName(){
  60.     return name;
  61.   }
  62. }


  Operating results of the program are as follows:

Han Cheng Fan and star Ruby met. 
China Media Limited and Star Ruby business negotiation.

Guess you like

Origin www.cnblogs.com/yuexiaoyun/p/11825635.html