5 Replies - 309 Views - Last Post: 02 November 2017 - 01:53 AM Rate Topic: -----

#1 gypso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-November 17

Pattern Observer Uml & Java

Posted 01 November 2017 - 02:35 PM

Hi everyone, I really need your help for a question: the right implementation of pattern Observer. The fact is that I have no guarantees about doing right so I just would like to know if there are inconsistencies between UML and code below, please report me any minimum mistake/improvable detail in both parties. I would be so much grateful to anyone, thanks in advance.

The UML diagram

JAVA CODE:
public interface Subject {
    public void registerObserver(Observer o);
    public void removeObserver(Observer o);
    public void notifyObservers();
}

public class ConcreteSubject implements Subject {
    private int my_state;
    private ArrayList<Observer> observers;
    public ConcreteSubject(){
        observers=new ArrayList<Observer>();
    }
    public void registerObserver(Observer o){
        observers.add(o);
    }
    public void removeObserver(Observer o){
        observers.remove(o);
    }
    public void notifyObservers(){
        for(Observer o : observers)
            o.update();
    }
    //PULL Mode -> Observer musts grab the state of the subject
    public int getState(){
       return my_state;
    }
    public void setState(int state){
        my_state=state;
    }
 }

public interface Observer {
    public void update(); //In PULL mode update don't need the state of the subject in argument
}

public class ConcreteObserver implements Observer {
    private ConcreteSubject my_subject;
    public ConcreteObserver(ConcreteSubject subject){
        my_subject=subject;
    }
    public void update(){
        System.out.println("The state is: "+my_subject.getState());
    }
}

This post has been edited by g00se: 01 November 2017 - 02:45 PM
Reason for edit:: Attempted to internalize image. Image attachment failed - still buggy


Is This A Good Question/Topic? 0
  • +

Replies To: Pattern Observer Uml & Java

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3443
  • View blog
  • Posts: 15,601
  • Joined: 20-September 08

Re: Pattern Observer Uml & Java

Posted 01 November 2017 - 02:46 PM

It seems OK to me. Have you tried to run code based on it?
Was This Post Helpful? 0
  • +
  • -

#3 gypso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-November 17

Re: Pattern Observer Uml & Java

Posted 01 November 2017 - 02:55 PM

View Postg00se, on 01 November 2017 - 02:46 PM, said:

It seems OK to me. Have you tried to run code based on it?

Actually no. But I'd like to specify that my problem is not about implementing Observer, but rather to understand the relation with UML and code. For example: cardinality between interfaces and concrete classes is different and I'm not sure about this. More, method update is without argument while in Java.util it has two. Is it right to implement the association between concrete classes with the subject attribute?
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3443
  • View blog
  • Posts: 15,601
  • Joined: 20-September 08

Re: Pattern Observer Uml & Java

Posted 01 November 2017 - 03:37 PM

Quote

For example: cardinality between interfaces and concrete classes is different and I'm not sure about this.

In what way different?
Was This Post Helpful? 0
  • +
  • -

#5 gypso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-November 17

Re: Pattern Observer Uml & Java

Posted 02 November 2017 - 12:15 AM

In the first association I'm saying that an Observer can have a Subject (0..1), in the second I say a ConcreteObserver must have one ConcreteSubject (1..1). Could I do a two-way connection between interfaces?

This post has been edited by andrewsw: 02 November 2017 - 12:28 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3443
  • View blog
  • Posts: 15,601
  • Joined: 20-September 08

Re: Pattern Observer Uml & Java

Posted 02 November 2017 - 01:53 AM

Actually i'm not sure if cardinality between interfaces makes much sense. Let's see what some of the others think.
As for the cardinality between classes, your diagram, i think (correctly) is stating that a subject can be observed by zero or more observers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1