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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:44:13+00:00 2026-06-15T13:44:13+00:00

I’ve been trying to do some simple thing in java that in javascript would

  • 0

I’ve been trying to do some “simple thing” in java that in javascript would look like:

// Main class
var model = new Model();
this.callback = function(e){/* do something */}
model.addListener("change", callback);

Well in java what I found so far is making the Main class deriving from java.util.Observer and Model from java.util.Observable; Then when the model will dispatch the event it will call the update method on the Main class. I found really ugly and not elegant at all. I can’t even think of how I could work with this;

Is there any cleaner and flexible ways, maybe some libs to help me out here, because I have not found any acceptable tutorial about how to do it like this?

thanks a lot

Well what I’ve managed so far, and I quite I like it a lot more than creating “empty” classes just for simple events (but still not good, at least for me):

private ArrayList __items;
public void addListener(Method method, Object object){
    this.__listeners.add(new Object[] {method, object});
}


public void dispatch(){
    int i = this.__listeners.size();
    Method method;
    Object context;
    while(i>0){
        i--;
        method = (Method)(this.__listeners.get(i))[0];
        context = (Object)(this.__listeners.get(i))[1];
        try{
            method.invoke(context);
        }catch(java.lang.reflect.InvocationTargetException e){

        }catch(java.lang.IllegalAccessException e){

        }
    }
}

Then I use like this:

Gifts gifts = prendastotty.PrendasTotty.getMain().getLoggedUserGifts();
Class[] parameterTypes = new Class[0];
try{
    Method m = Home.class.getMethod("__updateTable", parameterTypes);
    gifts.addListener(m, this);
}catch(NoSuchMethodException e){

}

It this leaky/anti-pattern/buggy?

  • 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-15T13:44:14+00:00Added an answer on June 15, 2026 at 1:44 pm

    I must say that I had a bit of trouble keeping up with your code because in my head some of the stuff didn’t make sense (from a Java way of thinking, or at least my Java way of thinking). So I hope I understood you correctly and can help you out.

    Let’s first take your simple example:

    var model = new Model();
    this.callback = function(e){/* do something */}
    model.addListener("change", callback);
    

    In Java a good approach,for example, would be:

    public interface ModelListener {
       public void execute(Model context);
    }
    
    public class Model { 
        private List<ModelListener> listeners;
    
        public Model() {
          this.listeners = new ArrayList<ModelListener>();
        }
    
        public void addListener(ModelListener listener) {
          this.listeners.add(listener);
        }
    
        public void dispatch() {
          for (ModelListener listener: listeners) {
            listener.execute(this);
          }
        }
    

    }

    With this sort of design you can now do one of two things:

    Use anonymous classes

    In Java the most common case is that all your classes have a name, although there are cases when you can create anonymous classes, these are basically classes that
    are implemented inline. Since they are implemented inline, they’re usually only
    used when they’re small and it’s known they won’t be re-usable.

    Example:

    Model model = new Model();
    model.add(new ModelListener() {
         public void execute(Model model) { /* do something here */ }
    });
    

    Notice how the new ModelListener object is created (which is an interface) and the execute implementation is provided inline. That is the anonymous class.

    Interface Implementations

    You can create classes that implement your interface and use them instead of anonymous classes. This approach is often use when you want your listeners to be re-usable, have names that give semantic meaning to the code and/or they’re logic isn’t just a few lines of code.

    Example:

    public class LogListener implements ModelListener {
    
     public void execute(Model model) {
       // Do my logging here
     }
    }
    
    Model model = new Model();  
    model.addListener(new LogListener());
    

    Side note

    As a side note, I saw that the method you were trying to bind as a listener was called __updateTable are you by any chance trying to detect object’s changes so you can commit them to the database? If so I strongly suggest you to look at some ORM frameworks such as Hibernate or JPA they’ll keep all that hassle from you, keeping track of changes and committing them to the database.

    Hope it helps, regards from a fellow portuguese StackOverflow user 😉

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

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like to count the length of a string with PHP. The string

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.