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 8549513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:43:56+00:00 2026-06-11T13:43:56+00:00

I have a problem with Observer pattern. First, I have a HttpHelper class to

  • 0

I have a problem with Observer pattern.

First, I have a HttpHelper class to get data from server, I used it as Observerable.

 public class HttpHelper extends Observable,Runnable{
    public void run(){
      //do long task to get data
      String result = getData();
      setChanged();
      notifyObservers(result);
   }
 } 

The DataManager class get data from HttpHerlper when completed, then do some business task.

public class DataManager implements Observer {
    public void doTask(){
    HttpHelper helper = new HttpHelper();
    helper.addObserver(this); 
    Thread thread = new Thread(helper);
    thread.start();
    }
    public void update(Observable obj, Object data) {
       if (data instanceof String) {
         // do some stuff with this data
         // Then I want to notify the result to the view
         Model model = doSomething(data);
         notify(model)
       }
    }
}

Finaaly, View class will update data when DataManager complete task.

  public class View{
       private void getData(){
           DataManager manager = new DataManager()
           manager.doTask();
       }
       public void update(Observable obj, Object data) {

       }
 }

Should I use Observer again? And how can I do that?
P/s: for some reason, HttpHelper and DataManager must be separated.

Update: Here is the class structure
https://www.dropbox.com/s/givn6vzvqr4cgye/bkd.png

  • 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-11T13:43:58+00:00Added an answer on June 11, 2026 at 1:43 pm

    IMO, the relationship between HttpHelper and DataManager doesn’t need an observer pattern. It seems to be just a callback to notify the manager that the processing is done. Observers are better suited for dispatching events to multiple, different listeners via a common interface, not to a single listener. Having said that, what you have will work. Check this article on implementing callbacks if you want to follow my advice

    Now, for the relationship between the manager and the view i do agree that you should use an observer pattern, this will allow you to create different views that react differently to the same events. This means that it’s DataManager that should extend Observable, and every view listening it should implement Observer

    Finally, i have to say that if you plan on having different types of events, the JDK observable and observer (java.util) mechanism is not very clean. My biggest criticism is that the second argument of update is an Object, so you end up with a huge list of if else where you need to check instanceof like in your example, which in general is not good practice. An alternative is to use the event notification mechanism from the UI classes (like EventObject and EventListener) , they are part of the UI classes but are really more generic than just UIs. see this other question

    Oh and if you can, try to avoid cascading events. It makes code hard to read and debug. Maybe the view could observe directly the HttpHelper??

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

Sidebar

Related Questions

I have the following problem. Given a Interface EventNotifier for the Observer Pattern: public
I have a problem with Observer-Pattern and deadlock using threads. package observerDeadLock; import java.util.Observable;
I have met an interesting problem while implementing the Observer pattern with C++ and
In my Java Sockets program, I have implemented a client-server Observer pattern. That is,
I have problem with UIWebView delay when the load image from url. In my
I have problem while loading data into html select when users press or click
I have problem SIMILAR to preventing form data reposting, but not quite the same
I often come accross the problem that I have a class that has a
I have defined the following service with an observer of messages sent. The problem
I have a strange problem in matching a pattern. Consider the Perl code below

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.