Observer Pattern
Tags: programming, computerscience, codequality, oop
Published: 2021-05-15 20:04:15

<div class="modal-body">
<p>One of my most commonly used design patterns is the <a href="https://docs.microsoft.com/en-us/dotnet/standard/events/observer-design-pattern">Observer Pattern</a> and for good reasons. At its core, it allows you to program easier and worry less about how objects will get updated. For example, when programming using the MVC framework, you often want the views to be notified when the model’s state changes. <a href="https://medium.com/@jackwong_60367/observer-pattern-in-react-using-redux-ca9998e885b0">React uses this same pattern</a> under the hood to update components when the state changes. <a href="https://reactjs.org/docs/hooks-intro.html">React hooks</a> are also another cool way to use the pattern. With it being used so frequently in multiple programming languages/frameworks, I thought it would be beneficial to do a quick recap of what the pattern is and how it&nbsp;works!</p>
<blockquote>
<em>“Observer Pattern Intent — Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.” </em><strong><em>— Design Patterns: Elements of Reusable Object-Oriented Software</em></strong>
</blockquote>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*9kgiYUkANnktw0ji"><figcaption>Source: <a href="https://softwareengineering.stackexchange.com/questions/389559/about-observer-interface-in-observer-pattern-of-gof">Software Stack&nbsp;Exchange</a></figcaption></figure><h3><strong>Why is the Observer Pattern Important?</strong></h3>
<p>This pattern is critical to building robust applications because it decouples the implementation of the Subject from the Observer. This is pivotal because without this, you could have a huge dependency tree that would often have to violate <a href="https://stackify.com/oop-concept-for-beginners-what-is-encapsulation/">encapsulation</a>. The benefit in the future is that you can reuse the subjects without having to reuse the observers, and vice versa. You can also add observers without having to modify the subject or mess with any of the other observers. This plug and play attribute is highly desirable when trying to build production applications.</p>
<h3><strong>Implementation</strong></h3>
<p>Let’s walk through a quick example of how to practically use the Observer pattern in c#. This is most commonly done by handling and raising events.&nbsp;.Net events follow the delegate model and it enables an Observer to register and receive notifications from a Subject when an event is fired. Let’s walk through an example of a&nbsp;Clock.</p>
<p>Suppose you wanted to build a Clock app with some Alarm functionality. You could implement a Clock.cs that handles when the alarm time is&nbsp;reached.</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*_lFemxp-j1_GbMFt"></figure><p>The benefit to this is that in your main program, you can simply specify the time you want the alarm to go off, and then wait for the event to be&nbsp;raised.</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*fKa31yXZ3KnFuVgt"></figure><p>Notice that after the event is raised in ‘OnAlarmRaised’ I unsubscribe from the event. This is considered a good practice as you never know when events could be fired, and forgetting to unsubscribe could lead to memory&nbsp;leaks.</p>
<p>I realize that this example deviates from the typical example of the observer pattern, but it goes to show that the pattern isn’t always one for one with the book definition. I think that the example I showed is a cleaner example that requires less code overall&nbsp;🙂</p>
<h3><strong>Conclusion</strong></h3>
<p>The observer pattern might be confusing the first couple of times you set out to use it, but will provide great advantages down the road. Give it a try in your next&nbsp;project!</p>
<p><strong>Follow me on </strong><a href="https://twitter.com/maurerpwer"><strong>Twitter</strong></a><strong> or </strong><a href="https://dev.to/mwrpwr"><strong>DEV</strong></a><strong> for more posts like this&nbsp;one!</strong></p>
<img src="https://medium.com/_/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=c5b98b40753e" width="1" height="1" alt="">
</div>