10. Bridging mode (bridge)

Source: Internet
Author: User

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:

Implementation code:

Define the interface first:

[Java]View Plaincopy
    1. Public Interface sourceable {
    2. Public void method ();
    3. }

Define two implementation classes, respectively:

[Java]View Plaincopy
    1. Public class SourceSub1 implements sourceable {
    2. @Override
    3. Public void method () {
    4. System.out.println ("This is the first sub!");
    5. }
    6. }
[Java]View Plaincopy
    1. Public class SourceSub2 implements sourceable {
    2. @Override
    3. Public void method () {
    4. System.out.println ("This is the second sub!");
    5. }
    6. }

Define a bridge that holds an instance of sourceable:

[Java]View Plaincopy
  1. Public Abstract class Bridge {
  2. Private Sourceable source;
  3. Public void method () {
  4. Source.method ();
  5. }
  6. Public Sourceable GetSource () {
  7. return source;
  8. }
  9. Public void SetSource (sourceable source) {
  10. this. Source = source;
  11. }
  12. }
[Java]View Plaincopy
    1. Public class Mybridge extends Bridge {
    2. Public void method () {
    3. GetSource (). method ();
    4. }
    5. }

Test class:

[Java]View Plaincopy
  1. Public class bridgetest {
  2. Public Static void Main (string[] args) {
  3. Bridge bridge = new Mybridge ();
  4. /* Call the first object */
  5. sourceable source1 = new SourceSub1 ();
  6. Bridge.setsource (SOURCE1);
  7. Bridge.method ();
  8. /* Call the second object */
  9. sourceable source2 = new SourceSub2 ();
  10. Bridge.setsource (SOURCE2);
  11. Bridge.method ();
  12. }
  13. }


This is the first sub!
This is the second sub!

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.

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