[Design mode] concept analysis of coupling and decoupling

Source: Internet
Author: User

Is coupling dependent between two layers?
=
The same layer may also have dependencies.

We can see that the decoupling method adds a layer between the two interdependent layers. But isn't the newly added layer coupled with the original two layers?
=
The decoupling method is to make the design dependent on abstraction, rather than implementation. Let's look at the principles of the design pattern.

What is coupling ??
=
A explicitly uses B Resources

 
PublciClassA {B ;}Public ClassB {}

Now a depends on B

InterfaceIX {...}Public ClassA {ix I;PublicA (ix I ){This. I =I ;}}Public ClassB: IX {}

During the call, I can decide which class to implement the interface, or even generate from the configuration file reflection.

A A = new A (New B ());
It may also be caused by a condition.
A A = new A (New C ());

In this way, a depends on the abstract interface IX, rather than the specific Class B or C.

Question

Forgive me for being ignorant and weak. If I am weak, I would like to say that the "dependency on abstract interfaces does not depend on reality" here. Does it mean that no matter how the version of class B and class C compilation changes, but you can always call interfaces to call class B and class C, so Class A does not need to be compiled?

 
Answer

1. If B is modified, we can use C because we depend on interfaces rather than specific classes (objects)
For example, car makers can rely on the abstract tire, but cannot rely on specific brands such as Feng Shen tire"

2. When the requirement changes, we only need to modify the class where the main function is located (or even it may not need to be modified, but only the configuration file needs to be modified ).
Re-Compilation of the program is costly. For example, if windows is updated, it only provides patches instead of a new version of Windows.

The basic idea of a software design program is to minimize the number of changes in our applications when demand changes.

[Design mode] concept analysis of coupling and decoupling

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.