10

In reading about the Observer design pattern, I noticed that it is implemented using interfaces. In Java, the java.util.observable implementation is also a class. Shouldn't the C# and Java versions use interfaces ?

Scott

Scott Davies
  • 3,665
  • 6
  • 31
  • 39
  • 1
    How can you "implement as an interface"? If something is already implemented, it is a class, isn't it? An interface isn't implemented on its own. – Kobi Jul 25 '10 at 06:58
  • 3
    However, the meaning of the question stands. There is a huge gap in the .net collection object graph with ObservableCollection. Its functionality should have been separated into different interfaces in order to allow extensibility in a more flexible way. For example, if I implement an ObservableDictionary : INotifyCollectionChanged, I cannot use it as a source for ReadOnlyObservableCollection. The current implementation is not pattern based and does not support pattern based development at all. – Daniel Leiszen Oct 28 '15 at 16:50

2 Answers2

13

Well, it implements INotifyCollectionChanged and INotifyPropertyChanged. However, interestingly, it doesn't implement the new IObservable<T> interface from .NET 4.0, which you might have expected.

It would arguably be useful for there to be a generic form of INotifyCollectionChanged... but I don't know of one.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    So, I actually tried this, and it ends up being really ugly if you do it directly since any class that implements both IObservable and IEnumerable get *both* Rx and Linq extensions - lots of casting and gnashing of teeth. However, you could implement AsObservable() as an Extension Method pretty easily – Ana Betts Jul 27 '10 at 23:50
2

But they DO use interfaces. The ObservableCollection in .NET is an implementation of the interfaces - you are free to ignore it and to your own implementation.

TomTom
  • 61,059
  • 10
  • 88
  • 148