Design pattern learning 09 (Java implementation) – bridging pattern

Time:2021-7-1

Write on the front

  • Take notes on learning design patterns
  • Improve the flexible use of design patterns

Learning address

https://www.bilibili.com/video/BV1G4411c7N4

https://www.bilibili.com/video/BV1Np4y1z7BU

Reference article

http://c.biancheng.net/view/1317.html

Project source code
https://gitee.com/zhuang-kang/DesignPattern

11. Bridge mode

11.1 definition and characteristics of bridge mode

The definition of bridge mode is as follows:Separate abstraction from implementation so that they can change independently.It is realized by using composition relation instead of inheritance relation, which reduces the coupling degree of the two variable dimensions of abstraction and implementation.

Through the above explanation, we can well feel that the bridge mode follows the Richter’s replacement principle and dependency inversion principle, and finally realizes the open close principle, which is closed to modification and open to extension.

The advantages of bridge mode are as follows:

  • Separation of abstraction and implementation, strong scalability
  • Comply with the principle of opening and closing
  • Conform to the principle of composite reuse
  • Its implementation details are transparent to customers

The disadvantages are:

Because the aggregation relationship is based on the abstraction layer, developers are required to design and program for the abstraction, and can correctly identify the two independent changing dimensions in the system, which increases the difficulty of understanding and designing the system.

11.2 structure and implementation of bridge mode

11.2.1 structure of bridge mode

  1. Abstract role: defines an abstract class and contains a reference to an implementation object.
  2. Extended abstraction role: it is a subclass of the abstract role, which implements the business methods in the parent class, and calls the business methods in the role through the combination relationship.
  3. Implementer role: defines the interface of implementer role, which can be called by extension Abstract role.
  4. Concrete implementer role: give the concrete implementation of the role interface.

11.2.2 code implementation

Brand abstracts characters

package com.zhuang.bridge;

/**
 * @Classname Brand
 *@ description brand
 * @Date 2021/3/22 9:31
 * @Created by dell
 */

public interface Brand {
    void open();

    void call();

    void close();
}

Vivo expands Abstract role

package com.zhuang.bridge;

/**
 * @Classname Vivo
 *@ description mobile phone brand realizes brand interface
 * @Date 2021/3/22 9:30
 * @Created by dell
 */

public class Vivo implements Brand {
    @Override
    public void open() {
        System. Out. Println ("vivo phone on");
    }

    @Override
    public void call() {
        System. Out. Println ("vivo phone call");
    }

    @Override
    public void close() {
        System. Out. Println ("vivo phone off");
    }
}

Xiaomi expands Abstract role

package com.zhuang.bridge;

/**
 * @Classname XiaoMi
 *@ description mobile phone brand realizes brand interface
 * @Date 2021/3/22 9:30
 * @Created by dell
 */

public class XiaoMi implements Brand {
    @Override
    public void open() {
        System. Out. Println ("Xiaomi mobile phone starts up");
    }

    @Override
    public void call() {
        System. Out. Println ("Xiaomi mobile phone calls");
    }

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

Phone

package com.zhuang.bridge;

/**
 * @Classname Phone
 *@ description mobile class abstract class
 * @Date 2021/3/22 9:30
 * @Created by dell
 */

public abstract class Phone {
    //Combined brand
    private Brand brand;

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

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

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

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

FoldedPhone

package com.zhuang.bridge;

/**
 * @Classname FoldedPhone
 *@ description folding mobile phone class
 * @Date 2021/3/22 9:31
 * @Created by dell
 */

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

    @Override
    public void open() {
        super.open();
        System. Out. Println ("folding phone style");
    }

    @Override
    public void call() {
        super.call();
        System. Out. Println ("folding phone style");
    }

    @Override
    public void close() {
        super.close();
        System. Out. Println ("folding phone style");
    }
}

UpRightPhone

package com.zhuang.bridge;

/**
 * @Classname UpRightPhone
 *@ description mobile phone
 * @Date 2021/3/22 9:33
 * @Created by dell
 */

public class UpRightPhone extends Phone {
    //Constructors
    public UpRightPhone(Brand brand) {
        super(brand);
    }

    @Override
    public void open() {
        super.open();
        System. Out. Println ("vertical phone style");
    }

    @Override
    public void call() {
        super.call();
        System. Out. Println ("vertical phone style");
    }

    @Override
    public void close() {
        super.close();
        System. Out. Println ("vertical phone style");
    }
}

Client

package com.zhuang.bridge;

/**
 * @Classname Client
 *@ description client class
 * @Date 2021/3/22 9:30
 * @Created by dell
 */

public class Client {
    public static void main(String[] args) {
        Phone phone1 = new FoldedPhone(new XiaoMi());
        phone1.open();
        phone1.call();
        phone1.close();
        System.out.println("==============================");
        Phone phone2 = new UpRightPhone(new Vivo());
        phone2.open();
        phone2.call();
        phone2.close();
    }
}

Design pattern learning 09 (Java implementation) - bridging pattern

11.3 application scenarios of bridge mode

When there are two or more changing dimensions in a class, the bridge mode can decouple these changing dimensions and make the high-level code architecture stable.

  1. When a class has two independently changing dimensions, and both dimensions need to be extended.
  2. When a system does not want to use inheritance or the number of system classes increases sharply due to multi-level inheritance.
  3. When a system needs to add more flexibility between abstract role and concrete role of component.

Write at the end

  • If my article is useful to you, please give me some, thank you!
  • If you have any questions, please point them out in the comments area!