Bridge Pattern

The bridge pattern is a structural pattern that separates the abstract part from its implementation part so that they can all change independently of each other.

The use of object-oriented techniques to make it easy for a type to vary in multiple directions without introducing additional complexity is at the heart of the bridging pattern.

role

1. Abstraction

The abstract interface is defined, which contains the Implementor interface to implement the specific behavior and characteristics.

2. Refined Abstraction

Subclass Abstraction, still an abstract thing name;

3. Implementor

Define specific behavior, specific characteristics of the application interface;

4. ConcreteImplementor

Implementor.

The sample



The BridgePattern namespace contains the TV base class, three concrete TV classes, one remote control base class, and one concrete remote control class. This case tries to use bridge mode to solve how to use remote control to complete the corresponding action of different TV sets.

public abstract class Television {

    public abstract void TurnOn();

    public abstract void TurnOff();

    public abstract void TurnChannel();

}
Copy the code

TV base Television class, including TurnOn TV TurnOn, TurnOff TV TurnOff, TurnChannel.

public class Konka : Television {

    public override void TurnOn() {
        Console.WriteLine("Turn on Konka!");
    }

    public override void TurnOff() {
        Console.WriteLine("Turn off Konka!");
    }

    public override void TurnChannel() {
        Console.WriteLine("Turn channel Konka!"); }}Copy the code

Specific TV category, Konka Konka category.

public class Lenovo : Television {

    public override void TurnOn() {
        Console.WriteLine("Turn on Lenovo!");
    }

    public override void TurnOff() {
        Console.WriteLine("Turn off Lenovo!");
    }

    public override void TurnChannel() {
        Console.WriteLine("Turn channel Lenovo!"); }}Copy the code

Specific TV category, Lenovo Lenovo category.

public class Panasonic : Television {

    public override void TurnOn() {
        Console.WriteLine("Turn on Panasonic!");
    }

    public override void TurnOff() {
        Console.WriteLine("Turn off Panasonic!");
    }

    public override void TurnChannel() {
        Console.WriteLine("Turn channel Panasonic!"); }}Copy the code

Specific TV category, Panasonic category.

public class TeleControllerBase {

    public Television Television { get; set; }

    public virtual void TurnOn() {
        Television.TurnOn();
    }

    public virtual void TurnOff() {
        Television.TurnOff();
    }

    public virtual void TurnChannel(){ Television.TurnChannel(); }}Copy the code

The TeleControllerBase class maintains an internal reference to the TeleControllerBase class and contains the corresponding three TV functions.

public class TeleController : TeleControllerBase {

    public override void TurnOff() {
        base.TurnOff();
        Console.WriteLine("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --"); }}Copy the code

The specific remote control TeleController class.

public class Program {

    private static TeleControllerBase _teleController = null;

    public static void Main(string[] args) {
        _teleController = new TeleController();

        _teleController.Television = new Konka();
        _teleController.TurnOn();
        _teleController.TurnChannel();
        _teleController.TurnOff();

        _teleController.Television = new Lenovo();
        _teleController.TurnOn();
        _teleController.TurnChannel();
        _teleController.TurnOff();

        _teleController.Television = newPanasonic(); _teleController.TurnOn(); _teleController.TurnChannel(); _teleController.TurnOff(); Console.ReadKey(); }}Copy the code

This is the caller’s code, and here is the output of the case:

Turn on Konka!
Turn channel Konka!
Turn off Konka!
-------------------------
Turn on Lenovo!
Turn channel Lenovo!
Turn off Lenovo!
-------------------------
Turn on Panasonic!
Turn channel Panasonic!
Turn off Panasonic!
-------------------------
Copy the code

advantages

1. Separation of abstraction and implementation; 2. Excellent ability to expand; 3. Implementation details are transparent to customers.

disadvantages

1. The introduction of bridge mode will increase the difficulty of understanding and designing the system. Since the aggregation association relationship is established at the abstraction layer, developers are required to design and program for abstraction.

Usage scenarios

1. If a system needs to add more flexibility between the abstract role and the embodied role of the component, avoid the establishment of a static inheritance relationship between the two levels, through the bridge pattern can make them establish an association relationship at the abstraction level; 2. The bridge mode is especially suitable for those systems that do not want to use inheritance or the number of system classes increases dramatically due to multi-level inheritance; 3. A class has two dimensions that change independently, and both dimensions need to be extended.