<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by aswini jayaraman on Medium]]></title>
        <description><![CDATA[Stories by aswini jayaraman on Medium]]></description>
        <link>https://medium.com/@jayaraman.aswini?source=rss-36789221cf6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*pOwdFIHFpssku0XR</url>
            <title>Stories by aswini jayaraman on Medium</title>
            <link>https://medium.com/@jayaraman.aswini?source=rss-36789221cf6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 14:46:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jayaraman.aswini/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Subscribe to my channel to get all my updates…..]]></title>
            <link>https://medium.com/@jayaraman.aswini/subscribe-to-my-channel-to-get-all-my-updates-041b44b137e5?source=rss-36789221cf6------2</link>
            <guid isPermaLink="false">https://medium.com/p/041b44b137e5</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[observer-pattern]]></category>
            <category><![CDATA[design-patterns-in-js]]></category>
            <dc:creator><![CDATA[aswini jayaraman]]></dc:creator>
            <pubDate>Wed, 24 Apr 2024 11:40:54 GMT</pubDate>
            <atom:updated>2024-04-24T11:40:54.149Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><strong><em>Let’s say you have a special button in your app. You would like to understand how many times the special button is being clicked on your app. How many users per day uses that button! How would you do it? Attach a click event listener??? Mmmmm… May be!!!</em></strong></blockquote><p>What if the special button is used in 100’s of components….okay it’s a bit of an exaggeration. in 10–20 components??? would you attach event listener to every special button and repeat the same logic?? It’s doable.. but…..</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6TSKodnThXx3OOkQSCUsPg.jpeg" /><figcaption>Exactly!</figcaption></figure><p>You can use module pattern to provide some level of encapsulation and reusability. But what if you want to show a notification toast, send to analytics, do ’n’ number of stuff ? would you do everything inside the module? Well, you can certainly do that! However, what if some of your components want to just leverage notification toast functionality but not the analytics and all the other 100 things the module button is offering?? In that case, you can render different methods based on the component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*PF7EmR2FzYOcmJ2kfj6_OQ.jpeg" /><figcaption>Your common module will end up like this…non-refactorable..</figcaption></figure><p>What can we do to make it less messy?</p><p>tan ta daaaaai………Introducing the one and only Observer pattern to the rescue……. (ada ada ada)</p><p>There are only 2 actors in the pattern. Observable (the object that’s to be tracked for state changes) and observers (the objects that need to be notified of the changes in the observable). Observable is called the subject (confusingly…..🤦🏽‍♀️). But observable doesn’t need to be aware of who these observers are!!! Loosely coupled…..Which makes it easy to implement. any no of observers can be added that makes it extensible.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UMmcn8WSBX1iSJ68MJKXog.png" /><figcaption>Something like this…….</figcaption></figure><p>Here the speaker is talking to the audience. he doesn’t need to know the biography of each and everyone in the audience. Audience just receives updates from the speaker.</p><p>How this can be implemented in simple javascript file?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kWfMUvzTpod2_9BwIuQYfQ.jpeg" /></figure><pre>Observable.js<br><br>export class Observable {<br>    constructor() {<br>        this.observers = [];<br>    }<br><br>    subscribe(func) {<br>        this.observers.push(func);<br>    }<br><br>    unsubscribe(func) {<br>        this.observers = this.observers.filter(observer =&gt; observer !== func);<br>    }<br><br>    notify(data) {<br>        this.observers.forEach(observer =&gt; observer(data));<br>    }<br>}<br><br></pre><pre>App.js<br><br>import {Observable} from &quot;./Observable.js&quot;;<br><br>const observable = new Observable();<br><br>function logger(data) {<br>    console.log(&quot;logging at &quot;, new Date(), &quot;: &quot;, data);<br>}<br><br>observable.subscribe(logger);<br><br>document.getElementById(&quot;special-btn&quot;).addEventListener(&quot;click&quot;, () =&gt; {<br>    observable.notify(&quot;button is clicked&quot;);<br>});</pre><pre>index.html<br><br>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br>    &lt;head&gt;<br>        &lt;meta charset=&quot;UTF-8&quot;&gt;<br>        &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;<br>        &lt;title&gt;Module Example&lt;/title&gt;<br>    &lt;/head&gt;<br>    &lt;body&gt;<br>        &lt;button id=&quot;special-btn&quot;&gt;Click me&lt;/button&gt;<br>        &lt;script type=&quot;module&quot; src=&quot;./App.js&quot;&gt;&lt;/script&gt;<br>    &lt;/body&gt;<br>&lt;/html&gt;</pre><p>Directory structure:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*ki7k2bB1Z4_jeur9bu0ing.png" /><figcaption>Simple JS project</figcaption></figure><p>In this example, whenever the button is clicked, a simple console.log statement is executed.</p><p>logger function is an observer. It’s added to the observers array using <em>observable.subscribe(logger).</em></p><p>When the button is clicked, observable.notify(message), is called which in turn sends the message to the logger function.</p><p>A simple, yet powerful pattern that you can use in javascript.</p><p><strong>Where this pattern can be observed?</strong></p><blockquote>Some libraries such as Redux, Rx Js.</blockquote><p>Babaaayeeee!!!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=041b44b137e5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>