<?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 Nirmal Nepal on Medium]]></title>
        <description><![CDATA[Stories by Nirmal Nepal on Medium]]></description>
        <link>https://medium.com/@rockink?source=rss-de21cee14c75------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*UCB4Fvrjzz-d4fm8.</url>
            <title>Stories by Nirmal Nepal on Medium</title>
            <link>https://medium.com/@rockink?source=rss-de21cee14c75------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 02:11:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rockink/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[I just wanted to go over Observable pattern, and since I am used to with Java the most, I will…]]></title>
            <link>https://medium.com/@rockink/i-just-wanted-to-go-over-observable-pattern-and-since-i-am-used-to-with-java-the-most-i-will-f9217d5a3235?source=rss-de21cee14c75------2</link>
            <guid isPermaLink="false">https://medium.com/p/f9217d5a3235</guid>
            <category><![CDATA[observer-design-pattern]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[design-patterns]]></category>
            <dc:creator><![CDATA[Nirmal Nepal]]></dc:creator>
            <pubDate>Wed, 11 Jan 2017 21:00:08 GMT</pubDate>
            <atom:updated>2017-01-11T21:00:08.671Z</atom:updated>
            <content:encoded><![CDATA[<p>I just wanted to go over Observable pattern, and since I am used to with Java the most, I will write code in Java. But, of course, if you know the concept behind this pattern, you can definitely use it in any language you like.</p><p>Say there is a <strong>School,</strong> and this school has many classes they run.From the student point of view, he/she is supposed to get the message sent by all the courses he/she is involved. So, say Tom is taking cs101, eng101 and phy101, he should be getting messages from these courses. If cs101 is not taken by Sam, he shouldn’t be getting any messages from him. In this case, the courses is <strong>Observable, </strong>where each student, observer, subscribes the change happening to specific course.</p><p>But, before I start with my own example, here is Observable and Observer in rough way. Java has it for you, you can use theirs, but if you want to create your own, you can do so.</p><pre><strong>import </strong>java.util.Observer;<br><br><strong>public class </strong>Observable {<br><br>    <em>//adds the observer<br>    </em><strong>public void </strong>addObserver(Observer observer);<br><br>    <em>//deletes<br>    </em><strong>public void </strong>deleteObserver(Observer observer);<br><br>    <em>//notifies<br>    </em><strong>public void </strong>notifyObservers(Object o);<br>}</pre><pre><br><strong>public interface </strong>Observer {<br>    <br>    <strong>public void </strong>update(Observable o, Object arg);<br>}</pre><p>I am going to design School, Students and Activities, using Javas implementation:</p><p>In the Object below, by implementing Observer, we are saying it to <strong>observe </strong>the change that is going to be received in the overridden method <strong>update</strong>. Later, we are going to create a <strong>Course, </strong>and we will design <strong>Course </strong>in such a way that it will be able to send message to <strong>Student.</strong></p><pre><strong>import </strong>java.util.Observable;<br><strong>import </strong>java.util.Observer;<br><br><strong>public class </strong>Student <strong>implements </strong>Observer {<br><br>    String <strong>name</strong>;<br><br>    <strong>public </strong>Student(String name) {<br>        <strong>this</strong>.<strong>name </strong>= name;<br>    }<br><br>    @Override<br>    <strong>public void </strong>update(Observable observable, Object o) {<br>           System.<strong><em>out</em></strong>.println(<br>               <strong>name </strong>+ <strong>&quot;: Notification recieved: &quot; </strong>+ (String) o <br>           );<br>        }<br>    }<br>}</pre><p>In Course, by extending <strong>Observable, </strong>we say that this is where the change of event/data takes place.</p><ol><li><strong>addObserver(student) </strong>tells the Observable object to know that Student wants to observe the change. <strong>deleteObserver(student) </strong>removes it.</li><li>In method, sendMessage, we call <strong>setChanged() </strong>to tell Observable that data is changed, and now, notify the observers using<strong> notifyObservers(s)</strong> with the new data.</li></ol><pre><strong>import </strong>java.util.Observable;<br><br><strong>public class </strong>Course <strong>extends </strong>Observable {<br><br>    <strong>public </strong>Course() {<br><br>    }<br><br>    <strong>public void </strong>sendMessage(String s){<br>        setChanged(); //let Observable know the change<br>        notifyObservers(s); //notify observers attached to it <br>    }<br><br>    <strong>public void </strong>addStudent(Student student) {<br>        addObserver(student); //add student as observer<br>    }<br><br><br>    <strong>public void </strong>removeStudent(Student student) {<br>        deleteObserver(student);//remove the student from observer<br>    }<br>}</pre><p>Now the main activity, where we show how this works.</p><pre><strong>public class </strong>School {<br><br>    <strong>public static void </strong>main(String[] args) {<br><br>        Student tom = <strong>new </strong>Student(<strong>&quot;tom&quot;</strong>);<br>        Student sam = <strong>new </strong>Student(<strong>&quot;sam&quot;</strong>);<br>        Student leslie = <strong>new </strong>Student(<strong>&quot;leslie&quot;</strong>);<br><br>        Course cs101 = <strong>new </strong>Course();<br>        cs101.addStudent(tom);<br>        cs101.addStudent(leslie);<br><br>        Course eng101 = <strong>new </strong>Course();<br>        eng101.addStudent(tom);<br>        eng101.addStudent(sam);<br><br>        Course phy101 = <strong>new </strong>Course();<br>        phy101.addStudent(sam);<br>        phy101.addStudent(leslie);<br>        phy101.addStudent(tom);<br><br><br><br>        cs101.sendMessage(<strong>&quot;No class tomorrow&quot;</strong>);<br>        eng101.sendMessage(<strong>&quot;5 essays due tonight&quot;</strong>);<br>        phy101.sendMessage(<strong>&quot;Quizes tomorrow!&quot;</strong>);<br><br>    }<br>}</pre><p>Output:</p><pre>leslie: Notification recieved: No class tomorrow<br>tom: Notification recieved: No class tomorrow<br>sam: Notification recieved: 5 essays due tonight<br>tom: Notification recieved: 5 essays due tonight<br>tom: Notification recieved: Quizes tomorrow!<br>leslie: Notification recieved: Quizes tomorrow!<br>sam: Notification recieved: Quizes tomorrow!</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f9217d5a3235" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>