<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>C# Programming</title>
    <link>https://sukhpinder.writeas.com/</link>
    <description>A passionate self-taught .Net developer from India. Focuses on ASP.Net | ASP.Net Core | .Net &amp; C# advance design, best practices &amp; experiences to make developer</description>
    <pubDate>Tue, 17 Nov 2026 00:16:00 +0000</pubDate>
    <item>
      <title>Design Pattern - Observer</title>
      <link>https://sukhpinder.writeas.com/design-pattern-observer-ktnr?pk_campaign=rss-feed</link>
      <description>Design Pattern – Observer According to Gang of Four, observer pattern defines dependency b/w two or more objects. So when one object stat...</description>
      <content:encoded><![CDATA[<h3 id="design-pattern-observer">Design Pattern – Observer</h3>

<p>According to Gang of Four, observer pattern defines dependency b/w two or more objects. So when one object state changes, then all its dependents are notified.</p>

<p><img src="https://cdn-images-1.medium.com/max/2560/1*ELcUZLT-wWFd3_womEweBA.jpeg" alt="Photo by [Christina Morillo](https://www.pexels.com/@divinetechygirl?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels) from [Pexels](https://www.pexels.com/photo/person-using-macbook-pro-on-person-s-lap-1181298/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels)"/></p>

<p>In other words, a change in one object initiates the notification in another object.</p>

<h2 id="use-case">Use Case</h2>

<p>Let’s take an example of an Instagram celebrity influence who has “<strong>x</strong>” number of followers. So the moment the celebrity adds a post, then all the followers are notified.</p>

<p>Let us implement the aforementioned use case using Observer Design Pattern.</p>

<h2 id="prerequisites">Prerequisites</h2>
<ul><li><p>Basic knowledge of OOPS concepts.</p></li>

<li><p>Any programming language knowledge.</p></li></ul>

<p>The article demonstrates the usage of observer design patterns using the C# programming language. So, to begin with, C#
<a href="https://medium.com/c-sharp-progarmming/introduction-to-c-222567161964" rel="nofollow ugc"><strong>Introduction to C#</strong>
<em>C# has been around for quite some period, and it continues to develop, obtaining more enhanced features.</em>medium.com</a></p>

<h2 id="learning-objectives">Learning Objectives</h2>
<ul><li>How to code using an observer design pattern?</li></ul>

<h2 id="getting-started">Getting Started</h2>

<p>According to the use case, the first implement an interface that contains what actions a celebrity can perform. It is known as “<strong>Subject</strong>.”</p>

<pre><code>    public interface ICelebrityInstagram{
     string FullName { get; }
     string Post { get; set; }
     void Notify(string post);
     void AddFollower(IFollower fan);
     void RemoveFollower(IFollower fan);
    }
</code></pre>

<h3 id="the-subject-contains-the-following-member-functions">The Subject contains the following member functions.</h3>
<ul><li><p><strong>Notify:</strong> To notify all the followers.</p></li>

<li><p><strong>AddFollower:</strong> Add a new follower to the celebrity list.</p></li>

<li><p><strong>RemoveFollower:</strong> Remove a follower from the celebrity list.</p></li></ul>

<p>Now implement the **observer **“IFollower” interface, which contains the “Update” member function for notification.</p>

<pre><code>    public interface IFollower{
     void Update(ICelebrityInstagram celebrityInstagram);
    }
</code></pre>

<p>Finally, it&#39;s time to implement “Concrete Implementation” for both “<strong>Subject</strong>” and “<strong>Observer</strong>.”</p>

<h3 id="concreteobserver-named-follower-cs">ConcreteObserver named “Follower.cs”</h3>

<p>It provides an implementation of the Update member function, which outputs the celebrity name &amp; post to the console.</p>

<p><a href="https://gist.github.com/ssukhpinder/c98ce6c8c56113f6335a34f9782a3f49" rel="nofollow ugc">https://gist.github.com/ssukhpinder/c98ce6c8c56113f6335a34f9782a3f49</a></p>

<h3 id="concretesubject-named-sukhpinder-cs">ConcreteSubject named “Sukhpinder.cs”</h3>

<p><a href="https://gist.github.com/ssukhpinder/da667168bdc1f77333a201c87abd6b78" rel="nofollow ugc">https://gist.github.com/ssukhpinder/da667168bdc1f77333a201c87abd6b78</a></p>

<h3 id="how-to-use-observer-pattern">How to use observer pattern?</h3>

<p>The following use case shows that whenever the below statement is executed sukhpinder.Post = “I love design patterns.”; the Update method is triggered for each follower, i.e., each follower object is notified of a new post from “Sukhpinder.”
<a href="https://gist.github.com/ssukhpinder/78c9f9c7986b4b006a0d5eb48afcb1de" rel="nofollow ugc">https://gist.github.com/ssukhpinder/78c9f9c7986b4b006a0d5eb48afcb1de</a></p>

<h3 id="output">Output</h3>

<p><img src="https://cdn-images-1.medium.com/max/2000/1*9yi84-01XfRT9ZnGo_UGCw.png" alt=""/></p>

<h3 id="github-repo">Github Repo</h3>

<p>The following repository shows the above use case implementation using an Observer Design Pattern in the console-based application.
<a href="https://github.com/ssukhpinder/ObserverPattern" rel="nofollow ugc">ssukhpinder/ObserverPattern</a></p>

<p>Thank you for reading, and I hope you liked the article. Please provide your feedback in the comment section.</p>

<h3 id="follow-me-on">Follow me on</h3>

<p><a href="https://medium.com/c-sharp-progarmming" rel="nofollow ugc">C# Publication</a>, <a href="https://www.linkedin.com/in/sukhpinder-singh-532284a2/" rel="nofollow ugc">LinkedIn</a>, <a href="https://www.instagram.com/sukhpindersukh/" rel="nofollow ugc">Instagram</a>, <a href="https://twitter.com/sukhsukhpinder" rel="nofollow ugc">Twitter</a>, <a href="https://dev.to/ssukhpinder" rel="nofollow ugc">Dev.to</a>, <a href="https://in.pinterest.com/ssukhpinder/_created/" rel="nofollow ugc">Pinterest</a>, <a href="https://sukhpinder.substack.com/" rel="nofollow ugc">Substack</a>, <a href="https://www.csharp-dotnet.com/" rel="nofollow ugc">Wix</a>.</p>

<h3 id="more-design-patterns">More design patterns</h3>

<p><a href="https://medium.com/c-sharp-progarmming/abstract-factory-design-pattern-15a66a21ee13" rel="nofollow ugc"><strong>Design Pattern — Abstract Factory</strong>
<em>According to Gang of Four, abstract factory patterns can be assumed as the factory for creating factories.</em>medium.com</a>
<a href="https://medium.com/c-sharp-progarmming/factory-method-design-pattern-8994b9287265" rel="nofollow ugc"><strong>Design Pattern — Factory Method</strong>
<em>According to the Gang of Four, the factory method allows the subclass to determine which class object should be…</em>medium.com</a>
<a href="https://medium.com/c-sharp-progarmming/singleton-design-pattern-76a1e53f5ed2" rel="nofollow ugc"><strong>Design Pattern — Singleton</strong>
<em>Gang of Four — Singleton design pattern ensures that a particular class has only one instance/object and a global…</em>medium.com</a>
<a href="https://medium.com/c-sharp-progarmming/decorator-design-pattern-55cad1265091" rel="nofollow ugc"><strong>Design Pattern — Decorator Pattern</strong>
<em>According to Gang of Four, the pattern adds extra responsibilities to a class object dynamically.</em>medium.com</a>
<a href="https://medium.com/geekculture/iterator-design-pattern-6378b8ccd335" rel="nofollow ugc"><strong>Design Pattern – Iterator</strong>
<em>According to Gang of Four, the iterator pattern provides a process to obtain the aggregator object without knowing its…</em>medium.com</a></p>
]]></content:encoded>
      <guid>https://sukhpinder.writeas.com/design-pattern-observer-ktnr</guid>
      <pubDate>Mon, 31 May 2021 17:09:39 +0000</pubDate>
    </item>
  </channel>
</rss>