简体   繁体   中英

Java Observer Pattern issue?

I have the following Observer:

public class Fisc implements Observer {
double value1;
double value2;
private static HashMap<String, Deposit> accounts=new HashMap<String,Deposit>();
public Fisc(String cnp,Deposit deposit) {

    System.out.println("\n*******Observer*******Watching account:"+id);
    accounts.put(id, deposit);

}

public void update(Observable obj, Object arg) {
    if (arg instanceof Deposit) {
        value1 =((Deposit) arg).getvalue1();
        value2=((Deposit) arg).getvalue2();
        System.out.println("\n*******Observer*******value1 current value:"+value1);
        System.out.println("*******Observer*******value2 current value:"+value2);
    } else {
        System.out.println("Observable error!");
    }
}
}

and the Observable:

import java.util.HashMap;
import java.util.Observable;


public class obs extends Observable {

    private static HashMap<String, Deposit> accounts;

    private static obs instance;

    private obs(HashMap<String,Deposit> accounts){
        obs.accounts=accounts;
    }

    public static obs getInstance(){
        if (instance==null){
            return new obs(new HashMap<String,Deposit>());
        }
        else return instance;
    }

        // ... some unimportant other stuff

    public void depositvalue1(String id,double value1){
        Deposit deposit=accounts.get(id);
        deposit.addvalue1(value1);

        if(deposit.isWatchedByFisc()){
            notifyFisc(deposit);
        }
        System.out.println("obs:Deposited "+value1+ " value1 to account:"+id+"!");
        System.out.println("obs:Current value1 in account:"+deposit.getvalue1());
    }

    public void depositvalue2(String id,double value2){
        Deposit deposit=accounts.get(id);
        deposit.addvalue2(value2);

        if(deposit.isWatchedByFisc()){
            notifyFisc(deposit);
        }

        System.out.println("obs:Deposited "+value2+" value2 to account:"+id+"!");
        System.out.println("obs:Current value1 in account:"+deposit.getvalue2());
    }

    public void depositValues(String id,double value1,double value2){
        Deposit deposit=accounts.get(id);
        deposit.addvalue1(value1);
        deposit.addvalue2(value2);

        if(deposit.isWatchedByFisc()){
            notifyFisc(deposit);
        }

        System.out.println("obs:Deposited "+value1+ " value1 and "+value2+" value2 to account"+id+"!");
        System.out.println("obs:Current value1 in account:"+deposit.getvalue1());
    }

    public void watchAccount(String id){
        Deposit deposit=accounts.get(id);
        deposit.setWatchedByFisc(true);
        addObserver(new Fisc(id,deposit));
    }

    public void stopWatchAccount(String id){
        accounts.get(id).setWatchedByFisc(false);
        System.out.println("obs:Account "+id+" is no longer watched by Fisc!");
    }

    public void notifyFisc(Deposit deposit){
        setChanged();
        notifyObservers(deposit);
    }

}

Everything works as suppossed except the following: If I use the depositValue(1,2,s) methods instead of getting the message once, I get the same message the number of times I have registered deposits to be watched. How can I fix this?

Hope this makes sense. Thanks in advance and sorry if this is a stupid question this is the first time using Observer Pattern.

My guess is this line maybe(multiple instances?): addObserver(new Fisc(id,deposit));

Every observer (an instance of Fisc ) is notified whenever a Deposit instance has changed. So with your code, every Fisc has to look at the notification message and check, if it is about its deposit.

If you don't want that, then you should make the Deposit observable (instead of observing the whole bank ). Then you can register listeners to individual deposits.

You have lumped together all accounts in the same Observable object and that's why you get notified once for each account.

A better model would probably be to introduce an Account class and make that Observable .
I suggest something like:

public class Account extends Observable {

    private String id;    
    private BigDecimal balance = new BigDecimal("0.0");

    public Account(String id) {
      this.id = id;
    }

    public BigDecimal getBalance() {
        return balance;
    }

    public void deposit(BigDecimal amount) {
        balance = balance.add(amount);
        notifyObservers();
    }

    public void withdraw(BigDecimal amount) {
        balance = balance.subtract(amount);
        notifyObservers();
    }
}

Your obs class would then contain a list of Account s:

private Map<String, Account> accounts = new HashMap<String, Account>();

Note that this class uses BigDecimal for representing the balance, since it is not recommended to use floating point numbers for it .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM