A Simple Analysis of the Demeter rule and the dependency reversal Principle

Source: Internet
Author: User

/**
* The combination of the dimit rule (high cohesion principle) and the dependency inversion principle (image extraction layer application)
* (This idea runs through the entire design model)
* From <Java and mode>.
*CodeAnd Annotations: if it is not cold
* Note: Zhang sanli 4 is a friend relationship, while Li siwang 5 is a friend relationship. Zhang Sanhe and Wang 5 are strangers.
* To ensure that Zhang Sanhe and Wang wuzhong do not know each other, Zhang Sanhe can call the methods in Wang wuzhong.
* Use Li Si as a relay to isolate Zhang San and Wang Wu
First look at his UML diagram

*/

Public interface abstractstranger // specifies the target image, a stranger;
{
Abstract void operation3 ();
}
//---------------------------------
Public class someone // someone; (James)
{
Public void operation1 (friend)
{
Abstractstranger stranger = friend. Provide (); // Rishi replacement;
Stranger. operation3 ();
}
}
//---------------------------------
Public class friend // friend; (Li Si)
{
Private abstractstranger stranger = new stranger ();
Public void operation2 ()
{
System. Out. println ("operation2 ");
}
Public abstractstranger provide ()
{
Return stranger;
}
}
//---------------------------------
Public class stranger implements abstractstranger
{// Stranger (Wang Wu );
Public void operation3 ()
{
System. Out. println ("operation3 ");
}
}

Dependency inversion principle: It depends on abstraction and not on specifics.

8.3 dependency reversal Principle
Three Coupling Relationships:
1) Zero Coupling
2) specific coupling: occurs between two (instantiated) classes.
3) Abstract coupling: occurs between a specific class and an abstract class (or Java interface.

What?
Abstraction should not depend on details; details should depend on abstraction.

Another statement:
Do not implement programming for the interface.
A specific Java class should only implement Java interfaces and Abstract METHODS declared in the Java class, and should not provide redundant methods.

Static type of a variable: the type of the variable when it is declared.
True Type of a variable: The True Type of the object referenced by the variable.

The abstract type of the referenced object.
In many cases, a JavaProgramYou need to reference an object. At this time, if this object has an abstract type, you should use this abstract typeStatic variable type. This isInterface Programming.
Example: Egg x = new chicken ();
Instead of: Chicken x = new chicken ();

List employees = new vector ();
Instead of: vector employees = new vector ();

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.