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.
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:
Advantages:
Disadvantages:
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:
扩展抽象化角色被访问 具体实现化角色被访问
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 :
小汽车 行驶在水泥路 大巴车 行驶在水泥路
对于那些不希望使用继承或因为多层次继承导致系统类的个数急剧增加的系统,桥接模式尤为适用.
推荐学习:《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!