-----Bridging mode for Java 23 design Patterns

Source: Internet
Author: User

One, 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:

Implementation code:

Define the interface first:

    1. Public interface Sourceable {
    2. public void Method ();
    3. }

Define two implementation classes, respectively:

    1. public class SourceSub1 implements sourceable {
    2. @Override
    3. public void Method () {
    4. System.out.println ("This is the first sub!");
    5. }
    6. }
    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:

    1. public abstract class bridge {  
    2.     private  sourceable source;  
    3.   
    4.     public  Void method () {  
    5.         source.method ();   
    6.     }  
    7.       
    8.     public sourceable getsource ()  {  
    9.          return source;  
    10.     }  
    11.   
    12.     public void setsource (Sourceable source)  {  
    13.         this.source = source;   
    14.     }  
    15. }  
    1. public class Mybridge extends Bridge {
    2. public void Method () {
    3. GetSource (). method ();
    4. }
    5. }

Test class:

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

Output

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.

SOURCE https://www.cnblogs.com/maowang1991/archive/2013/04/15/3023236.html

-----Bridging mode for Java 23 design Patterns

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.