Search code examples
javaobserver-pattern

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));


Solution

  • 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.