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

  • SEARCH
  • Home
  • 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 8426987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:40:46+00:00 2026-06-10T04:40:46+00:00

I am implementing the Observer / Observable pattern using Java. However, I am encountering

  • 0

I am implementing the Observer / Observable pattern using Java. However, I am encountering a problem in the Observer portion of my code.

Observable

public class Model extends Observable {

   public void notify() {
       this.setChanged();
       this.notifyObservers(new ArrayList<A>());
       this.notifyObservers(new ArrayList<B>());
   }
}

Observer

public class View implements Observer {

    @Override
    public void update(Observable observable, Object object) {
        // TODO: If object is ArrayList<A>?
        System.out.println("A");
        // TODO: If object is ArrayList<B>?
        System.out.println("B");
    }
}

How would I fill in the TODO comments to check for the generic on the ArrayList? Is this even possible? (I would prefer to do it without implementing more classes, if possible.)

  • 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-06-10T04:40:47+00:00Added an answer on June 10, 2026 at 4:40 am

    An Observable should send one and only one type of data.

    public class ModelA extends Observable {
    
       public void notify() {
           this.setChanged();
           this.notifyObservers(new ArrayList<A>());
       }
    }
    
    public class ModelB extends Observable {
    
       public void notify() {
           this.setChanged();
           this.notifyObservers(new ArrayList<B>());
       }
    }
    

    Your other alternative is to put ArrayList<A> and ArrayList<B> into a class. You can notify your observers with that class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the Observable class / Observer interface in Java to implement the observer
I have met an interesting problem while implementing the Observer pattern with C++ and
I am using the example http://www.codeproject.com/Articles/3267/Implementing-a-Subject-Observer-pattern-with-templ to implement a templated subject/observer pattern. However, I
By referring to article Implementing a Subject/Observer pattern with templates template <class T> class
implementing publishActivity in PHP using the REST API using this code: $activity = array(
When implementing the Strategy Pattern, where does one put the code that determines which
I am thinking about implementing a user interface according to the MVP pattern using
I am having some trouble with implementing my own observer in Java on the
I was thinking about implementing a logic similar to observer pattern on my website,
I'm implementing a java TCP/IP Server using ServerSocket to accept messages from clients via

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.