In real life, some classes have two or more dimensions of variation, such as graphics can be divided by shape, but also by color. How to design a software like Photoshop that can draw graphics of different shapes and colors? If the inheritance method is used, there are m×n graphics with m shapes and n colors, which not only corresponds to many subclasses, but also is difficult to expand.
Of course, there are many more such examples, such as text in different colors and fonts, cars of different brands and power, men and women of different genders and occupations, media players that support different platforms and different file formats, etc. If you use bridge mode, you can solve these problems very well.
Definition and Features
The Bridge pattern is defined as follows: Separating the abstraction from the implementation so that they can change independently. It is implemented with a compositional relationship instead of an inheritance relationship, thereby reducing the coupling between the two variable dimensions of abstraction and implementation.
The advantages of Bridge mode are:
- Due to the separation of abstraction and implementation, it has strong scalability.
- The details of its implementation are transparent to the client.
The disadvantage is: because the aggregation relationship is established at the abstraction layer, developers are required to design and program for abstraction, which increases the difficulty of understanding and designing the system.
Structure and Implementation
The abstraction part can be separated from the realization part, the inheritance relationship between the two can be cancelled, and the composition relationship can be used instead.
schema structure
Bridge mode includes the following main roles:
- Abstraction role: Defines an abstract class and contains a reference to the realized object.
- Extended Abstraction (Refined Abstraction) role: It is a subclass of the abstracted role, implements the business methods in the parent class, and calls the business methods in the implemented role through the composition relationship.
- Implementor role: Define the interface of the implementor role, which is called by the extended abstract role.
- Concrete Implementor role: Gives the concrete implementation of the interface of the implemented role.
In general, Implementor interface provides only basic operations, while Abstraction defines higher-level operations based on these basic operations.
Its structure diagram is shown in the figure:
Implementation of the pattern
The code for bridge mode is as follows:
//access class public class Program { static void Main(string[] args) { //bridge mode IImplementor imple=new ConcreteImplementorA(); Abstraction abs=new RefinedAbstraction(imple); abs.Operation(); Console.ReadLine(); } } //realization role public interface IImplementor { void OperationImpl(); } //Concrete realization of role A public class ConcreteImplementorA : IImplementor { public void OperationImpl() { Console.WriteLine("concrete realization(Concrete Implementor)Role A be visited" ); } } //abstract role public abstract class Abstraction { protected IImplementor imple; protected Abstraction(IImplementor imple) { this.imple=imple; } public abstract void Operation(); } //Extended Abstraction Role public class RefinedAbstraction : Abstraction { public RefinedAbstraction(IImplementor imple): base(imple) { } public override void Operation() { Console.WriteLine("Extended abstraction(Refined Abstraction)role is accessed"); imple.OperationImpl(); } }
The result of running the program is as follows:
Extended abstraction(Refined Abstraction)role is accessed concrete realization(Concrete Implementor)role is accessed
Application scenarios
Bridge mode is usually suitable for the following scenarios:
- When a class has two dimensions that vary independently, and both dimensions need to be extended.
- When a system does not want to use inheritance or when the number of system classes increases dramatically because of multi-level inheritance.
- When a system needs to add more flexibility between the abstract and concrete roles of components.
Extensions: Use in conjunction with the Adapter Pattern
In software development, sometimes the Bridge mode can be used in conjunction with the Adapter mode. When the interface of the realization role of the Bridge mode is inconsistent with the interface of the existing class, an adapter can be defined between the two to connect the two. The specific structure diagram is shown in the figure: