Bridging mode (bridge)

Source: Internet
Author: User

Bridging mode (bridge)

Bridging mode is the separation of things from their specific implementations so that they can change independently of each other. Bridging is intended to: the abstraction and implementation of decoupling, so that they can change independently , like our common JDBC Bridge DriverManager, JDBC to connect to the database, the database switch between the various databases, basically do not need to move too much code, Even without moving, the reason is that JDBC provides a unified interface, each database provides its own implementation, with a program called database-driven to bridge the line. Let's take a look at the diagram:

The Java code is implemented as follows

/** * Bridging mode, the abstraction and implementation of decoupling, so that they can change independently *//** * pre-defined an interface * @author HW * */interface sourceable{public void Method ();} /** * Defines a bridge that holds an instance of sourceable * @author HW * */abstract class bridge{private sourceable sourceable;public void Setsourceab Le (sourceable sourceable) {this.sourceable = sourceable;} Public sourceable getsourceable () {return this.sourceable;} public void Method () {Sourceable.method ();}} /** * Defines class Sourceable1, implements Sourceable interface * @author HW * */class Sourceable1 implements Sourceable{public void Method () {SYSTEM.O Ut.println ("This is Sourceable1");}} /** * Defines class Sourceable1, implements Sourceable interface * @author HW * */class Sourceable2 implements Sourceable{public void Method () {SYSTEM.O Ut.println ("This is Sourceable2");}} /** * Definition Mybridge inherit Bridge * @author HW * */class Mybridge extends Bridge{public void method () {getsourceable (). method ();}} public class Bridgetest{public static void Main (string[] args) {/* Call the first object */mybridge Mybridge = new Mybridge (); sourceable sourceable1 = new Sourceable1 (); Mybridge.setsourceable (Sourceable1) Mybridge.method ();/* Call the second object */sourceable sourceable2 = new Sourceable2 (); Mybridge.setsourceable ( Sourceable2); Mybridge.method ();}}

Output

This is Sourceable1
This is Sourceable2

In this way, the call of the bridge class is implemented, and the implementation class SOURCESUB1 and SOURCESUB2 of the docking port sourceable are realized. Next I draw a diagram, we should understand, because this diagram is the principle of our JDBC connection, there is a database learning basis, a combination of all understand.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Bridging mode (bridge)

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.