Home  >  Article  >  Java  >  Java design pattern: understanding of bridge pattern abstraction and implementation decoupling

Java design pattern: understanding of bridge pattern abstraction and implementation decoupling

php是最好的语言
php是最好的语言Original
2018-08-01 14:35:112438browse

Bridge mode: Separate the abstract part from its implementation part, so that abstraction and implementation are decoupled so that they can change independently.

The abstract part and the implementation part are decoupled through the bridge (interface). The bridge is an interface. The implementation party inherits this interface, and the abstract party calls the method in the interface in the abstract class to point to the implementation party. In this way, the implementation side performs unilateral expansion by implementing the bridge interface, and the abstract side performs unilateral expansion by inheriting the abstract class. Both are called through the bridge interface, and the interface is not affected by the expansion of both parties.

UML class diagram:

Java design pattern: understanding of bridge pattern abstraction and implementation decoupling

interface Driver {
    public void connect();
}

class MysqlDriver implements Driver {
    @Override
    public void connect() {
        System.out.println("连接Mysql数据库");
    }
}
class OracleDriver implements Driver {
    @Override
    public void connect() {
        System.out.println("连接Oracle数据库");
    }
}
//抽象类
abstract class Bridge {
    private Driver driver;
    public void connect() {
        driver.connect();
    }
    public void setDriver(Driver driverTemp) {
        this.driver = driverTemp;
    }
    public Driver getDriver() {
        return this.driver;
    }
}
//实现类
class MyBridge extends Bridge { }
//测试类
public class BridgeTest {
    public static void main(String[] args) {
        Bridge mysqlBridge = new MyBridge();

        Driver mysqlDriver = new MysqlDriver();
        mysqlBridge.setDriver(mysqlDriver);
        mysqlBridge.connect();

        OracleDriver oracleDriver = new OracleDriver();
        mysqlBridge.setDriver(oracleDriver);
        mysqlBridge.connect();
    }
}

The bridge mode is directional. The party bound to the bridge is the callee, which belongs to the passive party and the abstract party. Belongs to the active party.

The bridge mode reduces the coupling of the code, improves the scalability of the system, and achieves transparency of details. However, the introduction of the bridge mode has made it more difficult to understand and design the system

Related articles:

Detailed explanation of the bridge mode in Java design patterns

JavaScript Design Pattern Series 6: Bridge Pattern

Related videos:

JavaScript Design Pattern Complete Video Tutorial

The above is the detailed content of Java design pattern: understanding of bridge pattern abstraction and implementation decoupling. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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