Dimitri Law--seven object-oriented design principles (6)

Source: Internet
Author: User

Source of the Dimitri Law:

The Dimitri rule is also known as the least known principle, which was first proposed by Ian Holland of the American Northeastern University in 1987. The closer the relationship between classes and classes, the greater the coupling, and the greater the impact on another class when one class changes. So he put forward the Dimitri law. In layman's terms, it is a class that knows as little as possible about the classes it relies on. That is to say, for a dependent class, no matter how complex the logic, as far as possible to encapsulate the logic inside the class, outside the public method provided, not to disclose any information.

A simple definition of the Dimitri Law:

communicate only with a direct friend.

Let's start by explaining what a direct friend is: Each object has a coupling relationship with other objects, so long as there is a coupling between the two objects, we say that the two objects are friends. There are many ways of coupling, dependence, association, composition, aggregation and so on. Where the current object itself (this), the member variable, the passed in as a parameter to the object in the current object method, the class in the method return value, the object created by the current object is a direct friend.

The role of the Dimitri Law:

The Dimitri law can reduce the coupling degree of the system and keep the coupling relationship between classes and classes low. We know that software programming has a general principle: low coupling, high cohesion. Whether it is process-oriented programming or object-oriented programming, it is possible to increase the code reuse rate by minimizing the coupling between the modules. The advantages of low coupling are self-evident, but how do you program to do low-coupling? That's exactly what the Dimitri law is going to do.

Example of the Dimitri rule:

as shown in 1, there is a complex interaction between classes, and an action execution of a Classbase class causes multiple other associated classes to respond, for example, when Classbase has an action, and it has associated classes CLASSB, ClassC, CLASSD and so on will change, in the initial design, the interaction between objects can be simplified to 1 of the structure shown:

In Figure 1, because of the complexity of the interaction between classes, the addition of new objects in the system requires modifying the source code of other classes that interact with it, the system is less extensible, and it is not easy to add and delete new objects.

In this example, you can reduce the degree of coupling between objects by introducing a mediation class (mediator) that is designed to control interaction between objects. After the intermediate class is introduced, the direct reference is no longer occurring between the related objects, but instead the request is forwarded to the Intermediate class and the intermediate class completes the call to the other object. When you need to add or delete new objects, you can simply modify the intermediate class, without modifying the source code of the new or existing object, as shown in structure 2 after refactoring:

Summary of the use of the Dimitri rule:

(1). In the Division of classes, we should try to create loosely coupled classes, the lower the coupling between classes, the more conducive to reuse, a loosely coupled class once modified, will not affect the associated class too much;

(2). In the class structure design, each class should minimize its member variables and member functions access rights;

(3). In the design of the class, whenever possible, a type should be designed as an invariant class;

(4). On references to other classes, one object's references to other objects should be minimized.


Dimitri Law--seven object-oriented design principles (6)

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.