Home Backend Development C#.Net Tutorial .NET bridge mode explained

.NET bridge mode explained

Dec 20, 2016 pm 12:57 PM
bridge mode

Definition of bridge pattern:

Decouple abstraction from implementation so that they can change independently.

Bridge mode structure diagram:

.NET bridge mode explained

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();
}
Copy after login

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("长虹牌电视机换频道");
 }
}
Copy after login

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();
 }
}
Copy after login

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();
 }
}
Copy after login

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();
  }
Copy after login

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.


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to handle special characters in C language How to handle special characters in C language Apr 03, 2025 pm 03:18 PM

In C language, special characters are processed through escape sequences, such as: \n represents line breaks. \t means tab character. Use escape sequences or character constants to represent special characters, such as char c = '\n'. Note that the backslash needs to be escaped twice. Different platforms and compilers may have different escape sequences, please consult the documentation.

What is the role of char in C strings What is the role of char in C strings Apr 03, 2025 pm 03:15 PM

In C, the char type is used in strings: 1. Store a single character; 2. Use an array to represent a string and end with a null terminator; 3. Operate through a string operation function; 4. Read or output a string from the keyboard.

How to use various symbols in C language How to use various symbols in C language Apr 03, 2025 pm 04:48 PM

The usage methods of symbols in C language cover arithmetic, assignment, conditions, logic, bit operators, etc. Arithmetic operators are used for basic mathematical operations, assignment operators are used for assignment and addition, subtraction, multiplication and division assignment, condition operators are used for different operations according to conditions, logical operators are used for logical operations, bit operators are used for bit-level operations, and special constants are used to represent null pointers, end-of-file markers, and non-numeric values.

The difference between char and wchar_t in C language The difference between char and wchar_t in C language Apr 03, 2025 pm 03:09 PM

In C language, the main difference between char and wchar_t is character encoding: char uses ASCII or extends ASCII, wchar_t uses Unicode; char takes up 1-2 bytes, wchar_t takes up 2-4 bytes; char is suitable for English text, wchar_t is suitable for multilingual text; char is widely supported, wchar_t depends on whether the compiler and operating system support Unicode; char is limited in character range, wchar_t has a larger character range, and special functions are used for arithmetic operations.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

How to convert char in C language How to convert char in C language Apr 03, 2025 pm 03:21 PM

In C language, char type conversion can be directly converted to another type by: casting: using casting characters. Automatic type conversion: When one type of data can accommodate another type of value, the compiler automatically converts it.

How to use char array in C language How to use char array in C language Apr 03, 2025 pm 03:24 PM

The char array stores character sequences in C language and is declared as char array_name[size]. The access element is passed through the subscript operator, and the element ends with the null terminator '\0', which represents the end point of the string. The C language provides a variety of string manipulation functions, such as strlen(), strcpy(), strcat() and strcmp().

What is the function of C language sum? What is the function of C language sum? Apr 03, 2025 pm 02:21 PM

There is no built-in sum function in C language, so it needs to be written by yourself. Sum can be achieved by traversing the array and accumulating elements: Loop version: Sum is calculated using for loop and array length. Pointer version: Use pointers to point to array elements, and efficient summing is achieved through self-increment pointers. Dynamically allocate array version: Dynamically allocate arrays and manage memory yourself, ensuring that allocated memory is freed to prevent memory leaks.

See all articles