Table: BROKERAGE_ACCOUNT
ACCOUNT_NUMBER
CUSTOMER_SS_NUMBER
Table: CUSTOMER
SS_NUMBER
NAME
Table: INVESTMENT
ACCOUNT_NUMBER
STOCK_SYMBOL
AMOUNT
public class BrokerageAccount {
String accountNumber;
String customerSocialSecurityNumber;
// Omit constructors, etc.
public Customer getCustomer() {
String sqlQuery =
"SELECT * FROM CUSTOMER WHERE" +
"SS_NUMBER='"+customerSocialSecurityNumber+"'";
return QueryService.findSingleCustomerFor(sqlQuery);
}
public Set getInvestments() {
String sqlQuery =
"SELECT * FROM INVESTMENT WHERE" +
"BROKERAGE_ACCOUNT='"+accountNumber+"'";
return QueryService.findInvestmentsFor(sqlQuery);
}
}
(
Note:
The QueryService, a utility for fetching rows from the database and creating objects, is
simple for explaining examples, but it's not necessarily a good design for a real project.)
Let's refine the model by qualifying the association between
Brokerage Account
and
Investment
, reducing its multiplicity. This says there can be only one investment per stock.
Figure 5.4.
This wouldn't be true of all business situations (for example, if the lots need to be tracked), but
whatever the particular rules, as constraints on associations are discovered they should be
included in the model and implementation. They make the model more precise and the
implementation easier to maintain.
The Java implementation could become:
public class BrokerageAccount {
String accountNumber;
Customer customer;
Map investments;
// Omitting constructors, etc.
public Customer getCustomer() {
return customer;
}
public Investment getInvestment(String stockSymbol) {
return (Investment)investments.get(stockSymbol);
}
}
And an SQL-based implementation would be:
public class BrokerageAccount {
String accountNumber;
String customerSocialSecurityNumber;
//Omitting constructors, etc.
public Customer getCustomer() {
String sqlQuery = "SELECT * FROM CUSTOMER WHERE SS_NUMBER='"
+ customerSocialSecurityNumber + "'";
return QueryService.findSingleCustomerFor(sqlQuery);
}
public Investment getInvestment(String stockSymbol) {
String sqlQuery = "SELECT * FROM INVESTMENT "
+ "WHERE BROKERAGE_ACCOUNT='" + accountNumber + "'"
+ "AND STOCK_SYMBOL='" + stockSymbol +"'";
return QueryService.findInvestmentFor(sqlQuery);
}
}
Carefully distilling and constraining the model's associations will take you a long way toward a
MODEL-DRIVEN DESIGN
. Now let's turn to the objects themselves. Certain distinctions clarify the
model while making for a more practical implementation. . . .
[ Team LiB ]
[ Team LiB ]
Do'stlaringiz bilan baham: |