tailieunhanh - Java Data Access—JDBC, JNDI, and JAXP phần 6
Một lỗi xảy ra nếu bạn cố gắng để chèn một hàng mà không xác định giá trị cho khóa chính (s). Tuy nhiên, một số DBMS có thể tự động tạo khóa chính: hệ thống như vậy có thể cho phép hành vi này. | Chapter 11 Producing Objects with the Factory Method Pattern a single source. 2. Parameterize the Factory class to create different product objects. Clients can supply different parameters to the factory to retrieve different products. This option enables you to change the factory method dynamically based on your application s current need. 3. Define an interface for the Factory and let the client reference it in order to change factories as needed. In my next example I build a connection manager that uses a factory that incorporates the enhancement listed above. The following list identifies some of the features The ConnectionManager and Factory objects are implemented as a Singleton. You can supply usernames passwords and database IDs as parameters to control what database you connect to and which database user account you connect with. I define an abstract factory class and build specific factory classes that provide connections to either an Oracle or an Access database. I also remove the client s reliance on concrete classes in the following example. The product and factory class names appear only in the connection manager not in the client s code base. This architecture creates a framework in which you can add your own database connection factory for your system. Remember the authors of Design Patterns suggest that you program to interfaces rather than to implementations. Listing 11-2 provides the code for an enhanced version of the Factory Method pattern in which I show you how to do this. Listing 11-2 package chapterll import . Abstract class that defines interface for factory objects abstract class AbstractConnFactory Protected variables that hold database specific information protected static Connection conn protected String dbType null protected String user null protected String password null protected String driver null protected String jdbcUrl null protected String database null Close the database connection public void .
đang nạp các trang xem trước