Definition of bridge pattern:
Decouple abstraction from implementation so that they can change independently.
Bridge mode structure diagram:
Roles in bridge mode:
Abstraction (Abstraction) role: Abstract the given definition and save a reference to the implemented object.
Refined Abstraction role: Expand the abstraction role, change and modify the definition of abstraction by the parent class.
Implementor role: This role gives an interface to implement the role, but does not give a specific implementation. It must be pointed out that this interface is not necessarily the same as the interface definition of the abstract role. In fact, the two interfaces can be very different. Implemented roles should only provide low-level operations, while abstract roles should only provide higher-level operations based on low-level operations.
Combined with examples:
Cite the example of a TV remote control. For each brand of remote control, there is a corresponding remote control to control. At this time, the design we think of may be: abstract a remote control interface, inside A set of functional methods such as power on, power off, and channel changing need to be implemented. Then create a specific remote control class to inherit this interface and implement the methods inside. This allows each TV to implement its own remote control. For new types of TVs, you only need to add a derived class to satisfy the derivation of the new remote control. But one day, when the user requests to add a function to return to the previous channel in the remote control, the abstract remote control interface needs to be changed, and a new method needs to be added to the abstract class, thus changing the implementation of the abstract class. If the user requests to change the product behavior of the TV and the behavior of the remote control at the same time, it will cause great changes to the above design. Using bridge mode can solve these problems very well.
Usage:
1. First abstract the TV and provide a behavior method for the remote control to change.
/// <summary> /// 电视机,提供抽象方法 /// </summary> public abstract class TV { public abstract void On(); public abstract void Off(); public abstract void tuneChannel(); }
2. Create a concrete TV, inherited from the abstract TV class:
/// <summary> /// 三星牌电视机,重写基类的抽象方法 /// </summary> public class Samsung:TV { public override void On() { Console.WriteLine("三星牌电视机已经打开了"); } public override void Off() { Console.WriteLine("三星牌电视机已经关掉了"); } public override void tuneChannel() { Console.WriteLine("三星牌电视机换频道"); } } /// <summary> /// 长虹牌电视机,重写基类的抽象方法 /// 提供具体的实现 /// </summary> public class ChangHong : TV { public override void On() { Console.WriteLine("长虹牌电视机已经打开了"); } public override void Off() { Console.WriteLine("长虹牌电视机已经关掉了"); } public override void tuneChannel() { Console.WriteLine("长虹牌电视机换频道"); } }
3. Then abstract the remote control in the overview and play the role of abstract words.
/// <summary> /// 抽象概念中的遥控器,扮演抽象化角色 /// </summary> public abstract class RemoteControl { public TV implementor { get; set; } /// <summary> /// 开电视机 /// 这里抽象类中不再提供实现了,而是调用实现类中的实现 /// </summary> public virtual void On() { implementor.On(); } /// <summary> /// 关电视机 /// </summary> public virtual void Off() { implementor.Off(); } /// <summary> /// 换频道 /// </summary> public virtual void SetChannel() { implementor.tuneChannel(); } }
4. Create a specific remote control class: Here, I have rewritten the method of changing channels. In fact, other methods can be rewritten.
/// <summary> /// 具体遥控器类 /// </summary> public class ConcreteRemote:RemoteControl { /// <summary> /// 重写更换频道方法 /// </summary> public override void SetChannel() { Console.WriteLine("重写更换频道方法"); base.SetChannel(); } }
5. Call:
static void Main(string[] args) { // 创建一个遥控器 RemoteControl remoteControl = new ConcreteRemote(); //长虹电视机 remoteControl.implementor = new ChangHong(); remoteControl.On(); remoteControl.SetChannel(); remoteControl.Off(); Console.WriteLine(); // 三星牌电视机 remoteControl.implementor = new Samsung(); remoteControl.On(); remoteControl.SetChannel(); remoteControl.Off(); Console.Read(); }
In this way, the design of the bridge mode is realized. The function implementation method of the remote control is not implemented in the remote control, but the implementation part is used in another A TV class encapsulates it. The remote control only contains a reference to the TV class. Through the bridge mode, we separate the abstraction and implementation parts, so that we can well cope with changes in these two aspects.
Advantages:
The abstract interface is decoupled from its implementation. The abstraction and implementation can be expanded independently without affecting each other.
Disadvantages:
Increases the complexity of the system.
Usage scenarios:
1. If a system needs to add more flexibility between the abstract role and the concrete role of components to avoid establishing a static connection between the two levels
2. Realization of design requirements Any changes to the role should not affect the client, or changes to the implemented role should be completely transparent to the client.
3. Graphics and window systems that need to span multiple platforms
4. A class has two independently changing dimensions, and both dimensions need to be expanded.
The above is the entire content of this article. I hope it will be helpful to everyone’s learning. I also hope that everyone will support the PHP Chinese website.

This article explores the challenges of NULL pointer dereferences in C. It argues that the problem isn't NULL itself, but its misuse. The article details best practices for preventing dereferences, including pre-dereference checks, pointer initiali

This article explains how to create newline characters in C using the \n escape sequence within printf and puts functions. It details the functionality and provides code examples demonstrating its use for line breaks in output.

This article guides beginners on choosing a C compiler. It argues that GCC, due to its ease of use, wide availability, and extensive resources, is best for beginners. However, it also compares GCC, Clang, MSVC, and TCC, highlighting their differenc

This article emphasizes the continued importance of NULL in modern C programming. Despite advancements, NULL remains crucial for explicit pointer management, preventing segmentation faults by marking the absence of a valid memory address. Best prac

This article reviews online C compilers for beginners, focusing on ease of use and debugging capabilities. OnlineGDB and Repl.it are highlighted for their user-friendly interfaces and helpful debugging tools. Other options like Programiz and Compil

This article discusses efficient code copying in C IDEs. It emphasizes that copying is an IDE function, not a compiler feature, and details strategies for improved efficiency, including using IDE selection tools, code folding, search/replace, templa

This article compares online C programming platforms, highlighting differences in features like debugging tools, IDE functionality, standard compliance, and memory/execution limits. It argues that the "best" platform depends on user needs,

This article troubleshoots missing output windows in C program compilation. It examines causes like failing to run the executable, program errors, incorrect compiler settings, background processes, and rapid program termination. Solutions involve ch


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor
