<?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 A.Mahdy AbdelAziz on Medium]]></title>
        <description><![CDATA[Stories by A.Mahdy AbdelAziz on Medium]]></description>
        <link>https://medium.com/@amahdy?source=rss-d818bea55e0b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*nE_8opJhgWzl-bSETm3hiw.png</url>
            <title>Stories by A.Mahdy AbdelAziz on Medium</title>
            <link>https://medium.com/@amahdy?source=rss-d818bea55e0b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 26 Jun 2026 22:27:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amahdy/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[Events Between Java Components]]></title>
            <link>https://medium.com/97-things/events-between-java-components-39c9c0f41e55?source=rss-d818bea55e0b------2</link>
            <guid isPermaLink="false">https://medium.com/p/39c9c0f41e55</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[events]]></category>
            <category><![CDATA[97-things]]></category>
            <dc:creator><![CDATA[A.Mahdy AbdelAziz]]></dc:creator>
            <pubDate>Fri, 21 Feb 2020 15:02:08 GMT</pubDate>
            <atom:updated>2020-02-21T15:26:39.591Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<p>One of the core concepts of object orientation in Java is that every class can be considered to be a <em>component</em>. Components can be extended or included to form bigger components. The final application is also considered a component. Components are like Lego blocks that build up a bigger structure.</p><p>An event in Java is an action that changes the state of a component. For example, if your component is a button, then clicking on that button is an event that changes the state of the button to be <em>clicked</em>.</p><p>Events do not necessarily happen only on visual components. For example, you can have an event on a USB component that a <em>device is connected</em>. Or an event on a network component that <em>data is transferred</em>.</p><p>Events help to decouple the dependencies between components. Consider the following example.</p><p>Assume we have an <em>Oven</em> component and a <em>Person</em> component. They are two components that exist in parallel and work independently of one another. We should not make <em>Person</em> part of <em>Oven</em>, nor the other way around. To build a smart house, we want the <em>Oven</em> to prepare food once <em>Person</em> is hungry. Here are possible implementations:</p><ol><li><em>Oven</em> checks <em>Person</em> in fixed, short intervals. This not only annoys <em>Person</em>, but is also expensive for the <em>Oven</em> if we want it to check on multiple instances of <em>Person</em>.</li><li><em>Person</em> comes with a public event <em>Hungry</em>, to which <em>Oven</em> is subscribed. Once <em>Hungry</em> is fired, <em>Oven</em> is notified immediately and starts preparing food.</li></ol><p>The second solution uses the event architecture to handle the listening and communication between components efficiently and without a direct coupling between Person and <em>Oven</em>, because <em>Person</em> will fire the event, and any component <em>Oven</em>, <em>Fridge</em>, and <em>Table</em> can listen to that event without any special handling from the <em>Person</em> component.</p><p>Implementing events for a Java component can take different formats, depending on how they are expected to be handled, and the logic of the component. Here is an example showing a minimal code to implement a <em>HungerListener</em> in the <em>Person</em> component:</p><p>First, create a listener interface:</p><pre>@FunctionalInterface<br>public interface HungerListener {<br>    void hungry();<br>}</pre><p>Then, in the <em>Person</em> class, define a list to store the listeners:</p><pre>private List&lt;HungerListener&gt; listeners = new ArrayList&lt;&gt;();</pre><p>Define an API to insert a new listener:</p><pre>public void addHungerListener(HungerListener listener) {<br>    listeners.add(listener);<br>}</pre><p>You can create a similar API for removing a listener.</p><p>Also, add a method to trigger the action of being hungry, that notifies all the listeners about the event:</p><pre>public void becomesHungry() {<br>    for (HungerListener listener : listeners)<br>        listener.hungry();<br>}</pre><p>Finally, from the <em>Oven</em> class, add code that listens to the event and implements the action when the event is fired:</p><pre>Person person = new Person();<br>person.addHungerListener(() -&gt; {<br>    System.err.println(“The person is hungry!”);<br>    // Oven takes action here<br>});</pre><p>And to try it out:</p><pre>person.becomesHungry();</pre><p>For a fully decoupled code, the last section should be in an independent class that has an instance of <em>Person</em> and <em>Oven</em>, and handles the logic between them. Similarly, we can add other actions for <em>Fridge</em>, <em>Table</em>, ..etc. They all will get notified only once the <em>Person</em> <em>becomesHungry</em>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=39c9c0f41e55" width="1" height="1" alt=""><hr><p><a href="https://medium.com/97-things/events-between-java-components-39c9c0f41e55">Events Between Java Components</a> was originally published in <a href="https://medium.com/97-things">97 Things</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>