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.

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment