<?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[Conceptual Viewpoint - Medium]]></title>
        <description><![CDATA[Here we are going to dig deep into the theoretical concepts behind the actual logical implementations. - Medium]]></description>
        <link>https://medium.com/concept-viewpoint?source=rss----63c49cde3232---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Conceptual Viewpoint - Medium</title>
            <link>https://medium.com/concept-viewpoint?source=rss----63c49cde3232---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 06:58:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/concept-viewpoint" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[JavaScript Event Loop: Explained]]></title>
            <link>https://medium.com/concept-viewpoint/javascript-event-loop-explained-e3839f5abcff?source=rss----63c49cde3232---4</link>
            <guid isPermaLink="false">https://medium.com/p/e3839f5abcff</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[event-loop]]></category>
            <category><![CDATA[js]]></category>
            <dc:creator><![CDATA[Shamoda Jayasekara]]></dc:creator>
            <pubDate>Wed, 24 Mar 2021 18:37:40 GMT</pubDate>
            <atom:updated>2023-11-17T03:24:41.187Z</atom:updated>
            <content:encoded><![CDATA[<h4>The concept behind the Event Loop:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Cspk0V_yqJ8fHBkF37fUfQ.png" /></figure><p>You may already hear about the asynchronous nature of JavaScript. Well, the secret behind this asynchronous nature is the Event Loop. JavaScript event loop is a very simple concept to understand. In fact, you may see this pattern occurs in real life so many times. Let’s take it step by step.</p><p>We already know that JavaScript is a combination of synchronous and asynchronous events. Synchronous events occur one after another and asynchronous events are things like callbacks and promises.</p><p>However, let’s starts with synchronous events and discuss the concept of the <strong>Event Queue</strong>. Because it’s much easier to explain and understand. Then we can move to asynchronous events.</p><p>Let’s jump into a real-life example,</p><p>Imagine a <strong>restaurant with just one waiter</strong>. Imagine the life of that poor guy. 😰 There are few guests in the restaurant sitting on different tables. This waiter has to provide service to them all. Assume one guest raises his hand to place an order, then the waiter goes to his table to take the order. While the waiter taking the first order, another guest raises his hand to place an order. So, what does the waiter do in such situation? The waiter will not immediately go for the second table, instead, he will make a mental note to go to that second table after he is done with the first one. Assume another 3rd guest calls him, he will add that to his mental note too. Once he is done with a certain activity, he goes through the list of things he noted in his mind. And he does them on <strong>First Comes First Serve</strong> basis. So, this is what we called an <strong>Event Loop</strong>. 😄</p><p>Okay, now let’s come to JavaScript,</p><p>JavaScript is a Single Threaded language which simply means there is only one thread that does all the work. Yeah, I know there are some exceptions like worker threads. However, the application execution is just one thread. right?</p><p><strong>Call Stack in JavaScript</strong></p><p>When JavaScript’s single thread picks up an event and that event happens to be a function that calls another function. These nested function calls will block the main thread synchronously. Well, here single thread has no other choice but to follow a stack of function calls until the first function has completed its execution. This kind of nested function calls usually managed using the <strong>Stack</strong> data structure. JavaScript uses something called <strong>Call Stack</strong> to manage these nested function calls. Whenever a function finished its execution, it will be popped out from the call stack.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*MuObxXJwlJZwPdm37gOrow.png" /><figcaption>Nested function and Call Stack</figcaption></figure><p>In JavaScript, we have something called <strong>Event Queue</strong> which is similar to the waiter’s mental note 😄</p><p>So, when JavaScript picks up multiple events it will take the first event and execute it. Other events will be added to the <strong>Event Queue </strong>on a<strong> First In First Out </strong>basis.<strong> </strong>Once the execution of the first event and its nested functions which reside in the call stack are finished, JavaScript will take the next event from the event queue and start execution. This will continue until the Event Queue is empty. This is what we called <strong>JavaScript Event Loop</strong>. It’s that simple, right? 😉</p><p>Look at this pseudo code 👇</p><pre>while isNotEmpty(eventQueue)<br>      pull out first item from the event queue<br>      follow the execution logic until call stack is empty</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/911/1*7Ea0vaz4HUKacUC7SRMQ2w.png" /><figcaption>Call Stack and Event Queue</figcaption></figure><p>Now let’s see how Event Queue works in asynchronous JavaScript</p><p>When an asynchronous event happens there will be often a <strong>callback function </strong>associated with it. When that async operation completed, the callback will not immediately be executed. Instead, it will push to the end of the Event Queue.</p><p>Look at this pseudo-code and the diagram 👇</p><pre>print (&#39;I will be the First&#39;)<br>fetchFromAPI(URL, callback =&gt; print(&#39;I will be the Third&#39;))<br>print (&#39;I will be the Second&#39;)</pre><p>Call back will be pushed to the Event Queue 👇</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/741/1*Iibo0vB4Vu30ngX-fZh7Sw.png" /><figcaption>Call back pushed to Event Queue</figcaption></figure><p>Cool !!! right? 😃 In this article, we have discussed the concept of the JavaScript Event Loop. So, I hope you’ve learned something, and Thanks for reading. CHEERS!!!</p><p>Happy Coding!!!</p><figure><a href="https://www.buymeacoffee.com/shamoda"><img alt="" src="https://cdn-images-1.medium.com/max/411/1*oH9GvFemA4J2dDcHJPpz3g.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e3839f5abcff" width="1" height="1" alt=""><hr><p><a href="https://medium.com/concept-viewpoint/javascript-event-loop-explained-e3839f5abcff">JavaScript Event Loop: Explained</a> was originally published in <a href="https://medium.com/concept-viewpoint">Conceptual Viewpoint</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Asynchronous JavaScript: Callbacks, Promises and Async/Await]]></title>
            <link>https://medium.com/concept-viewpoint/asynchronous-javascript-callbacks-promises-and-async-await-9425b1d248b4?source=rss----63c49cde3232---4</link>
            <guid isPermaLink="false">https://medium.com/p/9425b1d248b4</guid>
            <category><![CDATA[asynchronous-javascript]]></category>
            <category><![CDATA[asyncawait]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[callback-function]]></category>
            <dc:creator><![CDATA[Shamoda Jayasekara]]></dc:creator>
            <pubDate>Sun, 07 Mar 2021 14:24:03 GMT</pubDate>
            <atom:updated>2023-11-17T03:23:53.337Z</atom:updated>
            <content:encoded><![CDATA[<h4>Understanding Asynchronous JavaScript</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o65nxKTLsgmJ58RyVijMrQ.png" /></figure><p>Let’s first understand the difference between synchronous and asynchronous. Synchronous operations of JavaScript execute one at a time. This means no matter how long the current operation takes, subsequent processes won’t start until the previous one is completed. Asynchronous operations run at the same time as other synchronous operations and then completes at some point in the future. The best example of an asynchronous operation is an API call. It may take few seconds for the data to be returned from the server. But our application will not freeze while it’s waiting for the data of the API call, it will continue the execution of other operations while waiting.</p><p>Operations in JavaScript are normally synchronous because JavaScript is a single-threaded language. It executes one operation at a time from top to bottom. But using callbacks, promises and async/await we can perform asynchronous JavaScript operations without blocking the main thread.</p><p>Well, let’s dive in!!! 😉</p><h4>Callbacks</h4><p>When a function accepts another function as an argument, this argument is known as a callback function. When the first function is executed, it will execute the second function. The callback function may be a named or an anonymous function. Here is an example,👇</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42233081f16a533dbd520ba8c67afad7/href">https://medium.com/media/42233081f16a533dbd520ba8c67afad7/href</a></iframe><p><strong>Output</strong>: Hello John!</p><p>Multiple functions can be created independently and used as callbacks. But these will create a multi-level function and when you have too many of these nested functions the code becomes harder to read. It literally, impossible 😵This is known as <strong>Callback Hell</strong>. Look!!! 👇😬</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10e87d9b5f26869492125b983cd48298/href">https://medium.com/media/10e87d9b5f26869492125b983cd48298/href</a></iframe><p>So, you can see how confusing it is to pass each function as a callback. Callback functions are useful for short asynchronous operations. But when we are working with large sets of functions callback functions are not considered to be the best practice. Because of that, promises are introduced to simplify the same thing.</p><h4>Promises</h4><p>A promise represents the eventual completion or failure of an asynchronous operation and its resulting value. It is like a placeholder for a value that we are waiting on.</p><p>So, a promise is exactly what it sounds like. 😄 It is a promise to do something if some conditions are satisfied otherwise it won’t. With promises, we can differ the execution of a code block until an asynchronous operation is completed. This way other operations can keep executing without any interruption.</p><p>A promise can have 3 states,</p><p>· Pending<br>· Fulfilled<br>· Rejected</p><p>When the promise is pending, JavaScript continues code execution. This way the promise or the value that we are waiting on does not cause our program to freeze. Eventually, the promise state will change into either fulfilled or rejected. Then JavaScript goes back to that code block to execute the appropriate operations.</p><p>Let’s take a simple example.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa3bc8fe8bf69bbfcf071e36bf9f068e/href">https://medium.com/media/fa3bc8fe8bf69bbfcf071e36bf9f068e/href</a></iframe><p>We created a variable and assigned it to a new Promise. Promise accepts a function with two parameters (callbacks) called <strong>resolve </strong>and <strong>reject</strong>. Then, within our new promise, we can execute our asynchronous function. It may be an API call, downloading a file, or any other operation which will take some time.</p><p>After that, we can determine we should resolve or reject the promise depending on the success or failure of our asynchronous operation.</p><p>Well, we have created our promise and now let’s execute it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/69ef8857de9fd8375adc58acbc15a2fd/href">https://medium.com/media/69ef8857de9fd8375adc58acbc15a2fd/href</a></iframe><p>Here, we called <strong>myPromise </strong>to initiate it and we used <strong>.then()</strong> method to look for the promise to enter the <strong>fulfilled </strong>state. It accepts a function that receives the resolved value from the resolve method. Inside <strong>.then()</strong> method, we can do any operation with the results of our asynchronous function or the promise. 😊</p><p>After that, we can use <strong>.catch()</strong> to look for the promise to enter the <strong>rejected </strong>state. It also accepts a function that receives any errors from our reject method.</p><p><strong>Promise.all() Vs Promise.race()</strong></p><p>Well, Lot of time we have multiple promises that we need to run at the same time and then do something once they have all completed. In this case, we can use <strong>Promise.all()</strong>. It accepts an array of promises and it will return their results in an array once all of them completed their execution.</p><p>For some reason, you may have multiple promises, and all you care about the first one that finishes, the rest of them you want to discard. In that case, we can use <strong>Promise.race()</strong>. This is only going to return the results of the first promise which finishes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/afdefb01b806d66c75c461132c11eabc/href">https://medium.com/media/afdefb01b806d66c75c461132c11eabc/href</a></iframe><h4>Async/Await</h4><p>The async/await is basically just syntactic sugar wrapped around promises which makes them easier to work with. First, let’s take an example.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38296c75a25a7ca6bd8ba559345a6795/href">https://medium.com/media/38296c75a25a7ca6bd8ba559345a6795/href</a></iframe><p>We’ve used <strong>fetch </strong>API to retrieve a set of todos over the network and it will return us a promise object. Here, we used <strong>.then()</strong> to wait on the data to be returned. This is fine for simple requests, but when we are dealing with multiple promises, it can get a little bit confusing to determine when each line will get executed.</p><p>With <strong>async/await</strong> we can write JavaScript code that looks synchronous but it’s actually asynchronous. 😉 Now, let’s see how we can rewrite this fetch statement with async and await. The first thing that we need to do is wrap our asynchronous code in a function and add the <strong>async </strong>keyword before the function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eec684a825067a3bb7db440b7102ee3d/href">https://medium.com/media/eec684a825067a3bb7db440b7102ee3d/href</a></iframe><p>Above, within the function, we have created a <strong>response </strong>constant and used the <strong>await </strong>keyword before our promise. In this case, it’s our fetch statement. 😄 On the next line, we did the same thing. We are waiting for our <strong>response.json()</strong> to execute before executing the last line. Finally, on the last line, we are logging the <strong>JSON </strong>data. It will wait until the previous lines are complete with their execution.</p><p>So, you can see this is very logical and the way this is written is easy to understand. But remember, you cannot use the await keyword without the async keyword attached to its wrapper function. These keywords will only work together. The function can be a standard function, arrow function, or even an anonymous function that calls itself.</p><p>Well, understanding how promises and async/await work together can make your code cleaner and more logical. Hope you got something from this article. Thanks for reading. Cheers for now!!!</p><p>Happy Coding!!!</p><figure><a href="https://www.buymeacoffee.com/shamoda"><img alt="" src="https://cdn-images-1.medium.com/max/411/1*oH9GvFemA4J2dDcHJPpz3g.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9425b1d248b4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/concept-viewpoint/asynchronous-javascript-callbacks-promises-and-async-await-9425b1d248b4">Asynchronous JavaScript: Callbacks, Promises and Async/Await</a> was originally published in <a href="https://medium.com/concept-viewpoint">Conceptual Viewpoint</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The SOLID design principles: Explained]]></title>
            <link>https://medium.com/concept-viewpoint/the-solid-design-principles-explained-f0835fbbfc04?source=rss----63c49cde3232---4</link>
            <guid isPermaLink="false">https://medium.com/p/f0835fbbfc04</guid>
            <category><![CDATA[object-oriented-design]]></category>
            <category><![CDATA[best-practices]]></category>
            <category><![CDATA[solid]]></category>
            <category><![CDATA[oop]]></category>
            <category><![CDATA[design-principles]]></category>
            <dc:creator><![CDATA[Shamoda Jayasekara]]></dc:creator>
            <pubDate>Fri, 26 Feb 2021 17:44:07 GMT</pubDate>
            <atom:updated>2023-11-17T03:23:09.505Z</atom:updated>
            <content:encoded><![CDATA[<h4>What are SOLID principles and Why?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CqKCA2nwVz-lNga0sKbXYg.png" /></figure><p>If you are familiar with <a href="https://medium.com/concept-viewpoint/oop-concepts-for-beginners-simplified-fdb0efe13e0d">Object-Oriented Programming</a>, you have probably heard about the SOLID principles. Following these five principles in Software Development, makes it easier to maintain and scale when the project grows. So, don’t dare to violate them. 😉 They were introduced by a popular American Software Engineer called Robert C. Martin (also known as Uncle Bob).</p><p>SOLID stands for,</p><p>· <strong>S </strong>—&gt; Single Responsibility Principle</p><p>· <strong>O </strong>—&gt; Open-Closed Principle</p><p>· <strong>L </strong>—&gt; Liskov Substitution Principle</p><p>· <strong>I </strong>—&gt; Interface Segregation Principle</p><p>· <strong>D </strong>—&gt; Dependency Inversion Principle</p><p>Now, Let’s take them one by one,</p><h4>Single Responsibility Principle</h4><p>It is all about having one and only one reason to change. When we are talking about a class it should have one responsibility.</p><p>Let’s take an example.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a3c546ec2273534cffa654a91a967d7/href">https://medium.com/media/6a3c546ec2273534cffa654a91a967d7/href</a></iframe><p>Well, what do you think about this Person class which has two responsibilities? Is it reusable? Is it easy to maintain? NOOO!!! 😑<br>But why?? 🙄 Let me show you.</p><p><strong>What happens if a class has multiple responsibilities? </strong><br>Well, it will increase the possibility of bugs and errors because when you make some changes to one responsibility it may affect the other responsibilities as well.</p><p>Instead of the above approach, we can create 2 separate classes like this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b9ab72ec890a2ae1c777953a05a8b09/href">https://medium.com/media/0b9ab72ec890a2ae1c777953a05a8b09/href</a></iframe><p>The main goal of this principle is to separate responsibilities from each other. It will increase the code reusability and most importantly it will increase the maintainability of the code because changing one behavior will not affect the other unrelated behaviors.</p><h4>Open-Closed Principle</h4><p>It says software entities like classes should be open for extension but closed for modification. What does that mean? 🙄 Look at the below code segment.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/268ad3f5ef47181b6dae33f1dc18108f/href">https://medium.com/media/268ad3f5ef47181b6dae33f1dc18108f/href</a></iframe><p>Consider a scenario where you want to add more Animals to the above program like Lion, Cow, Sheep, etc. You have to keep constantly modify the program and add more if-else blocks to it. It would violate the open-closed principle.</p><p>So, how to overcome this problem? I’ll show you one approach, 😉</p><p>We can create an abstract class called Animal and declare <strong>speak()</strong> method inside of it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/09435e54facf20b331c174a65a4c821b/href">https://medium.com/media/09435e54facf20b331c174a65a4c821b/href</a></iframe><p>Then we can create separate classes for each animal and extend each class to the Animal class. It will enable us to do the implementation of <strong>speak()</strong> method separately. Look! 👇</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5744b75ed41d2b8d16a9b56ff6ba98ca/href">https://medium.com/media/5744b75ed41d2b8d16a9b56ff6ba98ca/href</a></iframe><p>See?? Now, if you want to add another animal to the program, just create a class extending to Animal class and do the necessary method implementations inside of it. It’s that simple! 😅<br> This satisfies the Open-Closed principle. Remember! Open for extension but Closed for modifications. 🤗</p><h4>Liskov Substitution Principle</h4><p>Well, sometimes you may extend some classes using inheritance even when it is not the best approach but to reuse some code. Liskov Substitution method says sub-classes must be substitutable for their base-class. Let’s take an example,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab02e4f655cf80b1ac2ea441fa3ed355/href">https://medium.com/media/ab02e4f655cf80b1ac2ea441fa3ed355/href</a></iframe><p>Above, we defined a simple Bird class with a couple of methods that all birds can fulfill. Now let’s extend our abstract class to the Eagle class and provide some code lines to its methods.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b1e284053ed8fe098c1a1443d3725ce/href">https://medium.com/media/3b1e284053ed8fe098c1a1443d3725ce/href</a></iframe><p>As our code describes, birds have a Beak and Wings. Let’s take Ostrich as our next example. But wait! 😳 Ostriches can’t really fly??? Exactly, they can’t fly. But it has all the other behaviors of a Bird. So, can we extend the <strong>Ostrich </strong>class to the <strong>Bird </strong>class? <strong>No!!!</strong> because when a child class cannot perform the same actions as its parent class, it can cause bugs and errors. In this case, we have to do separate implementations for our Ostrich class.</p><p>We should always be careful when we use inheritance. We should use inheritance only when our base-class is replaceable by a sub-class in all instances. Don’t use inheritance just to reuse few lines of code. 😬</p><h4>Interface Segregation Principle</h4><p>It says to keep our Interfaces as small as possible. The moment we have large interfaces with a lot of methods, then every time we change the interface all the implementing classes also should be changed.</p><p>Let’s take the previous example again. Initially, the Bird interface has <strong>makeSound()</strong> method only.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f49a21608d8039ef7d3966cd59596c95/href">https://medium.com/media/f49a21608d8039ef7d3966cd59596c95/href</a></iframe><p>Let’s implement this interface to Eagle and Ostrich classes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d0c782e0fcd68d4d42374452f6218a8b/href">https://medium.com/media/d0c782e0fcd68d4d42374452f6218a8b/href</a></iframe><p>Okay, up to now everything is fine. what if we want to add the <strong>fly()</strong> method into our Bird interface. If we do that, we have to do its implementation in both Eagle and Ostrich classes. But Ostriches cannot fly. 😐</p><p>Therefore, we have to make a dummy implementation of the <strong>fly()</strong> method in the Ostrich class just to make our compiler happy. 😄 So, how do we overcome this problem? It’s simple let me show you.</p><p>Let’s create another interface called Fly and define the <strong>fly()</strong> method inside of it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c4569c1fbdfdbdbafa05cc19148a16cf/href">https://medium.com/media/c4569c1fbdfdbdbafa05cc19148a16cf/href</a></iframe><p>Now, all you need to do is implement Bird and Fly interfaces to the Eagle class. It will not be affected to Ostrich class. Therefore, the Ostrich class will remain the same.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab5d5a4ded85101520d96851c85b1d32/href">https://medium.com/media/ab5d5a4ded85101520d96851c85b1d32/href</a></iframe><p>Cool, isn’t it? The aim of this principle is to split a set of behaviors into smaller sets so that a Class executes only the set of behaviors it requires.</p><h4>Dependency Inversion Principle</h4><p>First, let’s learn about <strong>coupling</strong>. Wait! What? 🙄 <strong>Coupling</strong>??? Yeah, coupling means how much one class knows about another class. Let’s take class <strong>A </strong>and class <strong>B </strong>for our example. If class A knows class B through its interface only, for example, class A interacts with class B through its API <strong>interface </strong>then class A and class B said to be <strong>loosely coupled</strong>.</p><p>On the other hand, assume classes A and B interact with each other using interfaces as well as <strong>non-interface</strong> stuff, then they are said to be <strong>tightly coupled</strong>. Suppose you change the class B’s <strong>non-interface</strong> part, as previous example <strong>non-API </strong>stuff, then in the case of loose coupling class A does not breakdown, but tight coupling causes class A to breakdown.</p><p><strong>Okay, now let’s discuss the Dependency Inversion Principle.</strong></p><p>This principle refers to the decoupling of software modules or classes. It says higher-level classes should not be dependent on lower-level classes. Both should depend on abstraction not on concrete classes. In simple terms, always try to depend upon interfaces(abstractions) rather than concrete classes.</p><p>You may probably think about what it means by a higher-level class? Well, the classes that implement the business logic are called higher-level classes. 😉</p><p>Here is a simple real-world example of database connectivity.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/21ce672abe634b1f2c1123b25232d772/href">https://medium.com/media/21ce672abe634b1f2c1123b25232d772/href</a></iframe><p>What do you think about this code? In this case, SQLconnection class will be the lower-level class and InsertData class will be the higher-level class. Yeah, it will work. But declaring SQLconnection with a new keyword inside InsertData class, we have tightly coupled these two classes. Now, you can see this code snippet ☝️ violates the Dependency Inversion Principle.</p><p>Later, if we wanted to change the database engine, we would have to modify the InsertData class, and this would violate the Open-Closed Principle as well.</p><p><strong>So, how to get rid of this tight coupling?</strong></p><p>Since both higher-level and lower-level classes should depend on abstraction. Let&#39;s create an <strong>interface </strong>called ConnectionInterface.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/22695a7200c1d89a9a3f12f9918b39e0/href">https://medium.com/media/22695a7200c1d89a9a3f12f9918b39e0/href</a></iframe><p>Now we can implement this interface to our SQLconnection class then inject the ConnectionInterface into InsertData class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05e52520a0349d1d001cd2072681cd00/href">https://medium.com/media/05e52520a0349d1d001cd2072681cd00/href</a></iframe><p>Excellent! Now our classes are not depending on each other. They are depending only on the interface that we have introduced.</p><p>Well, in this article we have taken a deep dive into the SOLID principles. I know SOLID is a little bit confusing topic, but I believe this article gave you a solid understanding of SOLID principles. 😂 Keep Learning, Keep Growing! 💪</p><p>Cheers for now!!!</p><figure><a href="https://www.buymeacoffee.com/shamoda"><img alt="" src="https://cdn-images-1.medium.com/max/411/1*oH9GvFemA4J2dDcHJPpz3g.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f0835fbbfc04" width="1" height="1" alt=""><hr><p><a href="https://medium.com/concept-viewpoint/the-solid-design-principles-explained-f0835fbbfc04">The SOLID design principles: Explained</a> was originally published in <a href="https://medium.com/concept-viewpoint">Conceptual Viewpoint</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[OOP Concepts for Beginners: Simplified]]></title>
            <link>https://medium.com/concept-viewpoint/oop-concepts-for-beginners-simplified-fdb0efe13e0d?source=rss----63c49cde3232---4</link>
            <guid isPermaLink="false">https://medium.com/p/fdb0efe13e0d</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[oop]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[object-oriented]]></category>
            <dc:creator><![CDATA[Shamoda Jayasekara]]></dc:creator>
            <pubDate>Thu, 25 Feb 2021 06:10:01 GMT</pubDate>
            <atom:updated>2023-11-17T03:20:40.374Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>What is Object-Oriented Programming and Why?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c5FGkED40YKvwQ5b_zheYw.png" /></figure><p>Well, we all know that there are lots of programming approaches out there. Like Procedural Programming, Functional Programming, Object-oriented Programming, and the list goes on. So, which programming approach you should take to solve a problem? Well, the simple answer is “It depends on your requirement” 👻</p><p><strong>But why Object Orient Programming (OOP) is so popular?</strong> 🙄<br>In OOP you will find that it is a new way of solving problems. When you are writing a program, you may probably divide large problems into smaller sub-problems. And solve them using separate units of code. The basic idea behind OOP is instead of writing a huge program, you can create a class which is a template containing a set of attributes and methods. Inside a method, you can implement the logic to solve a sub-problem. Then you create objects using that template or the class. Objects are instances of that class, and you can interact with them in a fun and exciting way. So, here you can create any number of classes depending on your requirement. Eventually, you’ll be able to come up with a solution for your complex problem in a more convenient way.</p><h3>So, what are the 4 pilers of OOP? Well, Let’s get into it.</h3><p>Wait ✋, before that, I want to introduce you to some basic terminologies we often use.</p><p><strong>Attribute:</strong><br>It is a variable owned by a class and it will determine the characteristics of that class.</p><p><strong>Method:</strong><br>Methods are functions in which we can implement our logic to solve a specific problem. Methods will determine the behavior of the instances of the class.</p><p><strong>Class:</strong> <br>It is a blueprint or template which contains its own attributes and methods.</p><p><strong>Object:</strong><br>Simply, it is an instance of the class. An object has its own attributes and methods which reside inside the blueprint or the class. Objects can be uniquely identified by their name.</p><h4>Well, here we go, these are the basic OOP concepts you should know,</h4><p>· Abstraction</p><p>· Encapsulation</p><p>· Inheritance</p><p>· Polymorphism</p><p>Okay, let’s take them one by one.</p><h4>Abstraction</h4><p>It is a process that provides only the functionality to the user while hiding complex implementations from the user. Simply, the user will have information on what the object does instead of how it does. Actually, users do not want to know the complex logic implemented in the methods of the class. The user is only interested in the functionality of the class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vNp3QOHQnbnsfGkuX1PjyA.jpeg" /></figure><p>Well, I’ll give you a simple example to understand this. Let’s take a TV remote controller as our example. We have so many buttons on that remote to control our Television. When we press the ON button, Television will be turned on. But we do not know how it happens inside the remote circuitry. Actually, we don’t need to know it, we just wanted to turn ON the TV, that’s it!</p><p>Well, we can apply the same thing in OOP. We have access to the public methods of an object, and we can use them to perform some operations. Here, we don’t need to know about the complex logical implementations behind them. To implement abstraction, we can use interfaces as well as abstract classes.</p><h4>Encapsulation</h4><p>Grouping related data and code together into a single unit is called Encapsulation. For example, let’s take a medicine capsule, which is wrapped up with different medicines. Simply, encapsulation will act as a security barrier that prevents accidental access to code and data by other codes defined outside the class. We can use access modifiers like private, public, and protected to secure class attributes (data) and methods (code).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/528/1*-S7fK3S9HdFsGTHLsxT7LA.png" /></figure><h4>Inheritance</h4><p>Well, inheritance is one of the basic concepts in OOP. Here, one class (child) can acquire characteristics (attributes) and behaviors (methods) of another class (parent). It will create a Parent-Child relationship between classes. One of the main advantages of inheritance is, it provides code reusability. This simply means, when parent classes have some specific characteristics and behaviors, we don’t need to rewrite or reimplement them in its child classes. Because when we use inheritance relationship those characteristics and behaviors will inherit by the child.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/491/1*HTC-sMbOaQhkPSVVnVA2TA.jpeg" /></figure><h4>Polymorphism</h4><p>Polymorphism means the ability to have multiple forms. Just look at the below example. <strong>Speak() </strong>method is implemented differently in each sub-class of Animal class. When you call the <strong>Speak()</strong> method in Dog class, it will say “Woof”, when you call the same method in Cat class, it will say “Meow” likewise Cow will say “Moo”. Look! Same method but different implementations, this is what we called Polymorphism.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/474/1*Cl-U004gfdrglSdHR7-hxQ.png" /></figure><p><strong>There are 2 types of Polymorphism.</strong></p><p>1. Static Polymorphism: Method overloading</p><p>Method overloading performed within the same class. When we are overloading the same method inside the same class, its return type can be the same or different from each other. But remember! parameters must be different.</p><p>2. Dynamic Polymorphism: Method overriding</p><p>Method overring performed inside child classes of a parent class, which means there should be an inheritance relationship between classes. Method overring can be used to provide a specific implementation of a method that is already provided by its parent class. Remember! method signature must be the same inside each and every class.</p><p>So, I hope now you have some basic idea about what OOP concepts really are? Isn’t that so? Let me know if you have any doubts and thoughts in the comment section down below. Cheers for now!!!</p><p>Happy Coding!!!</p><figure><a href="https://www.buymeacoffee.com/shamoda"><img alt="" src="https://cdn-images-1.medium.com/max/411/1*oH9GvFemA4J2dDcHJPpz3g.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fdb0efe13e0d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/concept-viewpoint/oop-concepts-for-beginners-simplified-fdb0efe13e0d">OOP Concepts for Beginners: Simplified</a> was originally published in <a href="https://medium.com/concept-viewpoint">Conceptual Viewpoint</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>