Home >Backend Development >C#.Net Tutorial >.NET bridge mode explained
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.