c# design pattern-structural pattern bridge pattern

Preface

        Bridge pattern is a design pattern that separates abstraction from implementation so that they can vary independently. This pattern involves an interface acting as a bridge, making the functionality of the entity class independent of the interface implementation class. These two types of classes can be structurally changed without affecting each other.

        The main purpose of the Bridge pattern is to allow the two to vary independently by separating implementation and abstraction. This type of design pattern is a structural pattern and it does this by providing a bridging structure between abstraction and implementation.

        For example, a TV remote control, its basic functions (on/off, volume control, etc.) are abstract, and these functions can be implemented through different implementations (different brands of TVs). Without bridge mode, each brand of TV would require a dedicated remote. But with bridge mode, you can have a universal remote control to control TVs of different brands through bridge mode.

Bridge mode contains the following main roles:

  1. Abstraction role: defines an abstract class and contains a reference to the implemented object.
  2. Extended abstraction (Refined Abstraction) role: It is a subclass of the abstract role, implements the business methods in the parent class, and calls the business methods in the implemented role through the combination relationship.
  3. Implementor role: defines the interface of the implemented role, which can be called by extending the abstract role.
  4. Concrete Implementor role: Gives the specific implementation of the implemented role interface.

Code examples 

In the following case, it is demonstrated that the bridge mode is used to realize the combination of animals and colors in the pet store. If we use the ordinary way to write this case, we may need to write m (kind) × n (color) classes , but use the bridge mode, we only need to write m (type) + n (color) classes, and then use them to combine into different requirements

Animal interface

Implement the interface to create the first animal

public interface IAnimal
{
    void animal(string colour);
}

dog

Implement dog class

public class Dog:IAnimal
{
    public void animal(string colour)
    {
        Console.WriteLine("这只狗的颜色是:"+colour);
    }
}

Cat

Implement class cat. If you need more types later, you only need to add the implementation class.

public class Cat:IAnimal
{
    public void animal(string colour)
    {
        Console.WriteLine("这只猫的颜色是:"+colour);
    }
}

color picker

Combining animal types and colors is implemented in this class. In the color selector, an animal is declared, a constructor is set, an animal class is passed in, and an abstract method is written to print animal.

public abstract class ColourSelect
{
    protected IAnimal Animal;


    protected ColourSelect(IAnimal animal)
    {
        Animal = animal;
    }

    public abstract void outAnimal(string colour);

}

White

White inherits the color selector, passes the parameters of the constructor to the parent class, and overrides the outAnimal method, calling animal in it. In fact, it is what we wrote at the beginning, outputting animal + color.

public class black : ColourSelect
{
    public black(IAnimal animal) : base(animal)
    {
    }

    public override void outAnimal(string colour)
    {
        Animal.animal(colour);
    }
}

 black

Same as above

public class white : ColourSelect
{
    public white(IAnimal animal) : base(animal)
    {
    }

    public override void outAnimal(string colour)
    {
        Animal.animal(colour);
    }
}

Test Results

new is a black class that inherits the color selector and passes in a cat class. Therefore, black.outAnimal calls the animal method of the cat class and carries a parameter black, so through Console.WriteLine("The color of this cat is: "+colour); Print the color of this cat: black, the following white dog example is the same as above.

Bridge mode advantages

         The bridge mode improves the scalability of the system. You can expand any one of the two changing dimensions without modifying the original system. For example: If there is another video file type wmv, we only need to define another class to implement the VideoFile interface, and other classes do not need to change. Implementation details are transparent to customers

scenes to be used

        When a class has two independently changing dimensions, and both dimensions need to be extended. When a system does not want to use inheritance or the number of system classes increases sharply due to multi-level inheritance. When a system requires more flexibility between the abstract and concrete roles of components. Avoid establishing a static inheritance relationship between two levels, and use the bridge pattern to establish an association between them at the abstraction layer.

Guess you like

Origin blog.csdn.net/weixin_65243968/article/details/132324700