Java data access

Page 197

Chapter 11: Producing Objects with the Factory Method Pattern super(msg); } }// end ConnectionManagerException

The output from Listing 11−2 is as follows: Connection not created. Beginning connection phase... Connecting to Oracle database... Connection successful.. Closing connection Connection not created. Beginning connection phase... Connecting to ODBC database... Connection successful.. Closing connection

Listing 11−2 uses numerous objects. Figure 11−3 provides a UML class diagram showing the relationships of the classes used in the example. To further help understand the classes, Table 11−2 maps the Factory Method components to the classes in the example. In the code listing, I create the AbstractConnFactory to represent the FactoryInterface. I make the class abstract for two reasons. First, I do not want the class to be directly instantiated. Rather, I want clients to use one of the connection factories, either Oracle or ODBC. Secondly, the two factories share implementations of the openConnection() and the close() methods. If the methods need to be different, I can always override them in a subclass.

Figure 11−3: UML class diagram of the enhanced Factory Method example

Table 11−2: EnhancedFactory Example Classes Factory Method Components FactoryInterface Factory ProductInterface Product

EnhancedFactory Components AbstractConnFactory class OracleConnFactory class OdbcConnFactory class java.sql.Connection interface java.sql.Connection interface 185


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.