Detailed 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.
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:
Principle class diagram description:
- Client class: Bridge mode The caller
- Abstract class (Abstraction): maintains Implementor/that is, its implementation class ConcretelmplementorA...the two are in a combined relationship, and Abstraction acts as a bridge
- RehinedAbstraction: is a subclass of Abstraction abstract class lmplementor: interface of behavior implementation class
- ConcretelmplementorA/B: concrete implementation class of behavior
- 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:
- Separation of abstraction and implementation, strong scalability
- Conforms to the principle of opening and closing
- Conforms to the principle of synthesis and reuse
- 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:
- Abstraction role: Defines an abstract class and contains an implementation object Reference
- 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 Implementor role: defines the interface of the implemented role, which is used to extend the abstract role call
- Concrete Implementor role: gives the specific implementation of the implemented role interface
Structure diagram:
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:
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、桥接模式的注意事项
- 实现了抽象和实现部分的分离,从而极大的提供了系统的灵活性,让抽象部分和实现部分独立开来,这有助于系统进行分层设计,从而产生更好的结构化系统
- 对于系统的高层部分,只需要知道抽象部分和实现部分的接口就可以了,其它的部分由具体业务来完成
- 桥接模式替代多层继承方案,可以减少子类的个数,降低系统的管理和维护成本
- 桥接模式的引入增加了系统的理解和设计难度,由于聚合关联关系建立在抽象层,要求开发者针对抽象进行设计和编程
- 桥接模式要求正确识别出系统中两个独立变化的维度,因此其使用范围有一定的局限性,即需要有这样的应用场景
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4
