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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:48:37+00:00 2026-06-16T06:48:37+00:00

Currently I have faced the following problem: we use some third-party libs in our

  • 0

Currently I have faced the following problem: we use some third-party libs in our project and there is a model which supports adding and removing (not getting!) certain type of listeners. And the problem is to add the listener then to remove it somewhere else, where we don’t have its instance. One of the solutions was: to make it singleton so you can add it and remove the same instance (but this is ugly, imho). The second one was the following:

class MyListener implements CoolThirdPartyModelListener{
...
  @Override
  public boolean equals(Object obj){
    if (obj == null){
      return false;
    }
    if(obj == this){
      return true;
    }
    return obj instanceof MyListener;
  }
}

And remove it from the listeners list just by calling:

coolThirdPartyModelInstance.removeListener(new MyListener());

But the second one has the ugly equals() method:( And I have been told that its kind tricky to remove listeners that way.

So I want to ask about your opinion about these two variants and maybe (that would be fantastic) you could suggest something more cool then I have found.

  • 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-16T06:48:38+00:00Added an answer on June 16, 2026 at 6:48 am

    Instead of using singleton listener, you can introduce some sort of singleton manager, something like this:

    interface CoolThirdPartyListenerManager {
        public void addListenerForModel(CoolThirdPartyModel model, CoolThirdPartyModelListener listener);
        /*
         * return value should indicate success of operation, i.g. return false if no
         * listeners were added to model.
         * it can also throw some sort of exception, if it suits your needs better.
         */
        public boolean removeListenerFromModel(CoolThirdPartyModel model);
    }
    

    (or you can make a class with bunch of static methods)

    Internally implementation can have something like

    private Map<CoolThirdPartyModel, CoolThirdPartyModelListener> mAddedListeners;
    

    which would store instances of listeners, which were added for certain model.

    Then in removeListenerFromModel() you can just get listener, that was added for that model from the Map and remove it.

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

Sidebar

Related Questions

Currently im faced with the following problem: I have a script that searches through
I currently face the following problem: I have written a Java program that parses
I currently have a table of concentrations, which are linked to a table of
I currently have one project that currently contains multiple packages. These packages make up
I've faced the following problem. I'm developing a form for the site and this
I find myself often faced with this problem: I have a dictionary where the
I have the following usecase: A div with some buttons When the user clicks
I have the following problem. As you know ie current versions do not support
I often run in the following problem, I have a web page with the
I have the following setup: testBean.java with a method public String getResult() , which

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.