Java Surface test----design patterns used in JDBC (bridging mode)

Source: Internet
Author: User
Tags sybase database

1.JDBC (Javadatabase Connectivity)

JDBC is an API that accesses the database in a uniform manner.

It provides platform-independent database access, that is, with the JDBC API, we do not have to access the Oracle database to write a program specifically to access the Sybase database and write a program, and so on, only need to write a program with the JDBC API is enough, It can send SQL calls to the appropriate database. JDBC is a mechanism for the way that Java applications interact with various databases . Simply put, it does three things: establish a connection to the database--the statement that sends the operation database--processes the result.

The JDBC specification uses the design pattern that is primarily involved: bridging mode

Bridging mode (bridge) is a structural design pattern. Bridge mode is based on the principle of minimum class design, and allows different classes to assume different responsibilities by using encapsulation, aggregation, and inheritance behaviors. Its main feature is the separation of abstraction (abstraction) from behavioral implementation (implementation), which preserves the independence of each part and the ability to extend its function.

Roles and responsibilities for bridging mode:

1.Client Call End

This is the caller of bridge mode.

2. Abstract class (abstraction)

Abstract class Interface (interface this abstract class) maintains a reference to the team behavior implementation (implementation). Its role is bridging classes.

3.Refined Abstraction

This is a subclass of abstraction.

4.Implementor

Behavior implementation class Interface (abstraction interface defines a higher level of operation based on the Implementor interface)

5.ConcreteImplementor

Subclass of Implementor

The UML diagram for bridging mode is as follows:

The sample code is as follows:

First define the Implementor interface, which defines the interface that its implementation class must implement operation ()

1  Public Interface implementor {2      Public void operation (); 3 }

The following defines two implementation classes for the Implementor interface:

1  Public classConcreateimplementoraImplementsImplementor {2 @Override3      Public voidoperation () {4System.out.println ("This is Concreteimplementora ' s operation ...");5     }6}
1  Public classConcreateimplementorbImplementsImplementor {2 @Override3      Public voidoperation () {4System.out.println ("This is Concreteimplementorb ' s operation ...");5     }6}

The following defines the bridging class abstraction, which has a reference to the Implementor interface:

1  Public Abstract classAbstraction {2     Privateimplementor implementor;3 4      Publicimplementor Getimplementor () {5         returnimplementor;6     }7 8      Public voidsetimplementor (implementor implementor) {9          This. implementor =implementor;Ten     } One  A     protected voidoperation () { - implementor.operation (); -     } the}

The following is a subclass of the abstraction class Refinedabstraction:

1  Public classRefinedabstractionextendsAbstraction {2 @Override3     protected voidoperation () {4         Super. Getimplementor (). operation ();5     }6}

The test class is given below:

1  Public classBridgetest {2      Public Static voidMain (string[] args) {3Abstraction abstraction =Newrefinedabstraction ();4 5         //Call the first implementation class6Abstraction.setimplementor (NewConcreateimplementora ());7 abstraction.operation ();8 9         //Call the second implementation classTenAbstraction.setimplementor (NewConcreateimplementorb ()); One abstraction.operation (); A  -     } -}

The results of the operation are as follows:

In this way, the implementation of Concreteimplementora and Concreteimplementorb of the docking port implementor is realized by calling the abstraction Bridge class. The separation between abstraction and behavior implementation is realized.


Summarize:

1. Advantages of Bridging mode

(1) Realization of the abstraction and realization of partial separation

Bridging mode separates the abstraction and implementation parts, thus providing the flexibility of the system, separating the abstraction and implementation parts, and defining the interface separately, which helps the system to design hierarchically, thus producing a better structured system. For the high-level part of the system, you only need to know the abstract part and the implementation part of the interface.

(2) Better scalability

Because the bridging mode separates the abstract part from the implementation part, and thus defines the interface separately, the abstract part and the implementation part can be independently expanded, without affecting each other, and greatly provides the expansibility of the system.

(3) can be dynamically switched to achieve

Because the bridging mode realizes the separation of abstraction and implementation, when the bridge mode is implemented, it can realize dynamic selection and use concrete implementation.

(4) The implementation details are transparent to the client, and the implementation details can be hidden from the user.

2. Disadvantages of Bridging mode

(1) The introduction of bridging mode increases the difficulty of system understanding and design, because the aggregation correlation relationship is based on the abstraction layer, which requires the developer to design and program the abstraction.

(2) Bridge mode requires the correct identification of the system of two independent changes in the dimensions, so its scope of use has some limitations.



Java Surface test----design patterns used in JDBC (bridging mode)

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.