Class diagram:
/** * 手机壳、套 * @author stone * */ public abstract class BaseCellphoneShell { public abstract void mapping();//对应匹配哪种手机 }
/** * 手机,桥接了 手机壳BaseCellphoneShell * @author stone */ public abstract class BaseCellphone { private BaseCellphoneShell shell; public void setShell(BaseCellphoneShell shell) { this.shell = shell; } public BaseCellphoneShell getShell() { return shell; } public abstract void mapping();//对应哪种壳 }
/** * 手机的实现 * @author stone * */ public class Cellphone extends BaseCellphone { @Override public void mapping() { System.out.println("本手机对应的壳是:"); getShell().mapping(); } }
/** * 手机壳的实现 * @author stone * */ public class CellphoneShell extends BaseCellphoneShell { @Override public void mapping() { System.out.println("壳----iphone6"); } }
/* * 桥接(Bridge)模式, 是结构型模式 * * 将抽象化与实现化脱耦,使得二者可以独立的变化,也就是说将他们之间的强关联变成弱关联, * 也就是指在一个软件系统的抽象化和实现化之间使用组合/聚合关系而不是继承关系,从而使两者可以独立的变化。 */ public class Test { public static void main(String[] args) { /* * 在本例中:手机和手机壳之间有强关联的关系:要么手机对应一种壳(BaseCellphoneShell),要么壳对应一种手机(BaseCellphone) * 手机可以有多维度变化,如依据品牌划分的、依据尺寸划分的、依据个性需求划分的等 * 手机壳也有多维度变化,因为它要适配上面划分出来的手机 * 将桥作在了BaseCellphone中,聚合接入了BaseCellphoneShell */ BaseCellphone cellphone = new Cellphone(); cellphone.setShell(new CellphoneShell());//聚合接入 cellphone.mapping(); } }
The above is the detailed content of Sample code for implementing Bridge mode in Java. For more information, please follow other related articles on the PHP Chinese website!

Why can't the main class be found after copying and pasting the package in IDEA? Using IntelliJIDEA...

State synchronization between Java multi-interface calls: How to ensure that interface A is called after it is executed? In Java development, you often encounter multiple calls...

How to stop subsequent code execution when ID is repeated in Java programming. When learning Java programming, you often encounter such a requirement: when a certain condition is met,...

In-depth discussion of final consistency: In the distributed system of application scenarios and implementation methods, ensuring data consistency has always been a major challenge for developers. This article...

The troubleshooting idea of SSH connection failure after SpringBoot service has been running for a period of time has recently encountered a problem: a Spring...

How to push video streams from Hikvision camera SDK to front-end Vue project? During the development process, you often encounter videos that need to be captured by the camera to be circulated...

How to use access_token of OAuth2.0 to restrict interface access permissions How to ensure access_token when authorizing using OAuth2.0...

SpringBootRedis gets the key garbled problem analysis using Spring...


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.