Bridging mode of design mode

1. Introduction to bridging mode

  • Bridging mode

    • Bridging is used to decouple abstraction from realization, so that they can change independently. This type of design pattern belongs to structural pattern, which realizes the decoupling of abstraction and realization by providing a bridge structure between them.
    • The implementation of this interface pattern is independent of the class as an interface. These two types of classes can be structurally changed without affecting each other.
  • intention

    • Separate the abstract part from the implementation part so that they can change independently.
  • Main solution

    • When there are many possible changes, using inheritance will cause class explosion and inflexible expansion.
  • When to use

    • The implementation system may have multiple angle classifications, and each angle may change.
  • How to solve

    • Separate this multi angle classification, let them change independently and reduce the coupling between them.
  • critical code

    • Abstract classes depend on implementation classes
  • advantage

    • Separation of abstraction and Implementation
    • Excellent expansion ability, in line with the opening and closing principle
    • Make details transparent to customers
  • shortcoming

    • The introduction of bridging mode will increase the difficulty of system understanding and design. Because the aggregation association relationship is based on the abstraction layer, developers are required to design and program for the abstraction.
  • Usage scenario

    • If a system needs to add more flexibility between the abstract role and concrete role of components, and avoid establishing static inheritance relationship between the two levels, they can establish an association relationship at the abstract level through bridging mode.
    • The bridging mode is especially suitable for those systems that do not want to use inheritance or the number of system classes increases sharply due to multi-level inheritance.
    • A class has two independently changing dimensions, and both dimensions need to be extended.
  • matters needing attention

    • For two independently varying dimensions. Bridging mode is the most appropriate.

When learning knowledge, sometimes many languages are more abstract. If you read more blogs and learn, you will gradually understand them. (the most important thing is persistence)

2. Bridge mode description

  • What is the original intention of bridge mode design

    You see, for example, we now want to create a mobile phone class. Mobile phones have different brands and colors. If we create mobile phones with specific colors of a specific brand by inheritance, we now have Huawei mobile phones, apple mobile phones and Samsung mobile phones. The colors are black, yellow and red. If we directly inherit the mobile phone class, there will be 9 subcategories, Black Huawei mobile phones, yellow Huawei mobile phones, red Huawei mobile phones, black apple mobile phones... In this way, there will be many categories (i.e. class explosion problem), and the expansion is extremely inflexible. However, if we extract the color, there is a color instance in Huawei mobile phone. If you want to set any color, you can transfer the corresponding color object. In this way, the number of classes will be reduced (Huawei mobile phone, apple mobile phone and Samsung mobile phone), and the color object can be transferred according to the color you need, which is very flexible. It feels very similar to the adapter mode, but the difference between the two is the use time. The bridge mode should be used when the design is not completed and is being designed, while the adapter mode should be used when the design is completed and when you want to expand the function.

  • Components of bridging mode

    • Abstraction: defines an abstract class and contains a reference to the implementation object.
    • Refined Abstraction: it is a subclass of the abstract role, which implements the business methods in the parent class and calls the business methods in the abstract role through the composition relationship.
    • Implementer: defines the interface to implement the role, which can be called by the extended abstract role.
    • Concrete implementer: give the concrete implementation of the implementation role interface.
  • example

    Let's take an example of mobile phones. Mobile phones have different brands and colors. In the following examples, Huawei, apple and Samsung are the brands, and the colors are black, yellow and red.

    In this example, the abstract roles are mobile phones, the extended abstract roles are Huawei, apple and Samsung, the realized roles are colors, and the concrete realized roles are black, yellow and red.

  • Structure diagram

Realize role

/**
 * Materialized role: materialized role is the interface of materialized role
 * colour
 */
public interface Color {
    public void useColor();
}

Concrete implementation role

/**
 * Specific role A: Black
 */
public class BlackColor implements Color{

    @Override
    public void useColor(){
        System.out.println("black");
    }
}
/**
 * Specific role B: yellow
 */
public class YellowColor implements Color{

    @Override
    public void useColor(){
        System.out.println("yellow");
    }
}
/**
 * Specific implementation role C: Red
 */
public class RedColor implements Color{

    @Override
    public void useColor(){
        System.out.println("gules");
    }
}

Abstract role

/**
 * Abstract role
 * mobile phone
 */
public abstract class Phone {

    protected Color color;
    protected Phone(Color color){
        this.color = color;
    }

    // Method of mobile phone
    abstract public void run();
}

Extended abstract role

/**
 * Extended role A: Huawei Mobile
 */
public class HuaweiPhone extends Phone{

    public HuaweiPhone(Color color){
        super(color);
    }

    @Override
    public void run(){
        color.useColor();
        System.out.println("Huawei Mobile");
    }
}
/**
 * Extended role B: Apple Mobile
 */
public class ApplePhone extends Phone{

    public ApplePhone(Color color){
        super(color);
    }

    @Override
    public void run(){
        color.useColor();
        System.out.println("iPhone");
    }
}
/**
 * Extended role C: Samsung Mobile
 */
public class SamsungPhone extends Phone {

    public SamsungPhone(Color color){
        super(color);
    }

    @Override
    public void run() {
        color.useColor();
        System.out.println("Samsung mobile phone");
    }
}

Test class

@Test
public void test01(){
    // Huawei mobile phone is set to black
    Phone phone1 = new HuaweiPhone(new BlackColor());
    // Apple phone set to yellow
    Phone phone2 = new ApplePhone(new YellowColor());
    // Samsung mobile phone is set to red
    Phone phone3 = new SamsungPhone(new RedColor());

    phone1.run();
    phone2.run();
    phone3.run();
}

result

black
 Huawei Mobile
 yellow
 iPhone
 gules
 Samsung mobile phone

3. Summary

  1. Bridging mode is to separate the abstract part from the implementation part, so that they can change independently.
  2. Bridge mode mainly solves the problem of class explosion and inflexible expansion.
  3. The bridge mode consists of four parts:
    • Abstraction: defines an abstract class and contains a reference to the implementation object.
    • Refined Abstraction: it is a subclass of the abstract role, which implements the business methods in the parent class and calls the business methods in the abstract role through the composition relationship.
    • Implementer: defines the interface to implement the role, which can be called by the extended abstract role.
    • Concrete implementer: give the concrete implementation of the implementation role interface.

Analysis of bridge mode and adapter mode

  • The bridge mode is very similar to the adapter mode, but the bridge mode is used in the design, while the adapter mode is used after the design is completed to check for leaks and make up deficiencies,

Keywords: Design Pattern

Added by carsonk on Thu, 03 Feb 2022 08:49:14 +0200