Bridging mode of design mode

Keywords: Java Design Pattern

Basic introduction

  1. Bridging mode means that the implementation and abstraction are placed in two different class levels, so that the two levels can be changed independently
  2. It is a structural design mode
  3. Based on the minimum design principle of classes, different classes assume different responsibilities by using encapsulation, aggregation and inheritance. Its main feature is to separate abstraction from behavior implementation, so as to maintain the independence of each part and deal with their function expansion

Role:

  1. Client: the caller of bridge mode
  2. Abstract class: it maintains the implementer / that is, its implementation class. The two are an aggregate relationship, and abstract acts as a bridge class
  3. RefinedAbstraction: is a subclass of the Abstraction abstract class
  4. Implementor: the interface of the behavior implementation class

code

Scenario: multiple brand mobile phones have different styles, and the styles correspond to different brand mobile phones

Defines the interface of the behavior implementation class

public interface Brand {

    void call();
    void open();
    void close();
}

It defines the operation behavior of the mobile phone and the implementation interface of the mobile phone

public class Vivo implements Brand{
    @Override
    public void call() {
        System.out.println("vivo Phone call");
    }

    @Override
    public void open() {
        System.out.println("vivo Power on");
    }

    @Override
    public void close() {
        System.out.println("vivo Shut down");
    }
}

public class XiaoMi implements Brand{
    @Override
    public void call() {
        System.out.println("Millet phone call");
    }

    @Override
    public void open() {
        System.out.println("Millet phone on");
    }

    @Override
    public void close() {
        System.out.println("Xiaomi mobile phone off");
    }
}

Define abstract classes, aggregate behavior, and operate interfaces of classes, which is also called bridges

public abstract class Phone {

    private Brand brand;

    public Phone(Brand brand){
        this.brand = brand;
    }

    protected void open(){
        brand.open();
    }

    protected void close(){
        brand.close();
    }

    protected void call(){
        brand.call();
    }
}

Define the implementation of the bridge

public class FoldedPhone extends Phone{
    public FoldedPhone(Brand brand) {
        super(brand);
    }

    @Override
    public void open(){
        super.open();
        System.out.println("Folding style phone");
    }

    @Override
    public void close(){
        super.close();
        System.out.println("Folding style phone");
    }

    @Override
    public void call(){
        super.call();
        System.out.println("Folding style phone");
    }
}
public class UpRightPhone extends Phone{
    public UpRightPhone(Brand brand) {
        super(brand);
    }

    @Override
    public void open(){
        super.open();
        System.out.println("Upright style mobile phone");
    }

    @Override
    public void close(){
        super.close();
        System.out.println("Upright style mobile phone");
    }

    @Override
    public void call(){
        super.call();
        System.out.println("Upright style mobile phone");
    }
}

Client call

public class Client {
    public static void main(String[] args) {
        // Get a foldable phone, style and brand
        FoldedPhone foldedPhone = new FoldedPhone(new XiaoMi());
        foldedPhone.open();
        foldedPhone.call();
        foldedPhone.close();

        System.out.println("------------");
        foldedPhone = new FoldedPhone(new Vivo());
        foldedPhone.open();
        foldedPhone.call();
        foldedPhone.close();

        System.out.println("------------");
        // erect
        UpRightPhone upRightPhone = new UpRightPhone(new XiaoMi());
        upRightPhone.open();
        upRightPhone.call();
        upRightPhone.close();

    }
}

In this mode, we only need to add the implementation class of the interface to add a new mobile phone

Precautions and details

  1. It realizes the separation of abstract part and implementation part, which greatly provides the flexibility of the system and makes the abstract part and implementation part independent, which is helpful for the hierarchical design of the system, so as to produce a better structured system
  2. For the high-level part of the system, you only need to know the interface between the abstract part and the implementation part, and the other parts are completed by specific business
  3. The bridge mode replaces the multi-layer inheritance scheme, which can reduce the number of subclasses and reduce the management and maintenance cost of the system
  4. The introduction of bridging mode increases 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
  5. The bridging mode requires to correctly identify two independently changing dimensions in the system, so its scope of use has certain limitations, that is, it needs such an application scenario

Posted by ksimpkins on Tue, 14 Sep 2021 21:05:41 -0700