In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "the introduction of java bridging mode and the structure of the pattern". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Bridging pattern (Bridge Pattern): separates the abstract part from its implementation part so that they can all change independently. It is a kind of object structural mode, also known as Handle and Body mode or Interface mode.
Imagine that if you want to draw rectangles, circles, ellipses, and squares, we need at least four shape classes, but if the graphics you draw need to have different colors, such as red, green, blue, etc., there are at least two design schemes:
A set of versions in various colors are provided for each shape.
Combine shapes and colors according to actual needs
For the system with two changing dimensions (that is, two reasons for the change), using the second scheme to design the number of classes in the system is less, and the system expansion is more convenient. The second scheme is the application of bridging mode. The bridging pattern converts inheritance relationships into association relationships, thus reducing the coupling between classes and reducing the amount of code writing. For the system with two change dimensions (that is, two reasons for the change), it is more convenient and concise to use the bridge mode to develop. The bridging pattern converts inheritance relationships into association relationships, thus reducing the coupling between classes and reducing the amount of code writing.
Pattern structure
The bridging mode includes the following roles:
Abstraction: abstract class, bridge class
RefinedAbstraction: extending abstract classes
Implementor: implementation class, interface to be bridged
ConcreteImplementor: concrete implementation class
Source code guide
JDBC supports a variety of database operations based on Java, but the self-implementation and transmission protocols of different databases are different. Does Java write an interface for each database to support the implementation of database vendors, which obviously violates the principle of streamlined design? what Java does here is to provide a set of interfaces for manufacturers to implement, and a set of interfaces for program developers to call, the combination of the two is the classic bridging mode. Working with jdbc as a programmer goes like this:
Class.forName ("com.mysql.jdbc.Driver"); String url = ""; String user = ""; String password = ""; Connection con = DriverManager.getConnection (url, user, password); Statement statement = connection.createStatement (); String sql = "insert into student (name,age) VALUE ('" + name + "," + age + ")"; statement.execute (sql)
Let's take a look at some of the source code.
Private static Connection getConnection (String var0, Properties var1, Class var2) throws SQLException {ClassLoader var3 = var2! = null? Var2.getClassLoader (): null; Class var4 = DriverManager.class; synchronized (DriverManager.class) {if (var3 = = null) {var3 = Thread.currentThread () .getContextClassLoader ();}} if (var0 = = null) {throw new SQLException ("The url cannot be null", "08001") } else {println ("DriverManager.getConnection (\"+ var0 +"\ ")); SQLException var10 = null; Iterator var5 = registeredDrivers.iterator (); while (true) {while (var5.hasNext ()) {DriverInfo var6 = (DriverInfo) var5.next () If (isDriverAllowed (var6.driver, var3)) {try {println ("trying" + var6.driver.getClass (). GetName ()); Connection var7 = var6.driver.connect (var0, var1) If (var7! = null) {println ("getConnection returning" + var6.driver.getClass () .getName ()); return var7 }} catch (SQLException var8) {if (var10 = = null) {var10 = var8 } else {println ("skipping:" + var6.getClass (). GetName ()) }} if (var10! = null) {println ("getConnection failed:" + var10); throw var10;} println ("getConnection: no suitable driver found for" + var0); throw new SQLException ("No suitable driver found for" + var0, "08001") }}}
Look at these lines of code.
ClassLoader var3 = var2! = null? Var2.getClassLoader (): null; Class var4 = DriverManager.class; synchronized (DriverManager.class) {if (var3 = = null) {var3 = Thread.currentThread (). GetContextClassLoader ();}}. .connection var7 = var6.driver.connect (var0, var1)
In fact, here DriverManager obtains the Connection through reflection and class loading mechanism to get the connection from the driver of the database driver package, so the one who really participates in the bridging mode here is driver, while DriverManager has nothing to do with the bridging mode, DriverManager is just a manager for driver. As users, we only care about Connection, not driver, because our operations are achieved by manipulating Connection. In this way, the bridging makes the logic clear-java.sql.Driver is the abstract bridge class, while the driver package, such as com.mysql.jdbc.Driver, implements the bridging class, and Connection is the bridged object. The two dimensions here are:
Different database types (different drivers)
Database connection information is different (URL,username,password)
Now suppose a scenario like this-we have designed a framework that needs to provide api, but a class within the framework needs to change frequently, which is very unstable, but the api we provide can't change all the time. How to isolate api methods from frequently changed code, you can actually consider adapter patterns or bridging patterns.
This is the end of the introduction of the java bridging mode and the structure of the pattern. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.