search
HomeJavajavaTutorialDetailed introduction to Java bridging mode

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. If there is any infringement, please contact admin@php.cn delete
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.