Reputation: 5660
Connection conn = DriverManager.getConnection(URL); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql);
Which design pattern is in use here ? We see each object returning a different object.
What would be some alternatives to the chosen design pattern ?
Upvotes: 5
Views: 14688
Reputation: 3667
Rectification on the checked answer:
The ResultSet
is not a data mapper, from whose link we can easily figure out that the concept data mapper was coined in the context of enterprise application architecture in 2003. Whereas the ResultSet
already existed in the late 1990s. One can search for the keyword "since", by means of which one can infer that the earliest version when new features were added is since 1.2(in 1998. Refer to Java version history), meaning most of the old features such as those getters are already in this class before 1998
Class list of JDK 1.1.8 is here, where there is already ResultSet
Moreover, from the aspect of DriverManager
, Connection
and Driver
, JDBC is not only an application of static factory method but more precisely a standard application of service provider framework, a design pattern introduced in Item 1: consider static factory methods instead of constructors in the book Effective Java:
Connection
is the service interfaceDriverManager.registerDriver
is the provider registration APIDriverManager.getConnection
is the service access APIjava.sql.Driver
is the service provider interfaceUpvotes: 1
Reputation: 379
Obviously not every code follows design patterns. But there are still things there that resembles some familiar patterns.
The whole JDBC architecture is actually a Bridge, it is an abstract concept which holds other abstractions that could be replaced separately.
The classes below implements some patterns.
DriverManager.getConnection(URL) seems like a static factory method to me, which is very common in Java frameworks.
Statement and Connection actually follows the same patterns, it is some sort of Unit of Work or Transaction pattern since it allows you to bulk statements together. But it also follows a Proxy pattern when it implements JDBC Wrapper interface.
ResultSet follows the Iterator pattern but it is also a Data mapper.
Upvotes: 12