Java design pattern -- Bridge Pattern

1, Introduction

The 23 design modes are roughly divided into three categories:

There are 5 kinds of creation modes: factory method mode, abstract factory mode, singleton mode, prototype mode and builder mode.

7 types (structural mode): adapter mode, decorator mode, agent mode, appearance mode, bridge mode, combination mode and sharing mode.

11 kinds of behavioral modes: strategy mode, template method mode, observer mode, iteration sub mode, responsibility chain mode, command mode, memo mode, status mode, visitor mode, mediator mode and interpreter mode.

Behavior type can be divided by the relationship between classes:

Basic introduction to bridge mode:

  • Bridge pattern refers to putting the implementation and abstraction in two different class levels, so that the two levels can be changed independently. It is a structural design pattern.
  • Bridge pattern is 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 implementation, so as to maintain the independence of each part and deal with their function expansion.

2, Bridging mode

1. Schematic diagram of bridge mode

Schematic class diagram description:

  • Client class: caller of bridge mode

  • Abstract class: it maintains the Implementor, that is, its implementation class ConcreteImplementorA/B. the two are aggregation relations, and the Abstraction acts as a bridge

  • RefinedAbstraction: is a subclass of the Abstraction abstract class

  • Implementor: the interface of the behavior implementation class

  • ConcreteImplementorA /B: concrete implementation class of behavior

  • The abstract classes and interfaces here are aggregate relationships, which are also called and called relationships

3, Specific needs

1. Mobile phone operation problems

Now we can realize operation programming for different brands of different mobile phone types, such as turning on, turning off, surfing the Internet, making phone calls, etc. As shown in the figure:

2. Traditional programmes

Thought analysis - class diagram

Traditional mode analysis

  • Scalability problem (class explosion). If we add more mobile phone styles (rotary), we need to add mobile phone classes of various brands. Similarly, if we add a mobile phone brand, we also need to add mobile phone classes under various mobile phone style classes
  • In violation of the principle of single responsibility, when we add mobile phone styles, we should add all brands of mobile phones at the same time, which increases the cost of code maintenance
  • Solution - use bridge mode

3. Bridging mode scheme

Bridge mode is used to improve the traditional way and make the program more scalable

Thought analysis - class diagram

Concrete implementation

// Interface brand java
public interface Brand {
	void open();
	void close();
	void call();
}

// Vivo.java/Xiaomi.java
public class Vivo implements Brand {
	@Override
	public void open() { System.out.println("vivo Mobile phone on"); }
	@Override
	public void close() { System.out.println("vivo Mobile phone off"); }
	@Override
	public void call() { System.out.println("vivo Phone call"); }
}

// Phone.java
public abstract class Phone {
	private Brand brand;//polymerization
	
	public Phone(Brand brand) {
		this.brand = brand;
	}
	
	protected void open() {
		this.brand.open();
	}
	protected void close() {
		this.brand.close();
	}
	protected void call() {
		this.brand.call();
	}
}

// FoldedPhone.java/UprightPhone.java
// Foldable mobile Phone inherits the abstract class Phone
public class FoldedPhone extends Phone {
	//The constructor must have: pass brand into
	public FoldedPhone(Brand brand) {
		super(brand);
	}
	
	public void open() {
		super.open();
		System.out.println("Folding style phone");
	}
	public void close() {
		super.close();
		System.out.println("Folding style phone");
	}
	public void call() {
		super.call();
		System.out.println("Folding style phone");
	}
}

4, Precautions and details

  • 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.

  • 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 the specific business.

  • Bridging mode replaces multi-layer inheritance scheme, which can reduce the number of subclasses and reduce the management and maintenance cost of the system.

  • The introduction of bridging mode increases the difficulty of system understanding and design. Since the aggregation association relationship is based on the abstraction layer, developers are required to design and program for abstraction (which are in the abstraction layer and which are in the implementation layer)

  • The bridging mode requires to correctly identify two independently changing dimensions (abstraction, and Implementation) in the system. Therefore, its scope of use has certain limitations, that is, it needs such an application scenario.

5, Application scenario

  • The bridging 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

  • Common application scenarios

    • JDBC Driver

    • Bank transfer system

      • Transfer classification: online transfer, counter transfer, AMT transfer (Abstract)
      • Transfer user type: ordinary user, silver card user, gold card user... (implemented)
    • Message management

      • Message type: instant message, delayed message (abstract layer)

      • Message classification: SMS, email, QQ... (implementation layer)

Keywords: Java Design Pattern

Added by unknownproto on Thu, 16 Dec 2021 18:19:48 +0200