【问题标题】:Should I use Observer pattern to notify Customer object when the customer's Account has made deposit transaction?当客户的账户进行存款交易时,我应该使用观察者模式来通知客户对象吗?
【发布时间】:2014-09-16 02:20:22
【问题描述】:

我确实有分别实现 ICustomerIAccount 的 Customer 和 Account 业务对象。我也有每个业务对象的服务层。 AccountServiceaddAccountdeposit()withdraw() 方法)和 CustomerService(addCustomeraddAdressForACustomerremoveCustomer() 等方法)。

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


    【解决方案1】:

    你应该使用观察者模式吗?这可能是一个好主意,因为您可以清楚地分离关注点,简化您的设计,而不是到处乱七八糟的业务逻辑。

    我应该通过扩展 Observable Java 类将 notifyCustomer() 方法添加到我的 Account 业务对象还是服务中?

    这绝对是NO。 Observerable 是 Java 1 类,设计不佳。首先,它是一个类,而不是一个接口,所以你必须扩展它才能使用它。这严重限制了您的设计选择。而是创建自己的接口,让您的类实现该接口来进行观察。

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

    public interface AccountObserver{
    
       public void withdrawl(IAccount from, BidDecimal amount);
    
       public void deposit(IAccount to, BigDecimal amount);
    
    }
    

    我提供了一个IAccount 参考,以便客户知道哪个帐户已更新,因为您要管理多个帐户。

    【讨论】:

    • 感谢您的快速回复。当您说“让您的类实现该接口”时,是在谈论业务对象类 Account 来实现 AccountObserver?主题(客户)怎么样?我应该做同样的事情吗?再次感谢。
    • Account 不应该实现AccountObserver,不管是什么客户都应该实现AccountObserver。但是,某些对象(AccountAccountService)应跟踪哪些客户正在观察哪些帐户,以便正确通知他们
    • 太棒了!但有一件事还不清楚。题目怎么样?它应该是 Customer 对象,我们将在其中拥有 update() 方法吗?而且我认为在上面的 AccountObserver 中,我们还应该包括 notify() 和 addAccounts() 。对吗?
    【解决方案2】:

    对于这种情况,您可以维护每个观察者的帐户列表。这样当模型发生变化时,所有帐户都可以更新

    public class Account(account)
    {
    
    List<Customer> listCustomers = new ArrayList();
    public addRelatedCustomer(Customer customer)
    {
    listCustomers.add(customer)
    }
    
    public updateAccount(int balance)
    {
    this.account.balance=balance;
    updateAllUsers();
    }
    
    public void updateAllUsers()
    {
    customer.notify();
    
    }
    
    
    public class Customer 
    {
    public void notify(Account account)
    {
    //sample code
    }
    
    }
    
    
    public class Demo{
    
    Account ac = new Account();
    
    Customer c1= new Customer();
    Customer c2= new Customer();
    
    ac.add(c1);
    ac.add(c2);
    
    
    //// This will trigger notification to all customers
    ac.updateAccount(5000);
    
    
    
    
    
    
    }
    }
    }
    

    【讨论】:

    • 我不想更新帐户。反之亦然。当帐户更新时,我想通知客户。
    • Ksv。我不认为你明白这一点。一个账户只属于一个客户。所以我们不需要在帐户中拥有客户列表。但是一个客户可以有多个帐户
    【解决方案3】:

    即使一个客户可以有多个帐户,但每个帐户都会有 1:1 的对应关系。意味着一个帐户将与一个所有者有关系(但是,如果他拥有多个帐户,则所有者将是相同的)。

    现在,每当更新帐户时,您都可以通过所有者的回调向附加的所有者发送通知,或者您可以按照@dkatzel 的建议使用 AccountObserver。如果您有除所有者以外的观察者(例如 - 批处理作业、银行交易系统、审计系统等),这将很有帮助。在这种情况下,您可以注册监听器并维护它们的列表

    class Account{
     List<AccountObserver> allListeners;
    
     public void deposit(Double amt){
       doDeposit();
       for(AccountObserver o: allListeners){
          o.notify(this,amt);
       }
      ...
     }
    
      class Customer implements AccountObserver{
       Account act;
       ...
       registerMe(){
        act.register(this);//adds to the listener list of Account
       }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-20
      • 1970-01-01
      • 1970-01-01
      • 2014-03-03
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 2019-12-31
      相关资源
      最近更新 更多