Home >Java >javaTutorial >How to use java's bridge mode

How to use java's bridge mode

WBOY
WBOYforward
2023-05-14 18:28:061609browse

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();  
    }  
}

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete