【发布时间】:2014-09-16 02:20:22
【问题描述】:
我确实有分别实现 ICustomer 和 IAccount 的 Customer 和 Account 业务对象。我也有每个业务对象的服务层。 AccountService(addAccount、deposit()、withdraw() 方法)和 CustomerService(addCustomer、addAdressForACustomer、removeCustomer() 等方法)。
我希望我的系统使用观察者模式,以便在其中一项交易(withdraw() 或deposit() 方法成功)时向客户发送电子邮件通知。我明白客户是观察者,账户是应该通知客户的主体。但是客户可以有不同的帐户,并且应该在交易发生时通知每个帐户。
我应该通过扩展 Observable Java 类将 notifyCustomer() 方法添加到我的 Account 业务对象还是服务中?我有点困惑如何实现它。在这种情况下使用观察者模式应该是一件好事吗?
谢谢
这是我的业务对象的示例代码
package business;
import java.math.BigDecimal;
public class Account implements IAccount {
private int accountNumber;
private IParty owner;
public Account(int accountNumber, IParty owner, BigDecimal balance) {
super();
this.accountNumber = accountNumber;
this.owner = owner;
this.balance = balance;
}
private BigDecimal balance;
public int getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}
public IParty getOwner() {
return owner;
}
public void setOwner(IParty owner) {
this.owner = owner;
}
public BigDecimal getBalance() {
return balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
package business;
import java.util.Date;
public class Person extends Party implements IPerson {
private Date birthDate;
public Person(String name, IAddress address,Date birthDate) {
super(name, address);
this.birthDate=birthDate;
// TODO Auto-generated constructor stub
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
}
package service;
import java.math.BigDecimal;
import java.util.List;
import business.Account;
import business.IAccount;
import business.IAccountEntry;
import business.IParty;
public class AccountService implements IAccountService {
/* (non-Javadoc)
* @see service.IAccountService#createAccount()
*/
IDaoService daoService;
@Override
public void createAccount(){
//IAccount account= new Account(accountNumber,party,amount) ;
//daoService.create();
}
/* (non-Javadoc)
* @see service.IAccountService#removeAccount(business.IAccount)
*/
public void removeAccount(){
}
/* (non-Javadoc)
* @see service.IAccountService#addAccountEntry(business.IAccountEntry)
*/
@Override
public void addAccountEntry(IAccountEntry accountEntry){
}
/* (non-Javadoc)
* @see service.IAccountService#removeAccountEntry(business.IAccountEntry)
*/
@Override
public void removeAccountEntry(IAccountEntry accountEntry){
}
/* (non-Javadoc)
* @see service.IAccountService#deposit(business.IAccount, java.math.BigDecimal)
*/
@Override
public void deposit(IAccount account, BigDecimal amount){
}
/* (non-Javadoc)
* @see service.IAccountService#withdraw(business.IAccount, java.math.BigDecimal)
*/
@Override
public void withdraw(IAccount account, BigDecimal amount){
}
/* (non-Javadoc)
* @see service.IAccountService#getCurrentBalance()
*/
@Override
public BigDecimal getCurrentBalance(){
return null;
}
/* (non-Javadoc)
* @see service.IAccountService#findAccountsByParty(business.IParty)
*/
@Override
public List<IAccount> findAccountsByParty(IParty party){
return null;
}
/* (non-Javadoc)
* @see service.IAccountService#findAccountByAccountNumber(int)
*/
@Override
public IAccount findAccountByAccountNumber(int accountNumber){
return null;
}
@Override
public void removeAccount(IAccount account) {
// TODO Auto-generated method stub
}
}
【问题讨论】:
标签: java algorithm design-patterns observer-pattern