"Design Patterns" Bridge Pattern

"Design Patterns" Basic Principles of Design Patterns
"Design Patterns" Singleton Patterns "
Design Patterns" Factory Patterns
"Design Patterns" Prototype Patterns "
Design Patterns" Builder Patterns "Design Patterns
" Adapter Patterns "Design
Patterns" Bridge Patterns
"Design Patterns" "Decorator Mode
" Design Mode" Composition Mode
"Design Mode" Appearance Mode "Design Mode"
Flyweight Mode "Design Mode" Proxy Mode "
Design Mode
" Template Method Mode
"Design Mode" Command Mode

"Design Patterns" Bridge Pattern


Definition :

  • The bridge mode is a structural design mode based on the minimal design principles of classes. By using behaviors such as encapsulation, aggregation, and inheritance, different classes can assume different responsibilities, and the implementation and abstraction are placed in two different class levels. Independence of the parts and their functional extensions.

Notes on bridge mode :

  • The separation of abstraction and implementation is realized, which greatly improves the flexibility of the system, makes abstraction and implementation independent, helps the system to carry out hierarchical design, and produces a better structured system .
  • The bridge mode replaces the multiple inheritance scheme, reduces the number of subclasses, and reduces the management and maintenance costs of the system .
  • Since the aggregation relationship is established on the abstract layer, developers are required to design and program against the abstraction .
  • The bridge mode requires correct identification of two independently changing dimensions (abstract and implementation) in the system , so its scope of use has certain limitations, and such application scenarios are required.
  • The bridge 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.

The UML class diagram for the bridge pattern is as follows :

insert image description here

  • Client class: the caller of the bridge mode
  • Abstraction: Act as a bridging class and maintain the Implementor interface, the two are aggregation relationships
  • Implementor: the interface of the behavior implementation class
  • ConcreteImplementorA/B: the concrete implementation class of the behavior

It can be seen from the UML diagram that the relationship between abstract classes and interfaces is aggregated, and it is actually the relationship between calling and being called.

There is a need to program the operations (switching on and off, surfing the Internet, and making calls) of different brands of different mobile phone types. Use the traditional way to solve this problem, and analyze its UML class diagram as follows:

insert image description here

When using the traditional method to solve this problem, there is a shortcoming that is the scalability problem (class explosion) . If we increase the style of mobile phones (rotary), we need to increase the classes of mobile phones of various brands. If we want to add a mobile phone brand, we also need to add Added under each mobile phone style category.

Therefore, in order to overcome this shortcoming, we decided to use the bridge mode to improve the traditional method, so that the program has better scalability and is conducive to program maintenance. The UML class diagram analysis is as follows:

insert image description here

Brandinterface

public interface Brand {
    
    
    void open();
    void close();
    void call();
}

kindPhone

public abstract class Phone {
    
    
    private Brand brand;

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

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

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

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

kindUpRight

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

    @Override
    public void open() {
    
    
        super.open();
        System.out.println("打开直立式手机");
    }

    @Override
    public void close() {
    
    
        super.close();
        System.out.println("关闭直立式手机");
    }

    @Override
    public void call() {
    
    
        super.call();
        System.out.println("直立式手机呼叫");
    }
}

Mobile phone class A, same as class B

public class A implements Brand {
    
    
    @Override
    public void open() {
    
    
        System.out.println("A手机开机");
    }

    @Override
    public void close() {
    
    
        System.out.println("A手机关机");
    }

    @Override
    public void call() {
    
    
        System.out.println("A手机呼叫");
    }
}

kindClient

public class Client {
    
    
    public static void main(String[] args) {
    
    
        Phone phone = new UpRight(new A());
        phone.open();
    }
}

In addition, the bridging mode is also applied in the JDBC source code, the Driver interface of JDBC, and the Driver interface implementation classes of MySQL and Oracle below.

insert image description here

Guess you like

Origin blog.csdn.net/weixin_43252521/article/details/127358294