Definition:
Bridge Mode: separates abstract parts from their implementations so that they can all change independently.
Explanation: the separation of abstract and its implementation does not mean that abstract classes are separated from their derived classes, but abstract classes and Their Derived classes are used to implement their own objects.
UML class diagram and basic code:
class Program { static void Main(string[] args) { Abstration ab = new RefinedAbstration(); ab.SetImplementor (new ConcreteImplementorA ()); ab.Operation(); ab.SetImplementor(new ConcreteImplementorB()); ab.Operation(); Console.Read(); } } abstract class Implementor { public abstract void Operation(); } class ConcreteImplementorA : Implementor { public override void Operation() { Console.WriteLine("implement A action"); } } class ConcreteImplementorB : Implementor { public override void Operation() { Console.WriteLine("implement B action"); } } class Abstration { protected Implementor implementor; public void SetImplementor(Implementor implementor) { this.implementor = implementor; } public virtual void Operation() { implementor.Operation(); } } class RefinedAbstration : Abstration { public override void Operation() { implementor.Operation(); } }
View code
When you see an instance on the internet, it feels very appropriate. Refer to learning.
Specifically, the remote control is implemented in real life. The remote control does not include functions such as power-on, shutdown, and change. The remote control only contains references to these functions on the TV, then the infrared rays find the implementation of the corresponding functions on the TV.
Abstract class TV sets and Changhong and Samsung TV sets:
Public abstract class TV {public abstract void on (); public abstract void off (); public abstract void turnchannel ();} public class Changhong: TV {public override void on () {console. writeline ("Changhong TV already on");} public override void off () {console. writeline ("Changhong TV has been turned off");} public override void turnchannel () {console. writeline ("Changhong TV for Channel");} public class Samsung: TV {public override void on () {console. writeline ("the licensed TV has been turned on");} public override void off () {console. writeline ("the licensed TV has been turned off");} public override void turnchannel () {console. writeline ("changing the channel of a TV set with a license brand ");}}
View code
Abstract class remote control and implementation:
public class RemoteControl { private TV implementor; public TV Implementor { get { return implementor; } set { implementor = value; } } public virtual void On() { implementor.On(); } public virtual void Off() { implementor.Off(); } public virtual void TurnChannel() { implementor.TurnChannel(); } } public class ConcreteRemote : RemoteControl { }
View code
Client call:
RemoteControl remoteControl = new ConcreteRemote(); remoteControl.Implementor = new ChangHong(); remoteControl.On(); remoteControl.Off(); remoteControl.TurnChannel();
Advantages:
Abstract interfaces and implementations are decoupled.
Abstraction and implementation can be expanded independently without affecting each other.
Disadvantages:
This increases the complexity of the system.
Applicable scenarios:
A class has two independent dimensions, and both dimensions need to be extended.
Graphics and window systems that span multiple platforms.
Design Mode (8) --- Bridge Mode