Home >Java >javaTutorial >Detailed introduction to Java bridging mode

Detailed introduction to Java bridging mode

WBOY
WBOYforward
2022-05-01 09:00:232603browse

This article brings you relevant knowledge about java, which mainly introduces the issues related to the bridge mode. The bridge mode separates abstraction and implementation so that they can change independently, reducing abstraction. Let's take a look at the coupling degree of these two variable dimensions. I hope it will be helpful to everyone.

Detailed introduction to Java bridging mode

Recommended study: "java video tutorial"

In fact, in real life, there are many classes that can have two or more Changes in two dimensions. For example, graphics can be divided by shape or color. If inheritance is used, there are m*n types of graphics with m shapes and n colors. Not only do there are many corresponding subcategories, but also the expansion comparison difficulty.

For example, text in different colors and fonts, cars of different brands and powers, men and women of different genders and occupations, media players that support different platforms and different file formats, etc. These problems can be solved well if you use bridge mode.


1. The definition of bridge mode

Separates abstraction and implementation so that they can change independently. It is implemented by using a combination relationship instead of an inheritance relationship, thus reducing the coupling degree of the two variable dimensions of abstraction and implementation

Pattern type: Structural design pattern

Principle class diagram:
Detailed introduction to Java bridging mode
Principle class diagram description:

  1. Client class: Bridge mode The caller
  2. Abstract class (Abstraction): maintains Implementor/that is, its implementation class ConcretelmplementorA...the two are in a combined relationship, and Abstraction acts as a bridge
  3. RehinedAbstraction: is a subclass of Abstraction abstract class lmplementor: interface of behavior implementation class
  4. ConcretelmplementorA/B: concrete implementation class of behavior
  5. From UML diagram: The abstract classes and interfaces here are in an aggregation relationship. In fact, the relationship between calling and being called

2. Advantages and disadvantages of the bridge mode

Advantages:

  1. Separation of abstraction and implementation, strong scalability
  2. Conforms to the principle of opening and closing
  3. Conforms to the principle of synthesis and reuse
  4. The implementation details are transparent to customers

Disadvantages:

  • ##Since the aggregation relationship is established at the abstraction layer, developers are required to target the abstraction It can correctly identify two independently changing dimensions in the system, which increases the difficulty of understanding and designing the system

3. The structure of the bridge mode

The Bridge pattern includes the following main roles:

  1. Abstraction role: Defines an abstract class and contains an implementation object Reference
  2. Extended abstraction (Refined Abstraction) role: It is a subclass of the abstract role, implements the business methods in the parent class, and calls the business methods in the implemented role through the combination relationship
  3. Implementor role: defines the interface of the implemented role, which is used to extend the abstract role call
  4. Concrete Implementor role: gives the specific implementation of the implemented role interface

Structure diagram:

Detailed introduction to Java bridging mode
Implementation code of this structure diagram:

Realized role:

/**
 * 实视化角色
 */public interface Implemntor {

    public void OperationImpl();}

Concrete realized role:

/**
 * 具体实现化角色
 */public class ConcreteImplementorA implements Implemntor{
    @Override
    public void OperationImpl() {
        System.out.println("具体实现化角色被访问");
    }}

Abstract role:

/**
 * 抽象化角色
 */public abstract class Abstraction {

    protected Implemntor implemntor;

    protected Abstraction(Implemntor implemntor){
        this.implemntor = implemntor;
    }

    public abstract void Operation();}

Extended abstract role:

/**
 * 扩展抽象化角色
 */public class RefinedAbstraction extends Abstraction{

    protected RefinedAbstraction(Implemntor implemntor) {
        super(implemntor);
    }

    public void Operation(){
        System.out.println("扩展抽象化角色被访问");
        implemntor.OperationImpl();
    }}

Test class:

public class Test {
    public static void main(String[] args) {

        Implemntor implemntor = new ConcreteImplementorA();
        Abstraction abs = new RefinedAbstraction(implemntor);
        abs.Operation();

    }}

Output:

扩展抽象化角色被访问
具体实现化角色被访问

4. Case implementation of bridge mode

Place implementation and abstraction in two different class levels so that the two levels can be changed independently

There are two dimensions of change when vehicles are driving on the road.

The types of vehicles are different, and roads are also divided into cement roads and asphalt roads

Picture:
Detailed introduction to Java bridging mode

Type of transportation:

/**
 * 交通工具类
 */public interface Vehicle {

    public void drive();}

Specific transportation: car

/**
 * 具体的交通工具:小汽车
 */public class Car implements Vehicle{
    @Override
    public void drive() {
        System.out.println("小汽车");
    }}

Specific means of transportation: bus

/**
 * 具体的交通工具:大巴车
 */public class Bus implements Vehicle{
    @Override
    public void drive() {
        System.out.println("大巴车");
    }}

Abstract road:

/**
 * 抽象的路
 */public abstract class Road {

    protected Vehicle vehicle;

    public Road(Vehicle vehicle){
        this.vehicle = vehicle;
    }

    public abstract void driveOnRoad();}

Specific road: asphalt Road

/**
 * 具体的路:油柏路
 */public class UnpavedRoad extends Road{
    public UnpavedRoad(Vehicle vehicle) {
        super(vehicle);
    }

    @Override
    public void driveOnRoad() {

        super.vehicle.drive();
        System.out.println("行驶在油柏路");
    }}

Specific road: cement road

/**
 * 具体的路:水泥路
 */public class CementRoad extends Road{
    public CementRoad(Vehicle vehicle) {
        super(vehicle);
    }

    @Override
    public void driveOnRoad() {
        super.vehicle.drive();
        System.out.println("行驶在水泥路");
    }}

Test class:

//测试public class Test {
    public static void main(String[] args) {

        Road roadCar = new CementRoad(new Car());
        roadCar.driveOnRoad();

        Road roadBus = new CementRoad(new Bus());
        roadBus.driveOnRoad();

    }}

Output :

小汽车
行驶在水泥路
大巴车
行驶在水泥路

5、桥接模式的注意事项

  1. 实现了抽象和实现部分的分离,从而极大的提供了系统的灵活性,让抽象部分和实现部分独立开来,这有助于系统进行分层设计,从而产生更好的结构化系统
  2. 对于系统的高层部分,只需要知道抽象部分和实现部分的接口就可以了,其它的部分由具体业务来完成
  3. 桥接模式替代多层继承方案,可以减少子类的个数,降低系统的管理和维护成本
  4. 桥接模式的引入增加了系统的理解和设计难度,由于聚合关联关系建立在抽象层,要求开发者针对抽象进行设计和编程
  5. 桥接模式要求正确识别出系统中两个独立变化的维度,因此其使用范围有一定的局限性,即需要有这样的应用场景

6、桥接模式应用场景

对于那些不希望使用继承或因为多层次继承导致系统类的个数急剧增加的系统,桥接模式尤为适用.

推荐学习:《java视频教程

The above is the detailed content of Detailed introduction to Java bridging mode. For more information, please follow other related articles on the PHP Chinese website!

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