The Observer Design Pattern in .NET

The Observer Design Pattern in .NET

This post was originally created in the Productiove C# Blog in 2012.

Observer Pattern? Sounds complicated??Reality is that if you are a .NET developer you already know it and it is very simple.

The observer pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

But but but I can do this with events!

Oh yes :)

Events (and delegates) are the idiomatic way to implement the Observer Pattern in .NET.

Nothing new then.

Let's see an example just as a reminder.

Let's say?I want to implement a class that generate numbers every so often (this can be a class that read data from a sensor for example) and notify clients when new numbers are generated.

This a possible implementation:

No alt text provided for this image
No alt text provided for this image

Considering the new features of C# 3.5 and LINQ that made the language more functional, an another way to implement this pattern is using delegates directly.?

No alt text provided for this image
No alt text provided for this image

The are more ways to implement this pattern but you got the idea. The framework brilliantly solve this problem and these techniques are actually used heavily in many contexts.

The main advantage of using this pattern is that the resulting system is loosely coupled.?In this example, the number generator know nothing about the observers.

This is an?important Object Oriented Design principle:?Low Coupling.?


要查看或添加评论,请登录

Andrea Angella的更多文章

  • 15 reasons why you should learn C# in 2024

    15 reasons why you should learn C# in 2024

    Why you should learn C#? This is the question I want to answer in this article. I am Andrea, a Microsoft MVP, Technical…

    8 条评论
  • The importance of defining performance goals

    The importance of defining performance goals

    This post was originally created in the Productive C# blog in 2016. Let’s start from some questions: Is you application…

  • The Singleton Pattern in .NET – Avoid it if you can!

    The Singleton Pattern in .NET – Avoid it if you can!

    This post was originally created in the Productive C# blog in 2012. The Singleton Pattern ensures a class has only one…

    3 条评论
  • Factory Method Pattern in .NET

    Factory Method Pattern in .NET

    This post was originally written on the Productive C# blog in 2012. There is a little bit of confusion around this…

  • The Command Pattern in .NET

    The Command Pattern in .NET

    The post was originally written on the Productive C# blog in 2012. The Command Pattern encapsulates a request as an…

    4 条评论
  • The Adapter Pattern in .NET

    The Adapter Pattern in .NET

    This post was originally written in the Productive C# blog in 2012. The Adapter Pattern converts the interface of a…

    2 条评论
  • The Facade Pattern in .NET

    The Facade Pattern in .NET

    This post was originally written on the Productiove C# blog in 2012. The Facade Pattern provides a unified interface to…

  • The Template Method Pattern in .NET

    The Template Method Pattern in .NET

    This post was originally written in the Productive C# blog in 2013. The Template Method Pattern defines the skeleton of…

    1 条评论
  • The Iterator Pattern in .NET

    The Iterator Pattern in .NET

    This post was originally written in the Productive C# blog in 2013. The Iterator Pattern provides a way to access the…

    1 条评论
  • Value Types and Reference Types in C#

    Value Types and Reference Types in C#

    This post was originally published on the Productive C# blog in 2019. Learn about the difference between value types…

社区洞察

其他会员也浏览了