Design mode bridge mode JDBC

Source: Internet
Author: User

Bridge mode is the structure pattern of the object. Also known as the handle body (Handle and body) mode or interface (Interface) mode. The bridge model is intended to "decouple abstraction (abstraction) from implementation (implementation) so that they can vary independently".

The purpose of bridge mode

  While bridge mode is not a highly used pattern, familiarity with this pattern is useful for understanding object-oriented design principles, including the " open-close" principle and the combination/aggregation multiplexing principle . Understanding these two principles helps to form the right design ideas and develop a good design style.

The bridge model is intended to " decouple abstraction (abstraction) from implementation (Implementation) so that they can vary independently". This is a short sentence, but the first person to read this sentence is likely to think for a long time and do not understand the meaning.

This sentence has three key words, namely abstraction, realization and decoupling. Understanding the concepts represented by these three words is the key to understanding the purpose of bridge patterns.

    • Abstraction of

Extracting common and essential features from many things, and discarding their non-essential features, is abstraction. such as apples, bananas, pears, peaches, and so on, their common characteristic is fruit. The process of drawing fruit concept is an abstract process. To be abstract, comparisons must be made, and there is no comparison to find the parts that are essentially common. Common characteristics are those features that distinguish a kind of thing from his class, and these distinguishing features are also called essential features. So the common feature of extracting things is to extract the essential characteristics of things and discard the non-essential features. So the process of abstraction is also a cropping process. In the abstract, the same and different, it is decided from what angle to abstract. The abstract angle depends on the purpose of the analysis problem.

Typically, a group of objects, if they have the same characteristics, can be described by a common class. If some classes have the same characteristics, they can often be described by a common abstract class.

    •   Realization of

The concrete implementation of abstraction is realization.

An instance of a class is an instantiation of this class, and a concrete subclass is an instantiation of its abstract superclass.

    •   de-coupling

The so-called coupling is a strong association of the behavior of two entities. and the strong association is removed, is the coupling of liberation, or decoupling. In this case, decoupling means freeing the coupling between abstraction and implementation, or changing the strong association between them into weak associations.

The so-called Strong Association, which is determined in the compile time, cannot be dynamically changed at runtime, so-called weak correlation is the association that can be dynamically determined and can be dynamically changed during the run time. Obviously, in the Java language, an inheritance relationship is strongly associative, whereas an aggregation relationship is a weak association.

changing the inheritance relationship between the two roles to an aggregation relationship is the change of the strong association between them into a weak association. Therefore, the so-called decoupling in bridge mode refers to the use of aggregation relationships rather than inheritance relationships between abstraction and implementation of a software system (which is also an important difference between the bridge pattern and the template method pattern), so that the two can vary relatively independently. This is the purpose of bridge mode.

UML diagram:

As you can see, this system contains two hierarchical structures:

Abstract hierarchical structure composed of abstract roles and modified abstract roles.

  Second, the implementation of the role and two specific implementation of the role of the implementation of the hierarchical structure.

  The roles involved in bridge mode are:

abstract (abstraction) role: abstract The given definition and save a reference to the implemented object.

Fix abstract (refinedabstraction) roles: extend abstract roles, change and modify the definition of the parent class for abstraction.

implementation-implementor role: This role gives the interface to the implementation of the role, but does not give a specific implementation. It must be noted that this interface is not necessarily the same as the interface definition of the abstract role, in fact, the two interfaces can be very different. The implementation role should only give the underlying operation, and the abstract role should give only a higher level of operation based on the underlying operation.

implementation-specific (concreteimplementor) role: This role gives a concrete implementation of the implementation of the role interface.

The abstract character is like a handle to a water cup, while the realization of the role and the specific implementation of the role is like a cup of water cup body. The handle controls the cup body, which is the source of this pattern alias "handle Body".

An object is a wrapper over the behavior, and the behavior is implemented by the method. In this schematic system, classes in the abstract hierarchical structure encapsulate the operation () method, whereas classes in the implementation hierarchy encapsulate the Operationimpl () method. Of course, there are often more than one method in the actual system.

Methods in an abstract hierarchical structure implement their own functionality by delegating to the corresponding implementation object, which means that the abstract role can achieve the purpose of dynamically transforming its own functionality by delegating to different implementation objects .

Advantages of bridge mode

separating abstraction and implementation parts

The bridge pattern separates the abstract and the implementation parts, thus greatly providing the flexibility of the system. To separate the abstract and implementation parts, define the interfaces separately, which helps to layer the system, resulting in a better structured system.

Better Extensibility

  The bridge mode allows the abstraction and implementation to be expanded independently, without affecting each other, thus greatly improving the scalability of the system.

A very typical example of bridge mode in Java applications is the JDBC driver. JDBC provides a common interface for all relational databases. An application dynamically selects a suitable drive and then sends instructions to the database engine through the drive. This process is to delegate the behavior of the abstract role to the process of implementing the role.

Abstract roles can issue query directives against any database engine, because abstract roles do not directly interact with the database engine, and the JDBC Drive is responsible for the underlying work. Because of the presence of a JDBC drive, the application system can evolve independently of the details of the database engine, and the database engine can evolve independently of the details of the application system. The two independent hierarchy is shown, the left side is the JDBC API hierarchy, and the right is the JDBC drive hierarchy. The application is built on the JDBC API.

This is because the application of JDBC has no effect on the application when switching database implementation.

Design mode bridge mode JDBC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.