tailieunhanh - Expert one-on-one J2EE Design and Development phần 6

(không gây ra một bản cập nhật). Một kiến trúc được phân phối là không nhất thiết phải mạnh mẽ hơn so với một kiến trúc collocated. Một kiến trúc bằng cách sử dụng các đối tượng kinh doanh không quốc tịch là cách duy nhất để đảm bảo rằng một kiến trúc phân tán là mạnh mẽ hơn - và khả năng mở rộng hơn - hơn một kiến trúc collocated. | Practical Data Access It is simpler to create objects of this type but we must still rely on the inherited execute methods. This query object can be used as follows SqlQuery customerQuery new CustomerQuery dataSource List customers 6 As RdbmsOperation objects are threadsafe we will typically construct such objects once and hold them as instance variables in Data-Access Objects. The following version is a more sophisticated query that not only hides SQL parameter declaration and compilation inside its constructor but implements a new query method which enables it to take a combination of parameters for which there is no convenient execute method in the SqlQuery class class CustomerQuery extends ManualExtractionSqlQuery public CustomerQuery Datasource ds super ds SELECT id AS id forename AS forename FROM customer WHERE mystring AND myint AND String3 declareParameter new SqlParameter declareParameter new SqlParameter declareParameter new SqlParameter Types-VARCHAR compile protected object extract Resultset rs int rownum throws SQLException Customer cust new Customerf forename id return cust The new query method can take strongly typed parameters and can be given a meaningful name public List findWithMeaningfulName String myString int id String strings return execute new Object myString new Integer id string3 We can use this as shown below. Note that this code is self-documenting CustomerQuery customerQuery new CustomerQuery ds List l foo 1 bar It s easy to avoid code duplication in practice by the use of inheritance among query objects. For example the implementation of the extract method can be provided in a base class while subclasses provide different SQL and variable declarations. When only the SQL WHERE clause varies multiple query objects of the same class can be created each configured to execute different SQL. 349 Brought to