Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3423156
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:19:18+00:00 2026-05-18T06:19:18+00:00

I have been trying to no avail to get the observer pattern working in

  • 0

I have been trying to no avail to get the observer pattern working in a relatively simple application.

I have 4 GUI classes

  • StarterClass (contains a CompositeWordLists and a CompositeWordListData)
  • CompositeWordLists (contains many CompositeListItem/s and a CompositeWordListData)
    • CompositeListItem
  • CompositeWordListData (Contains a DialogWordData)
    • DialogWordData

Here is my Observable

interface Observable<T> {
    void addObserver(T o);
    void removeObserver(T o);
    void removeAllObservers();
    void notifyObservers();
}

And I am creating Observers like this:

public class Observers {
    private Observers(){};

    interface WordListsObserver {
        public void update(CompositeWordLists o);
    }   

    interface ListItemObserver {
        public void update(CompositeListItem o);
    }
}

Basically I am having trouble with specifying the sort of event that occurred. For example, the CompositeWordLists class needs to know when a CompositeListItem is deleted, saved edited etc but I only have one update method … my brain hurts now!

What is a better way of doing this?


UPDATE

Still having trouble with this, I added events and changed Observable and Observers but now I have type safety problems.

public class Observers {
    private Observers(){};

    /**
     * @param <T> the object that is passed from the Observable
     */
    interface ObservableEvent<T> {
        T getEventObject();
    }


    /**
     * Get notified about Authentication Attempts
     */
    interface ObserverAuthenticationAttempt {
        /**
         * @param e true if authentication was successful
         */
        public void update(ObservableEvent<Boolean> e); 
    }


    /**
     * Get notified about a Word Deletion
     */
    interface ObserverWordDeleted {
        /**
         * @param e the id of the word that was deleted
         */
        public void update(ObservableEvent<Integer> e); 
    }
}

The Observable Interface now looks like this

interface Observable<T> {
    void addObserver(T o);
    void removeObserver(T o);
    void removeAllObservers();
    <K> void  notifyObservers(Observers.ObservableEvent<K> e);
}

The problem is that when I implement this I get and would have to cast K to the appropriate type, not really what I want to do.

@Override
public <K> void notifyObservers(ObservableEvent<K> e) {
    for(Observers.ObserverAuthenticationAttempt o : this.observers)
        o.update(e);
}

What am I doing wrong?

update 2

Actually it works better with an Observable like this, but I still need to specify the correct EventType in two different places.

interface Observable<T,K> {
    void addObserver(T o);
    void removeObserver(T o);
    void removeAllObservers();
    void  notifyObservers(Observers.ObservableEvent<K> e);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:19:18+00:00Added an answer on May 18, 2026 at 6:19 am

    You do not need to parametrise the Observers, but you need to parametrize the events.

    public interface Observer<T> {
        void notify(T event);
    }
    

    An example event:

    public class WordListUpateEvent {
    
        private final int changedIndex;
    
        public WordListUpateEvent(int changedIndex) {       
            this.changedIndex = changedIndex;
        }
    
        public int getChangedIndex() {
            return changedIndex;
        }
    }
    

    Then you can have different interface of it for example:

    public interface WordListObserver extends Observer<WordListUpateEvent> {}
    

    and its implementations

    public class ConcreteWordListObserverA implements WordListObserver {
        @Override
        public void notify(WordListUpateEvent event) {
            System.out.println("update item at index: " + event.getChangedIndex());
        }
    }
    

    on the other hand you need your Observable interface, i have splitted it in two interface in order ti make the notifyObservers method not public to the observers (you will see it later):

    public interface Observable<T> extends ObservableRegistration<T> {  
        void notifyObservers(T event);
    }
    
    public interface ObservableRegistration<T> {
    
        void addObserver(Observer<T> o);
        void removeObserver(Observer<T> o);
        void removeAllObservers();
    }
    

    If you would have several observables in a subject, you can not implemnt the Observalbe interface direct to your subject, so you need a seperate implementation class:

    public class ObservableImpl<T> implements Observable<T>{
    
        private final List<Observer<T>> observers = new ArrayList<Observer<T>>();
    
        @Override
        public void addObserver(Observer<T> o) {
            this.observers.add(o);
        }
    
        @Override
        public void removeObserver(Observer<T> o) {
            this.observers.remove(o);
        }
    
        @Override
        public void removeAllObservers() {
            this.observers.clear();     
        }
    
        @Override
        public void notifyObservers(T event) {      
            for(Observer<T> observer : observers) {
                observer.notify(event);
            }
        }
    
    }
    

    Now you can use the implementation in your subject:

    public class Subject {
    
        private Observable<WordListUpateEvent> wordListObservable = new ObservableImpl<WordListUpateEvent>(); 
    
        //private Subject<OtherEvent> otherObservable = new ObservableImpl<WordListUpateEvent>();
    
    
        public ObservableRegistration<WordListUpateEvent> getWordListObservableRegistration() {
            return this.wordListObservable;
        }
    
    //  public ObservableRegistration<OtherEvent> getOtherRegistration() {
    //      return this.otherObservable;
    //  }
    
        public void doSomething() {
            this.wordListObservable.notifyObservers(new WordListUpateEvent(42));
        }
    
    }
    

    And this is how you can connect the observer and the subject:

    public class Start {
    
        public static void main(String[] args) {
            Subject subject = new Subject();
    
            subject.getWordListObservableRegistration().addObserver(new ConcreteWordListObserverA());
            subject.getWordListObservableRegistration().addObserver(new ConcreteWordListObserverA());
    
            subject.doSomething();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to get around this error for a day now and
I have been trying to explain the difference between switch statements and pattern matching(F#)
I have been trying to implement Win32's MessageBox using GTK. The app uses SDL/OpenGL,
I have been trying to find a really fast way to parse yyyy-mm-dd [hh:mm:ss]
I have been trying to determine a best case solution for registering a COM
I have been trying to work my way through Project Euler, and have noticed
I have been trying to read a picture saved in Access DB as a
I have been trying to learn multi-threaded programming in C# and I am confused
I have been trying to make a case for using Python at my work.
I have been trying to learn how to add testing to existing code --

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.