preface

This week’s project includes this article on design patterns. It has been more than half a month since the last lecture. It is a rather boring thing to discover the way of learning design patterns, but I think it is very impressive if I can explain them in an interesting way. Therefore, I hope to explain these things in a more popular language while learning.

Today’s lesson is the fourth type of structural pattern —- bridging pattern.

Introduction to bridge mode

1.1 an overview of the

Bridging pattern problems are often used to solve multi-dimensional problems. For example, in this classic picture, on one side of the river are computers, TVS, radios and other equipment, while on the other side are various remote controls. This actually corresponds to two dimensions, and communication is completed through a high bridge. The so-called “bridge” connection is the extracted interface connection, calling the interface of dimension two in dimension one and passing the interface into the constructor of dimension one.

Next, we continue to talk about the case of hand cake in decorator mode as an example to start the explanation of bridge mode.

Assume that the aunt who sells hand-scratched cake currently has shandong multi-grain pancake and original hand-scratched cake. Now, in order to expand sales, offline stores and Taobao e-commerce channels are adopted, which are two dimensions including selling products and selling channels.

However, this definition obviously has a disadvantage, that is, for each additional product to be sold, there may be many more products to be sold through various channels. Similarly, for each additional sales channel, there will be many more channels to sell products. No matter which dimension is added, more classes need to be created. Therefore, in a system with multiple dimensions that may change, inheritance will cause class explosion and inflexible. Each time you add a concrete implementation to a dimension, you need to add multiple subclasses.

Bridge Patten, a structural design pattern, can split a class or series of closely related classes into two separate hierarchies of abstraction and implementation that can be used separately at development time.

Definition quoted from Baidu Encyclopedia:

Separate the abstract part from its implementation part so that they can be changed independently.

The definition of Baidu Encyclopedia is relatively difficult to understand, we only need to remember that the characteristics of the bridge mode will be separated from the abstraction and implementation, and establish dependencies in the abstraction layer, so that the implementation classes on the two dimensions can carry out their own changes without affecting each other, which has very good scalability.

1.2 Bridge mode structure

Bridge mode contains the following roles:

  • Abstract role: Defines an abstract class and contains a reference to the implementation object.
  • Extended Abstract role: Is a subclass of an abstract role that implements the business methods in the parent class and implements the business methods in the role through composite relationship calls.
  • Implemented roles: Defines interfaces for implemented roles that can be invoked by extended abstract roles
  • Concrete implementation role: Provides the concrete implementation of the implementation role interface.

Ii. Case explanation

Now, let’s begin the demo restore of the previous case.

Implement roles:

@author xiaolei * @date 2021/7/17 16:02 **/ public interface SaleModel {void sale(String product); }Copy the code

Offline store sales — Specific roles:

public class OfflineSafe implements SaleModel { public void sale(String product) { System.out.println(" +product "); }}Copy the code

Online Taobao sales — specific realization of the role

Public class TaoBaoSafe implements SaleModel {public void sale(String product) {system.out.println (" TaoBaoSafe implements SaleModel {public void sale(String product) {system.out.println (" TaoBaoSafe implements SaleModel "+product); }}Copy the code

Product categories:

Public abstract class Product {protected SaleModel SaleModel; public Product(SaleModel saleModel){ this.saleModel=saleModel; } public abstract void make(String name); }Copy the code

Hand pie — (Abstract character subclass)

public class HandPancke extends Product { public HandPancke(SaleModel saleModel) { super(saleModel); } public void make(String name) { saleModel.sale(name); }}Copy the code

Shandong coarse grain pancake class — (Abstract role subclass)

public class ShandongPancake extends Product { public ShandongPancake(SaleModel saleModel) { super(saleModel); } public void make(String name) { saleModel.sale(name); }}Copy the code

Output result:

Third, summary

3.1 the advantages and disadvantages

3.1.1 advantages
  • Separation of abstraction and implementation: This is the main characteristic of the bridge pattern and the main reason inheritance is avoided. Using the bridge pattern, the abstraction and implementation are decoupled, so that the changes of the two do not affect the other side, and the system scalability is greatly enhanced.
  • Excellent extension ability: The bridge pattern is developed to solve the coupling of multiple independently changing dimensions, and its aggregation relationship of high-level modules has been determined. Therefore, no matter the abstract change or the implementation change, as long as it is extended, the high-level code changes in disorder, strictly abide by the dependency inversion principle.
  • Implementation details are transparent to the customer: stable architecture (encapsulation) is built through aggregation relationships in high-level modules due to the bridging pattern. As a result, the customer only needs to interact with the high-level modules, with no concern for the details of abstraction and implementation.
3.1.2 shortcomings
  • The bridge mode requires developers to design and program the abstraction layer because the aggregation association relationship is established at the abstraction layer, which increases the difficulty of system understanding and design.

3.2 Application Scenarios

A common use of the bridge pattern is to replace inheritance, which is inherently intrusive (superclass code invades subclasses) and makes subclasses bloated, so composition/aggregation is preferred.

The bridging pattern is characterized by the separation of abstraction from implementation, so it fits

  • A class has two or more dimensions that vary independently, and both dimensions need to be extended
  • Scenarios where inheritance is not desired or applicable

3.3 Mode Comparison

  • Differences between bridge mode and Abstract Factory mode:

The concept of this dimension, which we also mentioned in the abstract factory, is related to multiple dimensions. However, the abstract factory mode is more concerned with the creation of the object, which is the creation mode, while the bridge mode is more concerned with the action or function of the object after creation, so the difference is here.

  • Differences between bridge mode and decorator mode:

When drawing UML, I found that these two are very similar. They can avoid class explosion, transform inheritance into composition, and reduce coupling. Whereas the decorator pattern is a closed class that needs to be decorated with the same interface defined in the parent class, the bridge pattern does not use the same interface.

3.4 summary 😄

Bridging patterns are usually designed early in development to allow you to separate parts of your application for development. It is characterized by separating abstraction and implementation, establishing dependencies in the abstraction layer, so that the implementation classes on the two dimensions can carry out their own changes without affecting each other, and has very good scalability.

Design pattern itself has certain difficulty in the application, need certain business experience, if one day you suddenly revealed in which the underlying source code which you read mode, the value, if which day, you in the face of the business was in a hurry, suddenly the forehead a beat, ah, or try the decorator pattern, then you may be insight, I hope that day is closer to you. I am xiao Lei, if you think this article is helpful or inspiring to you, please give me a thumbs up, this will be the best motivation for my creation!

Discussion on design pattern series 👏👏👏

  • (1): Great oaks from little acorns grow
  • (2): Singleton pattern — only one instance
  • (3): Factory mode — give instance generation to subclasses
  • (4): Prototype model — generate examples through replication
  • (5): Builder mode — assemble complex instances
  • (6): Agent mode — look at the influence of intermediary
  • (7): Adapter mode — Let Obama see the three-body problem is not a dream
  • (8): Decorator mode — doll + “hand cake case” a quick introduction