Learning Bridge design pattern from the angle of reconstruction

Source: Internet
Author: User

Learning Bridge design pattern from the angle of reconstruction

Bridge mode is a pattern that is often applied in the actual system. It best embodies the principle of design pattern programming for interfaces, and the use of aggregation does not use the two principles of inheritance.

Because of our excessive use of inheritance, the structure of the class is too complex, difficult to understand, difficult to maintain. Especially in Java, because you cannot inherit multiple classes at the same time, this can result in multi-layer inheritance, which is more difficult to maintain.

Bridge mode is the root cause of multi-level inheritance. If you're implementing a class in your application, you need to inherit more than two classes, and there's a relationship between the two, and each of them has a variety of different two.

Bridge mode is to divide these two classes into an abstraction, an implementation that makes them two separate, so that two kinds can be changed independently.

Abstraction is the process of extracting a common concept of an entity (the same step), drawing it out (decomposing several separate steps). If we abstract the operation of the database into a process, there are several steps to create the SQL statement, send it to the database processing, and get the results.

Implementation is how to complete this abstract step, such as sending to the database, you need to combine a specific database, consider how to complete this step and so on. And the same step may have different implementations, such as different implementations for different databases.

Now let's assume a situation that is often encountered in the Web, where a page has an input box, such as the name of the customer information, address, and so on, enter the information, and then press the Find button to display the results of the search.

We now assume that we are looking for customer information and account information, which are in different tables.

But our system is faced with two kinds of people, the headquarters of their information is saved to the Oracle database, but the data of each branch store in Sybase, the database location and so on are different, these two kinds of operation.

Here are some of the first things we will use, use if else, to judge, so that the use of the system is difficult to maintain, difficult to expand, you may add a query, or a database to try?

public class SearchAction () {
Public Vector searchdata (string actiontype,string DbType) {
string sql= "";
if (actiontype.equal ("Find customer Information") {
//If you are querying customer information, the spelling SQL statement reads the data from the Customer table
sql= "SELECT * FROM Customer"
if (dbtype.e Qual ("Oracle") {
//read from the Headquarters database, database is Oracle
String connect_string = "Jdbc:oracle:thin:hr/hr@localhost:1521:hrdb" ;
Drivermanager.registerdriver (New Oracle.jdbc.OracleDriver ());
Connection conn = drivermanager.getconnection (connect_string);
//Create A statement
Statement stmt = Conn.createstatement ();
ResultSet RSet = Stmt.executequery (SQL);
//The following omitted part dynamically extracts data from the database, assembles it as a vector, and returns
....... ....... .......
... ... ........ ....... ........... ...
}else (dbtype.equal ("Sybase")) {
//is read from the branch database, which is Sybase
String connect_string = "jdbc:sybase:tds:cai/ Cai@192.168.1.12:1521:fin ";
Drivermanager.registerdriver (New Com.sybase.jdbc.SybDriver ());
Connection conn = drivermanager.getconnection (connect_string);
//Create A statement
Statement stmt = Conn.createstatement ();
ResultSet RSet = Stmt.executequery (SQL);
//The following omitted part dynamically extracts data from the database, assembles it as a vector, and returns
....... ....... .......
... ... ........ ....... ........... ...
}
}else if (actiontype.equal ("Find account Information") {
//If Query account information, concatenation SQL statement reads data from the Account table
sql= "SELECT * from Accounts" if (dbtype.equal ("Oracle")) {
... ..... ............
................. ...
(author Note: Omit read from Oracle, approx. 300 words)
}else if (dbtype.equal ("Sybase")) {
... ............
................. ...
(note: Here omitted from Sybase read, approx. 300 words)
}
}
}
}

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.