Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The definition of .NET Bridge Mode and its advantages and disadvantages

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the definition, advantages and disadvantages of .NET bridging mode". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the definition, advantages and disadvantages of .NET bridging pattern.

Definition of bridging mode:

Abstraction and Implementation are decoupled so that they can change independently.

Bridge mode structure diagram:

Roles in bridging mode:

Abstraction roles: abstract the definition given and save a reference to the implemented object.

Fix abstraction (Refined Abstraction) roles: extend abstraction roles, change and modify the parent class's definition of abstraction.

Implementor role: this role gives the 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. The implementing role should only give the underlying operation, while the abstract role should only give a higher level of operation based on the underlying operation.

An example is given to illustrate:

To quote an example of a TV remote control, for each brand of remote control, there is a corresponding remote control to control. At this time, we may think of a remote control interface that abstracts a remote control interface in which the computer is turned on and off. A set of functional methods such as changing channels. Then create a specific remote control class to inherit this interface and implement the methods in it. This can satisfy that each TV set has its own remote control, and for other types of televisions, it is only necessary to add a derived class to satisfy the derivation of the new remote control. But one day, when users ask to add a function to return to the previous channel in the remote control, they need to change the abstract remote control interface and add a new method to the abstract class, which changes the implementation of the abstract class. If the user asks 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. These problems can be well solved by using bridging mode.

Use:

1. First of all, the TV is abstracted and the behavior method of the remote control is provided.

/ TV set, providing abstract methods / public abstract class TV {public abstract void On (); public abstract void Off (); public abstract void tuneChannel ();}

two。 Create a concrete TV set that inherits from the abstract TV class:

/ three-star TV, override the abstract method of the base class / public class Samsung:TV {public override void On () {Console.WriteLine ("three-star TV has been turned on");} public override void Off () {Console.WriteLine ("three-star TV has been turned off");} public override void tuneChannel () {Console.WriteLine ("three-star TV changes channels") } / Changhong brand TV, override the abstract method of the base class / provide concrete implementation / public class ChangHong: TV {public override void On () {Console.WriteLine ("Changhong brand TV has been turned on");} public override void Off () {Console.WriteLine ("Changhong brand TV has been turned off") } public override void tuneChannel () {Console.WriteLine ("Changhong brand TV sets change channels");}}

3. Then abstract the remote control in the overview and play the role of abstract words.

/ the remote control in the abstract concept acts as an abstract role / public abstract class RemoteControl {public TV implementor {get; set;} / turn on the TV / the implementation is no longer provided in the abstract class, but calls the implementation / public virtual void On () {implementor.On () in the implementation class. } / turn off the TV / public virtual void Off () {implementor.Off ();} / change the channel / public virtual void SetChannel () {implementor.tuneChannel ();}}

4. Create a specific remote control class: here, I rewrite the method of changing the channel, but there are other methods that can be overridden.

/ specific remote control class / public class ConcreteRemote:RemoteControl {/ public override void SetChannel () {Console.WriteLine ("rewrite channel change method"); base.SetChannel ();}}

5. Call:

Static void Main (string [] args) {/ / create a remote control RemoteControl remoteControl = new ConcreteRemote (); / / Changhong TV remoteControl.implementor = new ChangHong (); remoteControl.On (); remoteControl.SetChannel (); remoteControl.Off (); Console.WriteLine (); / / three-star TV remoteControl.implementor = new Samsung (); remoteControl.On (); remoteControl.SetChannel (); remoteControl.Off (); Console.Read ();}

In this way, the design of the bridge mode is realized, and the function realization method of the remote controller is not realized in the remote controller, but the implementation part is used to encapsulate it by another TV class, and the remote controller contains only one reference of the TV class. Through the bridging mode, we separate the abstraction from the realization, so that we can deal with the changes in these two aspects well.

Advantages:

The abstract interface is decoupled from its implementation, and the mid-term abstraction and implementation can be extended independently without affecting each other.

Disadvantages:

It increases the complexity of the system.

Use the scene:

1. If a system needs to add more flexibility between the abstract and concrete roles of the component, avoid establishing a static relationship between the two levels

2. The design requires that any change in the implementation role should not affect the client, or the change in the implementation role should be completely transparent to the client.

3. Need to be on graphics and window systems across multiple platforms

4. There are two independent changing dimensions in a class, and both dimensions need to be extended.

At this point, I believe you have a deeper understanding of the definition, advantages and disadvantages of .NET bridging mode. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report