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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:44:44+00:00 2026-05-26T08:44:44+00:00

I am a big fan of the Observer pattern. In our code we use

  • 0

I am a big fan of the Observer pattern. In our code we use it in many places to decouple services from each other. However, I’ve seen it implemented poorly in many places since there is a lot to worry about:

  • Exception handling – don’t want listeners throwing runtime exceptions around.
  • Long-running listeners holding up the main thread
  • Concurrent modification of the listener list as we are iterating through it.

What’s more, we end up repeating this code all over the place. In the spirit of DRY, I want to pull out all Notification concerns into a single service. Some pseudo code:

Interface NotificationService
   // register the listener to receive notifications from this producer
   registerAsListener (NotificationProducer, NotificationListener)

   // Sends a notification to listeners of this producer
   sendNotification (NotificationProducer, Notification)

   // Sends a notification in a background thread
   sendAsynchNotification (NotificationProducer, Notification)

   // Listener no longer receives messages from this producer
   removeListener(NotificationProducer, NotificationListener)

My question is this: am I losing the original point of the observer pattern by doing this? Am I making a mistake by introducing another dependency on both sides of the pattern? Both the Listener and the Producer will now have an extra dependency on NotificationService.

What are your views?

  • 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-26T08:44:45+00:00Added an answer on May 26, 2026 at 8:44 am

    you are right with your concerns and questions.
    implementing the observer pattern many times seems like plain repetition.
    you’re also right that the above solution does lose the pattern’s objective.

    what you’ve just implemented is a (global?) event bus. it’s a matrix of producers and listeners. that’s useful for many applications (see GWT’s event bus ).

    however if you just want to minimize code duplication while maintaining the pattern. you can remove the coupling between the listeners and the service, use a minified version of the above interface as a member of the observed class. so the logic of registration and notification is written once.
    the observed class is just delegating the registration and notification logic to the service.

    class ObservedClass implements Observable {
        NotificationService notificationService = new NotificationServiceImpl (this);
        ....
     }
    
    interface NotificationService {
       // register the listener to receive notifications from this producer
       registerAsListener ( NotificationListener)
    
       // Sends a notification to listeners of this producer
       sendNotification (Notification)
    
       // Sends a notification in a background thread
       sendAsynchNotification (Notification)
    
      // Listener no longer receives messages from this producer
      removeListener(NotificationListener)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am not a big fan of datasets so I use POCO to return
As a big fan of Charles Petzold's books Code and The Annotated Turing I
I'm a big fan of MSpec so naturally I wanted to use is right
I'm a big fan of ReSharpers cleanup code feature. Especially the Solution wide clean
I have the following code but I'm not a big fan of reg exp
Like most of us, I am a big fan of improving efficiency of code.
I'm a big fan of the ruby way. However today it got in my
I'm big fan of the MVVM pattern, in particular while using the ASP.NET MVC
I'm a big fan of ctags, but sometimes it is hard to use it.
Hello I am a big fan of Git and Hg and have to use

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.