Writing in the front

  • Take notes on learning design patterns
  • Improve flexibility in the use of design patterns

Learning to address

https://www.bilibili.com/vide…

https://www.bilibili.com/vide…

Refer to the article

http://c.biancheng.net/view/1…

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

11. Bridge mode

11.1 Definition and characteristics of bridging patterns

The Bridge pattern is defined as follows: it separates abstraction from implementation so that they can vary independently. It is implemented by using composition relation instead of inheritance relation, thus reducing the coupling degree between the two variable dimensions of abstraction and implementation.

Through the above explanation, we can feel that the bridge mode follows the Richter principle of substitution and the principle of dependency inversion, and finally realizes the open-closed principle, which is closed to modification and open to expansion.

The advantages of the Bridge mode are:

  • Separation of abstraction and implementation, strong ability to expand
  • Comply with the open closed principle
  • Conformed to the principle of composite reuse
  • Its implementation details are transparent to the customer

The disadvantage is that:

Since the aggregation relationship is based on the abstraction layer, developers are required to design and program for abstraction and 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 the bridge pattern

11.2.1 Structure of Bridging Mode

  1. Abstraction roles: Defines abstract classes and contains a reference to the implemented object.
  2. Expansion of the Refined Abstraction role: A subclass of the abstract role that implements the business methods in the parent class and implements the business methods in the role through composite relationship calls.
  3. Implementor roles: Define an interface for implementing roles that can be invoked by extended abstract roles.
  4. Concrete Implementor roles: Give the Concrete Implementor 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 characters

package com.zhuang.bridge; /** * @classname Vivo * @description * @date 2021/3/22 9:30 * @created by dell */ public class Vivo Implements Brand {@Override public void open() {System.out.println("Vivo "); } @override public void call() {System.out.println("Vivo phone call ");} @override public void call() {System.out.println("Vivo phone call "); } @Override public void close() {System.out.println("Vivo phone shuts down "); }}

Xiaomi expands abstract characters

package com.zhuang.bridge; /** * @ClassName Xiaomi * @Description * @Date 2021/3/22 9:30 * @Created by Dell */ public class Xiaomi Implements Brand {@Override public void open() {System.out.println(" omi power on "); } @Override public void call() {System.out.println(" omi phone call "); } @Override public void close() {System.out.println(" omi's phone is off "); }}

Phone

package com.zhuang.bridge; /** * @classname Phone * @description Phone * @date 2021/3/22 9:30 * @created by dell */ public class Phone { // Private Brand Brand; // constructor 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 FoldedPhone * @Date 2021/3/22 9:31 * @Created by Dell */ public class FoldedPhone Extends Phone {// public foldedPhone (Brand Brand) {super(Brand); } @Override public void open() { super.open(); System.out.println(" Fold phone style "); } @Override public void call() { super.call(); System.out.println(" Fold phone style "); } @Override public void close() { super.close(); System.out.println(" Fold phone style "); }}

UpRightPhone

package com.zhuang.bridge; /** * @classname UpRightPhone * @description UpRightPhone * @date 2021/3/22 9:33 * @created by dell */ public class UpRightPhone extends Phone {// public UpRightPhone(Brand Brand) {super(Brand); } @Override public void open() { super.open(); System.out.println(" Upright Phone Style "); } @Override public void call() { super.call(); System.out.println(" Upright Phone Style "); } @Override public void close() { super.close(); System.out.println(" Upright Phone Style "); }}

Client

package com.zhuang.bridge;

/**
 * @Classname Client
 * @Description  客户端类
 * @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();
    }
}

11.3 Application scenarios of bridge mode

When there are two or more dimensions of variation within a class, bridging patterns can be used to decouple those dimensions and stabilize the high-level code architecture.

  1. When a class has two independently varying 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 dramatically because of multiple levels of inheritance.
  3. When a system needs to add more flexibility between the abstract and concrete roles of the component.

Write in the last

  • If my article is useful to you, please click 👍, thank you!
  • Let me know in the comments section if you have any questions! 💪