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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:03:51+00:00 2026-05-17T01:03:51+00:00

ok, so I am stuck at the observer pattern here, almost all tutorials I

  • 0

ok, so I am stuck at the observer pattern here, almost all tutorials I read tell the subject class to subscribe the observer(s).

But with encapsulation in mind, how is this not tighlty coupled? They still depend on each other know, don’t they?

What I mean to say is that the subject Class must know the observer object to add him to the list of objects to notify.

Therefore a dependency is created, right?

What’s the mistake I make?

Thanks!

Thanks everybody for the replies,

Now I have some new questions. If I understand correctly the best way to deal with this is with Interfaces. So I will do that 😉

But, Why are the always talking about delegates and events? events ARE a form of delegates. So why aren’t they just saying events?

  • 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-17T01:03:52+00:00Added an answer on May 17, 2026 at 1:03 am

    When you say “know”, you’re right that the publisher must know about the observer in order to publish information to it.

    However, it doesn’t need to “know” about it in the sense that it is hardcoded that:

    • The observer is always going to be this particular class
    • There is always going to be these particular observers available

    In its basic form, events are publisher/observer in play, so you can easily do this just with events:

    public class Observer
    {
    }
    
    public class Publisher
    {
        public event EventHandler SomethingHappened;
    }
    

    You would then make the observer handle that event:

    public class Observer
    {
        public Observer(Publisher pub)
        {
            pub.SomethingHappened += Publisher_SomethingHappened;
        }
    
        private void Publisher_SomethingHappened(object sender, EventArgs e)
        {
        }
    }
    
    public class Publisher
    {
        public event EventHandler SomethingHappened;
    }
    

    Whenever this event is raised from the publisher, the observer is informed about it. Realize that the act of hooking into an event is to “tell” that class about the observer, but the publisher doesn’t have any hardcoded information about the publisher, except that there is someone out there listening.

    A different way would be using interfaces:

    public class Observer : IObserver
    {
        public Observer(Publisher pub)
        {
            pub.Observers.Add(this);
        }
    
        void IObserver.SomethingHappened()
        {
        }
    }
    
    public class Publisher
    {
        public List<IObserver> Observers { get; private set; }
    }
    
    public interface IObserver
    {
        void SomethingHappened();
    }
    

    Again, the publisher will “know” about the observer in the sense that it has a reference to it, but again it has no hardcoded information about which class or how many instances there will be.

    Just a word of warning: The code above is very flawed, at a minimum you should ensure that the observer “unhooks” from the publisher when you’re done, otherwise you’re going to have leaks in the system. If you don’t understand what I mean by that, leave a comment and I’ll edit in an example.

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

Sidebar

Related Questions

I've been implementing a barebones observer pattern and am stuck on a somewhat cryptic
I am trying to send some data using NSNotification but get stuck. Here is
Taking my first steps with Rx I am stuck here: public class DisposableResourceDemo :
Kinda stuck here... I have an application with lets say 5000 rows of data
Im stuck with this little project in C# but basically my problem is this:
Got stuck here: http://jsfiddle.net/UFkg8/ Right now the animation is top-down. What do I need
I'm stuck on a problem for hours now and having read everything about this
I've followed all the tutorials I could find on 'adding a mass action' example
Stuck on KVCs in Obj-C again. I am wanting to use KVC to find
Been stuck on this for ages. Currently learning Django and done some bits on

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.