Home > Java > javaTutorial > body text

How to use java's bridge mode

WBOY
Release: 2023-05-14 18:28:06
forward
1483 people have browsed it

Explanation

1. The bridge mode separates the abstract part from the implementation part so that they can change independently.

It is implemented using a combination relationship instead of an inheritance relationship, thus reducing the coupling between the two variable dimensions of abstraction and implementation.

2. The bridge mode converts inheritance relationships into association relationships, reducing the coupling between classes, the number of classes in the system, and the amount of code.

Usage Scenarios

Not expected or applicable to inheritance scenarios.

The interface or abstract scene is unstable.

Scenarios with high reusability requirements.

Example

public class BridgeTest {  
      
    public static void main(String[] args) {  
          
        Bridge bridge = new MyBridge();  
          
        /*调用第一个对象*/  
        Sourceable source1 = new SourceSub1();  
        bridge.setSource(source1);  
        bridge.method();  
          
        /*调用第二个对象*/  
        Sourceable source2 = new SourceSub2();  
        bridge.setSource(source2);  
        bridge.method();  
    }  
}
Copy after login

The above is the detailed content of How to use java's bridge mode. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!