gpt4 book ai didi

java - 当客户的帐户进行存款交易时,我是否应该使用观察者模式通知客户对象?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:46:37 24 4
gpt4 key购买 nike

我确实有 Customer 和 Account 业务对象,它们分别实现了 ICustomerIAccount。我还有每个业务对象的服务层。 AccountService(addAccountdeposit()withdraw() 方法)和 CustomerService(addCustomeraddAdressForACustomerremoveCustomer() 等方法)。

我希望我的系统使用 Observer 模式,以便在其中一项交易(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

}
}

最佳答案

你应该使用观察者模式吗?这可能是个好主意,因为您可以清楚地分离关注点、简化您的设计并且不会在任何地方困惑业务逻辑。

我应该通过扩展 Observable Java 类向我的帐户业务对象或服务中添加 notifyCustomer() 方法吗?

这是一个明确的否定。 Observable 是 Java 1 类,设计不佳。首先,它是一个类,而不是一个接口(interface),因此您必须扩展它才能使用它。这严重限制了您的设计选择。相反,创建您自己的接口(interface)并让您的类实现该接口(interface)而不是进行观察。

由于您想要一个示例并且您有多个帐户:

public interface AccountObserver{

public void withdrawl(IAccount from, BidDecimal amount);

public void deposit(IAccount to, BigDecimal amount);

}

我包含了一个 IAccount 引用,以便客户知道更新了哪个帐户,因为您要管理多个帐户。

关于java - 当客户的帐户进行存款交易时,我是否应该使用观察者模式通知客户对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20054632/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com