2

I following all the examples from Head First Design Patterns book, I'm stand on second chapter: Observer Pattern. The basic (dirty example (made hand)) included in the first section of that chapter is easy to implement in C# (from Java code example). Check it out:

It is working correctly. Now my question is about if there are built-int equivalents to Observable class and Observer interface from Java in C#. See this image (taken from Head First Design Patterns)

I tried various tutorials, guides, and other resources to make that implementation.

Regards, John Ortiz

Apurv
  • 3,615
  • 3
  • 28
  • 49
InfZero
  • 2,424
  • 4
  • 21
  • 28
  • 2
    Looks pretty much like the closest "built in" thing would be standard events... only that events don't use the overhead of "classes". The event provider would be the "observable" and the receiver the "observer" if I get that pattern correctly. The contract they share is essentially the "EventArgs". Also, events can be part of interface declarations. I'm sure some Java migrated developer has implemented a library to do it "the Java way", but out of the box? Not that I know of. – Roman Gruber Mar 13 '13 at 03:43
  • 1
    Actually, C# "events" and "delegates" are very rich. For most "real world" scenarios, I would certainly look to events first. Please read these links: [Events Tutorial](http://msdn.microsoft.com/en-us/library/aa645739%28v=vs.71%29.aspx), [Multicast Delegates](http://msdn.microsoft.com/en-us/library/ms173175%28v=vs.80%29.aspx). IMHO... – paulsm4 Mar 13 '13 at 04:02

2 Answers2

2

The Observer pattern is just that...a pattern. You can implement the pattern with classes and interfaces in C# just as you would in Java

http://www.fatagnus.com/the-observer-pattern-in-csharp/

To answer your question, no, there is no reserved keyword in C# that is the equivalent of the Java keyword. That said, you can do the exact same thing with classes/interfaces/events. Here is an excellent example here on Stack Overflow by Jon Skeet.

using System;

class Observable
{
    public event EventHandler SomethingHappened;

    public void DoSomething()
    {
        EventHandler handler = SomethingHappened;
        if (handler != null)
        {
            handler(this, EventArgs.Empty);
        }
    }
}

class Observer
{
    public void HandleEvent(object sender, EventArgs args)
    {
        Console.WriteLine("Something happened to " + sender);
    }
}

class Test
{
    static void Main()
    {
        Observable observable = new Observable();
        Observer observer = new Observer();
        observable.SomethingHappened += observer.HandleEvent;

        observable.DoSomething();
    }
}

Super-simple example of C# observer/observable with delegates

http://yoda.arachsys.com/csharp/events.html

Community
  • 1
  • 1
David L
  • 28,075
  • 7
  • 54
  • 84
  • I'm not searching for a keyword, what I am looking is a Class/Interface (Observable/Observer) equivalents in C#. Thanks. – InfZero Mar 13 '13 at 03:47
  • 1
    @JohnOrtiz John, the links provide you with several examples and I particularly recommend you look at Jon Skeet's answer. I have updated my answer with it for your benefit. – David L Mar 13 '13 at 03:50
0

Yes, starting from .NET Framework 4 there are built-int equivalents to Observable class and Observer interface in Java. However they are both interfaces IObserver, IObservable and they are generic - that makes them quite powerful. Details how to use them are there https://msdn.microsoft.com/library/ee850490(v=vs.110).aspx.

Of course instead of implementing these interfaces you can still write your own, or use events/delegates approach.

tytyryty
  • 653
  • 7
  • 15