This is the 27th day of my participation in the More Text Challenge. For more details, see more text Challenge


👉 Design mode directory

What is Bridge Mode?

concept

The bridge pattern is defined as follows: ** Separates the abstraction from the implementation so that they can change independently. ** This concept is very abstract, meaning that the abstract part of the implementation is separated, so that the coupling between the abstract part and the implementation is reduced.

Abstraction: abstracting the common properties of complex objects. In the case of object orientation, it is abstracting the properties of various objects into a class.

Implementation: A concrete implementation of an abstraction.

The bridge pattern advocates composite relationships instead of multi-level inheritance relationships, which increase the number of implementation classes exponentially and increase the degree of class coupling.

Now want to design a product category, for example, millet and huawei have mobile phones and tablet, now want to design their products, if you want to through the way of inheritance to design, so the number of classes is m = n * 2 * 2, after if want to add a product, is 2 * 3, if add n and n brand products, the explosive growth of the number of classes will be.

If a combination of the way to design, only need to design mobile device class, tablet device class, Huawei brand class, Xiaomi brand class, and then specific implementation by them to the combination can be. As you can see, although you still need to create four classes, if you want to add one more product, you just need to write one more class.

advantages

  1. Abstract and implementation separation, improve the system scalability.
  2. It conforms to the principle of openness and closure.
  3. Improved class reuse.
  4. Reduce the degree of coupling between classes.

disadvantages

  1. You need an understanding of the system requirements, the ability to clear out classes for what needs to be done, and the ability to design for the abstraction layer, to be able to separate these classes in both dimensions.
  2. There are some limitations. The bridge pattern can only be used if the two dimensions are correctly identified.

In general, there are requirements for design.

The principle of

“+” means compliance, and “-” means noncompliance or irrelevant

The principle of Open the closed Single responsibility Di milt Replacement on the Richter scale Dependency inversion Interface segregation Synthesis of reuse
+ + + + +

Applicable Scenario (Motivation)

  1. There are multiple layers of inheritance. Using bridge mode to handle this situation can avoid the exponential growth of the system class.
  2. The implementation class extends in two dimensions that vary independently. This is what the bridge mode does.

How to implement

To implement the bridge pattern, you need the following:

  1. Abstract class: Defines an abstract class that makes a reference to the abstract interface and achieves a combination relationship with the abstract interface.
  2. Abstract Implementation classes of abstract classes: These classes are used to implement business logic.
  3. Abstract interface: Another dimension of abstraction layer that can be invoked by abstract classes.
  4. Abstract interface implementation class: Implements the business logic for this dimension.

Abstract is just an adjective I want to add, add a bit long, do not add and feel the meaning is not enough, think long can be omitted.

On the class diagram

In the code

Here’s an example of a mobile brand implementing the above

Abstraction

/** ** Mock Brand class * Created on 2021/5/30. **@author xuxiaobai
 */
public abstract class Abstraction {
    protected Implementor device;

    public Abstraction(Implementor device){
        this.device=device;
    }

    public abstract void operate(a);

}
Copy the code

Implementation of the abstract class: RefinedAbstraction

/** ** emulation mi brand class * Created on 2021/5/30. **@author xuxiaobai
 */
public class RefinedAbstraction extends Abstraction{


    public RefinedAbstraction(Implementor device) {
        super(device);
    }

    @Override
    public void operate(a) {
        System.out.print("this is xiaomi:"); device.operate(); }}Copy the code

Abstract interface: Implementor

/** * Analog Device class ** Created on 2021/5/30. **@author xuxiaobai
 */
public interface Implementor {

    void operate(a);
}
Copy the code

Abstract interface implementation class:

/** ** Analog Mobile Phone Set category * Created on 2021/5/30. **@author xuxiaobai
 */
public class ConcreteImplementor implements  Implementor{
    @Override
    public void operate(a) {
        System.out.println("Mobile phone"); }}Copy the code

Test class: BridgeTest

public class BridgeTest {
    public static void main(String[] args) {
        Abstraction phone = new RefinedAbstraction(new ConcreteImplementor());

        phone.operate();
        
    }
    /** * output: * this is xiaomi: mobile phone */
}
Copy the code

Here’s a simple example of a mobile phone brand,

conclusion

Bridge pattern mainly has complied with the dependency inversion principle, also is in advocate interface programming, provides an interface programming way, abide by the magnitude of the substitution principle, the single responsibility principle, the open closed principle, anyhow is basically satisfy most of the design principles, design pattern is a very good, also not too difficult to implement. We use it when we design the class structure.

When I have accumulated some development experience, I may automatically use this pattern. I also met a programmer who had not learned design patterns before, and he told me very disparagingly that it was useless to learn these, and THAT I could complete the requirements without learning them. Indeed, he used the bridge pattern in one of his class structures, and I was a bit of a hindsight. I am not saying that you should not learn design patterns, after all, design patterns are the experience of predecessors, learning can make us less detours.

In my view, multiple dimensions can also be used in the design of the abstraction layer. You only need to define multiple abstract interfaces. However, multiple dimensions are rare.

— — — — — — — — — — — — — — —

The more you know, the more you don’t know.

If you have questions about this article, please comment directly or send me a personal message. If you think my writing is good, you can also support me by clicking “like”

Without permission, shall not be reproduced!