<?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>Wed, 08 Apr 2026 16:24:56 +0000</pubDate>
    <item>
      <title>Design Pattern - Observer</title>
      <link>https://sukhpinder.writeas.com/design-pattern-observer-ktnr?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[Design Pattern - Observer&#xA;&#xA;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.&#xA;&#xA;Photo by Christina Morillo from [Pexels](https://cdn-images-1.medium.com/max/2560/1ELcUZLT-wWFd3womEweBA.jpeg)&#xA;&#xA;In other words, a change in one object initiates the notification in another object.&#xA;&#xA;Use Case&#xA;&#xA;Let’s take an example of an Instagram celebrity influence who has “x” number of followers. So the moment the celebrity adds a post, then all the followers are notified.&#xA;&#xA;Let us implement the aforementioned use case using Observer Design Pattern.&#xA;&#xA;Prerequisites&#xA;&#xA;Basic knowledge of OOPS concepts.&#xA;&#xA;Any programming language knowledge.&#xA;&#xA;The article demonstrates the usage of observer design patterns using the C# programming language. So, to begin with, C#&#xA;[Introduction to C#&#xA;C# has been around for quite some period, and it continues to develop, obtaining more enhanced features.medium.com](https://medium.com/c-sharp-progarmming/introduction-to-c-222567161964)&#xA;&#xA;Learning Objectives&#xA;&#xA;How to code using an observer design pattern?&#xA;&#xA;Getting Started&#xA;&#xA;According to the use case, the first implement an interface that contains what actions a celebrity can perform. It is known as “Subject.”&#xA;    public interface ICelebrityInstagram{&#xA;     string FullName { get; }&#xA;     string Post { get; set; }&#xA;     void Notify(string post);&#xA;     void AddFollower(IFollower fan);&#xA;     void RemoveFollower(IFollower fan);&#xA;    }&#xA;The Subject contains the following member functions.&#xA;&#xA;Notify: To notify all the followers.&#xA;&#xA;AddFollower: Add a new follower to the celebrity list.&#xA;&#xA;RemoveFollower: Remove a follower from the celebrity list.&#xA;&#xA;Now implement the observer “IFollower” interface, which contains the “Update” member function for notification.&#xA;    public interface IFollower{&#xA;     void Update(ICelebrityInstagram celebrityInstagram);&#xA;    }&#xA;Finally, it&#39;s time to implement “Concrete Implementation” for both “Subject” and “Observer.”&#xA;&#xA;ConcreteObserver named “Follower.cs”&#xA;&#xA;It provides an implementation of the Update member function, which outputs the celebrity name &amp; post to the console.&#xA;&#xA;https://gist.github.com/ssukhpinder/c98ce6c8c56113f6335a34f9782a3f49&#xA;&#xA;ConcreteSubject named “Sukhpinder.cs”&#xA;&#xA;https://gist.github.com/ssukhpinder/da667168bdc1f77333a201c87abd6b78&#xA;&#xA;How to use observer pattern?&#xA;&#xA;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.”&#xA;https://gist.github.com/ssukhpinder/78c9f9c7986b4b006a0d5eb48afcb1de&#xA;&#xA;Output&#xA;&#xA;Github Repo&#xA;The following repository shows the above use case implementation using an Observer Design Pattern in the console-based application.&#xA;ssukhpinder/ObserverPattern&#xA;&#xA;Thank you for reading, and I hope you liked the article. Please provide your feedback in the comment section.&#xA;&#xA;Follow me on&#xA;&#xA;C# Publication, LinkedIn, Instagram, Twitter, Dev.to, Pinterest, Substack, Wix.&#xA;&#xA;More design patterns&#xA;[Design Pattern — Abstract Factory&#xA;According to Gang of Four, abstract factory patterns can be assumed as the factory for creating factories.medium.com](https://medium.com/c-sharp-progarmming/abstract-factory-design-pattern-15a66a21ee13)&#xA;[Design Pattern — Factory Method&#xA;According to the Gang of Four, the factory method allows the subclass to determine which class object should be…medium.com](https://medium.com/c-sharp-progarmming/factory-method-design-pattern-8994b9287265)&#xA;[Design Pattern — Singleton&#xA;Gang of Four — Singleton design pattern ensures that a particular class has only one instance/object and a global…medium.com](https://medium.com/c-sharp-progarmming/singleton-design-pattern-76a1e53f5ed2)&#xA;[Design Pattern — Decorator Pattern&#xA;According to Gang of Four, the pattern adds extra responsibilities to a class object dynamically.medium.com](https://medium.com/c-sharp-progarmming/decorator-design-pattern-55cad1265091)&#xA;[Design Pattern - Iterator&#xA;According to Gang of Four, the iterator pattern provides a process to obtain the aggregator object without knowing its…medium.com](https://medium.com/geekculture/iterator-design-pattern-6378b8ccd335)&#xA;]]&gt;</description>
      <content:encoded><![CDATA[<h3 id="design-pattern-observer" 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" 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" 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"><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" id="learning-objectives">Learning Objectives</h2>
<ul><li>How to code using an observer design pattern?</li></ul>

<h2 id="getting-started" 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" 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" 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">https://gist.github.com/ssukhpinder/c98ce6c8c56113f6335a34f9782a3f49</a></p>

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

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

<h3 id="how-to-use-observer-pattern" 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">https://gist.github.com/ssukhpinder/78c9f9c7986b4b006a0d5eb48afcb1de</a></p>

<h3 id="output" 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" 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">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" id="follow-me-on">Follow me on</h3>

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

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

<p><a href="https://medium.com/c-sharp-progarmming/abstract-factory-design-pattern-15a66a21ee13" rel="nofollow"><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"><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"><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"><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"><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>