Design Mode (Java): Bridge Mode

Previous: Design Patterns (Java): Adapter Pattern

Nine, bridge mode

example requirement

When we implement operation programming for different brands of different mobile phone types, as shown in the figure:

insert image description here

Then its corresponding class diagram is

insert image description here

Traditional way to solve needs analysis:

  • The problem of scalability is that if we need to increase the style of mobile phones (flip cover), then we need to increase the types of each brand. If we add a mobile phone brand, we also need to increase it under each mobile phone style.
  • Violation of the single responsibility principle: when we add mobile phone styles, we need to add all brands of mobile phones at the same time, which increases the cost of code maintenance.
  • Solution: use bridge mode

9.1 Basic introduction to bridge mode

  • Bridge mode (Bridge mode) refers to: put the implementation and abstraction in two different class levels, so that the two levels can be changed independently.
  • is a structural design pattern
  • The Bridge pattern is based on the minimal design principles of classes , and allows different classes to assume different responsibilities by using behaviors such as encapsulation, aggregation, and inheritance. Its main feature is to separate abstraction from implementation, so that it can maintain the independence of each part and deal with their functional expansion.

Bridge Mode Schematic

insert image description here

Analysis :

  • Cilent class: the caller of the bridge mode
  • Abstract class (Abstraction): maintains Implementor / its implementation class ConcreteImplementorA/B, the two are aggregation relationships, and Abstraction acts as a bridge (bridge class)
  • RefinedAbstraction: is a subclass of Abstraction abstract class
  • Implementor: the interface of the behavior implementation class
  • ConcreteImplementorA: the concrete implementation class of the behavior

9.2 Bridge mode to solve the demand

Use the class diagram corresponding to the bridge mode

insert image description here

Code

brand interface

/**
 * @author cVzhanshi
 * @create 2023-06-27 17:30
 */
public interface Brand {
    
    
    void open();

    void call();

    void close();
}

Brand interface implementation class

@Slf4j
public class Huawei implements Brand{
    
    
    @Override
    public void open() {
    
    
        log.info("Huawei开机啦");
    }

    @Override
    public void call() {
    
    
        log.info("Huawei打电话啦");
    }

    @Override
    public void close() {
    
    
        log.info("Huawei关机啦");
    }
}


@Slf4j
public class Mi implements Brand{
    
    
    @Override
    public void open() {
    
    
        log.info("Mi开机啦");
    }

    @Override
    public void call() {
    
    
        log.info("Mi打电话啦");
    }

    @Override
    public void close() {
    
    
        log.info("Mi关机啦");
    }
}

Mobile phone abstract class

/**
 * @author cVzhanshi
 * @create 2023-06-27 17:32
 */
public abstract class Phone {
    
    

    // 品牌
    private Brand brand;

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

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

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

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

style subclasses of abstract classes

/**
 * @author cVzhanshi
 * @create 2023-06-27 17:34
 */

@Slf4j
public class AnJian extends Phone{
    
    
    public AnJian(Brand brand) {
    
    
        super(brand);
    }

    @Override
    protected void open() {
    
    
        super.open();
        log.info("按键手机");
    }

    @Override
    protected void call() {
    
    
        super.call();
        log.info("按键手机");
    }

    @Override
    protected void close() {
    
    
        super.close();
        log.info("按键手机");
    }
}


@Slf4j
public class ChuPin extends Phone{
    
    
    public ChuPin(Brand brand) {
    
    
        super(brand);
    }

    @Override
    protected void open() {
    
    
        super.open();
        log.info("触屏手机");
    }

    @Override
    protected void call() {
    
    
        super.call();
        log.info("触屏手机");
    }

    @Override
    protected void close() {
    
    
        super.close();
        log.info("触屏手机");
    }
}

Client invokes bridge mode

/**
 * @author cVzhanshi
 * @create 2023-06-27 17:37
 */

@Slf4j
public class Client {
    
    
    public static void main(String[] args) {
    
    
        Phone phone1 = new AnJian(new Mi());
        phone1.open();
        phone1.call();
        phone1.close();
        log.info("=================================");
        Phone phone2 = new AnJian(new Huawei());
        phone2.open();
        phone2.call();
        phone2.close();
        log.info("=================================");
        Phone phone3 = new ChuPin(new Huawei());
        phone3.open();
        phone3.call();
        phone3.close();
    }
}

// 输出结果
17:40:39.229 [main] INFO cn.cvzhanshi.design.bridge.Mi - Mi开机啦
17:40:39.233 [main] INFO cn.cvzhanshi.design.bridge.AnJian - 按键手机
17:40:39.234 [main] INFO cn.cvzhanshi.design.bridge.Mi - Mi打电话啦
17:40:39.234 [main] INFO cn.cvzhanshi.design.bridge.AnJian - 按键手机
17:40:39.234 [main] INFO cn.cvzhanshi.design.bridge.Mi - Mi关机啦
17:40:39.234 [main] INFO cn.cvzhanshi.design.bridge.AnJian - 按键手机
17:40:39.234 [main] INFO cn.cvzhanshi.design.bridge.Client - =================================
17:40:39.235 [main] INFO cn.cvzhanshi.design.bridge.Huawei - Huawei开机啦
17:40:39.235 [main] INFO cn.cvzhanshi.design.bridge.AnJian - 按键手机
17:40:39.235 [main] INFO cn.cvzhanshi.design.bridge.Huawei - Huawei打电话啦
17:40:39.235 [main] INFO cn.cvzhanshi.design.bridge.AnJian - 按键手机
17:40:39.235 [main] INFO cn.cvzhanshi.design.bridge.Huawei - Huawei关机啦
17:40:39.235 [main] INFO cn.cvzhanshi.design.bridge.AnJian - 按键手机
17:40:39.235 [main] INFO cn.cvzhanshi.design.bridge.Client - =================================
17:40:39.235 [main] INFO cn.cvzhanshi.design.bridge.Huawei - Huawei开机啦
17:40:39.235 [main] INFO cn.cvzhanshi.design.bridge.ChuPin - 触屏手机
17:40:39.235 [main] INFO cn.cvzhanshi.design.bridge.Huawei - Huawei打电话啦
17:40:39.235 [main] INFO cn.cvzhanshi.design.bridge.ChuPin - 触屏手机
17:40:39.236 [main] INFO cn.cvzhanshi.design.bridge.Huawei - Huawei关机啦
17:40:39.236 [main] INFO cn.cvzhanshi.design.bridge.ChuPin - 触屏手机

Analysis : If you use the bridge mode to complete the requirements, then if you want to add a style or a brand, you only need to add a class, and you don't need to modify other classes.

9.3 Notes and Details for Bridge Mode

  • The separation of abstraction and implementation is achieved , which greatly provides the flexibility of the system, making the abstraction and implementation independent, which helps the system to perform hierarchical design, resulting in a better structured system.
  • For the high-level part of the system, it is only necessary to know the interface between the abstract part and the implementation part, and the other parts are completed by specific services.
  • The bridge mode replaces the multi-layer inheritance scheme, which can reduce the number of subclasses and reduce the management and maintenance costs of the system.
  • The introduction of the bridge mode increases the difficulty of system understanding and design. Since the aggregation relationship is established on the abstract layer, developers are required to design and program for abstraction.
  • The bridge mode requires the correct identification of two independently changing dimensions in the system , so its scope of use has certain limitations, that is, such application scenarios are required.

Application Scenario

  • JDBC driver
  • bank transfer system
    • Transfer classification: online transfer, counter transfer, AMT transfer
    • Types of transfer users: ordinary users, silver card users, gold card users.
  • message management
    • Message type: instant message, delayed message
    • Message classification: mobile phone text messages, email messages, QQ messages...

Guess you like

Origin blog.csdn.net/qq_45408390/article/details/131482879