Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java bridging mode

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to use Java bridging mode". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use Java bridging mode" can help you solve the problem.

In fact, in real life, there are many categories that can have changes in two or more dimensions. For example, graphics can be divided not only by shape, but also by color. If inheritance is used, there are m shapes and n colors. Not only there are many corresponding subcategories, but also it is difficult to expand.

For example, text of different colors and fonts, cars of different brands and power, men and women of different genders and occupations, media players that support different platforms and different file formats, etc. If we use the bridge mode, we can solve these problems very well.

1. Definition of bridging mode

Separate the abstraction from the implementation so that they can change independently. It is realized by using combinatorial relations instead of inheritance relations, thus reducing the coupling degree of the two variable dimensions of abstraction and implementation.

Pattern type: structural design pattern

Schematic class diagram:

Schematic class diagram description:

Client class: the caller of the bridge mode

Abstract class (Abstraction): maintains Implementor/, its implementation class ConcretelmplementorA. The relationship between the two is a combination of devices, and Abstraction acts as a bridge.

RehinedAbstraction: is a subclass of the Abstraction abstract class lmplementor: the interface of the behavior implementation class

ConcretelmplementorA/B: concrete implementation class of behavior

From the UML diagram: the abstract class and interface here are aggregate relationships, which are actually called and called relationships

2. Advantages and disadvantages of bridging mode.

Advantages:

Separation of abstraction and implementation, strong scalability

In accordance with the principle of opening and closing

In accordance with the principle of synthetic reuse

Its implementation details are transparent to customers.

Disadvantages:

Since the aggregation relationship is based on the abstraction layer, developers are required to design and program for abstraction, and can correctly identify two independently changing dimensions in the system, which increases the difficulty of system understanding and design.

3. The structure of bridge mode.

The Bridge mode consists of the following main roles:

Abstraction role: defines an abstract class and contains a reference to the implemented object

Extended abstraction (Refined Abstraction) roles: subclasses of abstract roles, implement business methods in the parent class, and invoke business methods in the implemented role through composition relationships

Implementor role: defines the interface of the implemented role, which can be called by the extended abstract role

Concrete Implementor role: the concrete implementation of the role interface is given.

Structure diagram:

The implementation code of the structure diagram:

Visual roles:

/ * * Visual role * / public interface Implemntor {public void OperationImpl ();}

Specific implementation of the role:

/ * concrete implementation role * / public class ConcreteImplementorA implements Implemntor {@ Override public void OperationImpl () {System.out.println ("specific implementation role is accessed");}}

Abstract roles:

/ * abstract role * / public abstract class Abstraction {protected Implemntor implemntor; protected Abstraction (Implemntor implemntor) {this.implemntor = implemntor;} public abstract void Operation ();}

Extend the abstract role:

/ * extended abstract role * / public class RefinedAbstraction extends Abstraction {protected RefinedAbstraction (Implemntor implemntor) {super (implemntor);} public void Operation () {System.out.println ("extended abstract role accessed"); 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:

Extended abstract role accessed specific implementation role accessed 4. Case implementation of bridging pattern

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

Vehicles drive on the road, there are two dimensions of change, the type of transport is different, the road is also divided into cement road and asphalt road

Class diagram:

Vehicle category:

/ * * vehicle class * / public interface Vehicle {public void drive ();}

Specific means of transportation: cars

/ * * specific means of transport: car * / public class Car implements Vehicle {@ Override public void drive () {System.out.println ("car");}}

Specific means of transportation: bus

/ * * specific means of transport: public class Bus implements Vehicle {@ Override public void drive () {System.out.println ("bus");}}

The path of abstraction:

/ * Abstract path * / public abstract class Road {protected Vehicle vehicle; public Road (Vehicle vehicle) {this.vehicle = vehicle;} public abstract void driveOnRoad ();}

Specific road: oil cypress road

/ * * specific road: public class UnpavedRoad extends Road {public UnpavedRoad (Vehicle vehicle) {super (vehicle);} @ Override public void driveOnRoad () {super.vehicle.drive (); System.out.println ("driving on Cedar Road");}}

Concrete road: cement road

/ * * 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 ("driving on cement road");}}

Test class:

/ / Test 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:

Car driving on cement Road bus driving on cement Road 5. Matters needing attention in bridging mode

It realizes the separation of the abstract part and the implementation part, which greatly provides the flexibility of the system and makes the abstract part and the implementation part independent, which is helpful for the hierarchical design of the system, thus resulting in a better structured system.

For the high-level part of the system, you only need to know the interface between the abstract part and the implementation part, and the other parts are completed by the specific business.

Instead of multi-layer inheritance scheme, bridging mode can reduce the number of subclasses and reduce the cost of management and maintenance of the system.

The introduction of bridging pattern increases the difficulty of system understanding and design. Because the aggregation relationship is based on the abstract layer, developers are required to design and program for the abstraction.

The bridging mode requires the correct identification of two independently changing dimensions in the system, so its scope of use has some limitations, that is, such an application scenario is required.

6. Application scenario of bridging mode

Bridging mode is especially suitable for systems that do not want to use inheritance or that the number of system classes increases sharply because of multi-level inheritance.

This is the end of the content about "how to use Java bridging mode". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report