避免循环引用

隐藏这些实体 Bean
不要将您的实体 Bean 直接暴露给客户端--使用一个会话 Bean 作为前端· 隐藏实体 Bean:自动事务处理
| 为每个 Bean 的方法产生一个事务处理 |
| 客户端方法 void transferFunds(Account to, Account from, double amount) throws RemoteException { account1.debit(from); // TX_REQUIRED Assumed account2.credit(to); // TX_REQUIRED Assumed } |
| 为 transferFunds 方法创建一个事务处理 |
| TellerBean(会话 Bean)方法 void transferFunds(Account to, Account from, double amount) throws RemoteException { account1.debit(from); // TX_REQUIRED Assumed account2.credit(to); // TX_REQUIRED Assumed } |
隐藏实体 Bean: Loose Coupling
Facade 模式...

| ... Teller facade = tellerHome.create(); facade.transferFunds(1234, 564, 2000.0); ... |
| TellerBean method void transferFunds(int toId, int fromId, double amount) throws FinderException, RemoteException { Account to = accountHome.findById(toId); Account from = accountHome.findById(fromId); account1.debit(from); account2.credit(to); } |
· Command 模式...
| ... TransferCommand command = new TransferCommand(); command.setFromAccount(1234); command.setToAccount(543); command.setAmount(1000.0); Teller teller = tellerHome.create(); teller.processCommand(command); ... |
| TellerBean method CommandResult processCommand(Command command) { try { return command.process(getCommandContext()); } catch (Exception e) { return new ResultFailure(...); } } |
| TransferCommand method CommandResult process(CommandContext context) throws Exception { Account toAccount = context.getAccountHome().findById(getToAccount()); Account fromAccount = context.getAccountHome().findById(getFromAccount()); toAccount.credit(amount); fromAccount.debit(amount); return new ResultSuccess(); } |
本章讲述内容