<?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 Var Sha on Medium]]></title>
        <description><![CDATA[Stories by Var Sha on Medium]]></description>
        <link>https://medium.com/@var.sha?source=rss-271ff916bd83------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Var Sha on Medium</title>
            <link>https://medium.com/@var.sha?source=rss-271ff916bd83------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 01:31:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@var.sha/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[Open/Closed Principle - The hardest SOLID Principle]]></title>
            <link>https://medium.com/@var.sha/open-closed-principle-the-hardest-solid-principle-d07b5fe6c234?source=rss-271ff916bd83------2</link>
            <guid isPermaLink="false">https://medium.com/p/d07b5fe6c234</guid>
            <category><![CDATA[open-closed-principle]]></category>
            <category><![CDATA[solid-principles]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[software-design]]></category>
            <dc:creator><![CDATA[Var Sha]]></dc:creator>
            <pubDate>Fri, 15 Nov 2024 11:35:58 GMT</pubDate>
            <atom:updated>2024-11-15T11:46:43.609Z</atom:updated>
            <content:encoded><![CDATA[<p>The Open/Closed Principle in SOLID principles has always been one of the hardest concepts for me to wrap my head around. The principle states, “Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.” Despite watching countless YouTube videos, I struggled to grasp its true meaning.</p><h3>The Dilemma</h3><p>Initially, it felt counterintuitive. If we need to craft code such that no more changes can be made, doesn’t that contradict the tenets of regular refactoring? It seemed very anti-agile, as the main motto in software development is: “change is constant.” How can we adhere to the Open/Closed Principle while embracing constant change?</p><h3>Dependency Injection and Single Responsibility</h3><p>This is where dependency injection and the Single Responsibility Principle come into play. By segregating the code into classes that each change for a single reason and injecting dependencies, we can keep components loosely coupled. But what happens when requirements change? Even with dependency injection, changes are inevitable.</p><h3>A Real-World Example</h3><p>Consider a scenario where I am sending notifications to a list of people. Tomorrow, I need to add a priority to the list of recipients. In this case, the notification sending function isn’t truly closed for modification. I could extend the NotificationSender class and create a new PrioritisedNotificationSender class. However, does that mean the NotificationSender functionality is now deprecated?</p><h3>Insight from the Originators</h3><p>In my quest for clarity, I turned to the stalwarts who originated this concept. Following is a quote from wikipedia:</p><p>Bertrand Meyer, credited with the term Open/Closed Principle in his 1988 book “Object-Oriented Software Construction,” stated:</p><ul><li>A module will be said to be open if it is still available for extension. For example, it should be possible to add fields to the data structures it contains, or new elements to the set of functions it performs.</li><li>A module will be said to be closed if it is available for use by other modules. This assumes that the module has been given a well-defined, stable description (the interface in the sense of information hiding).</li></ul><p>In the 1990s, the principle evolved to emphasize the use of abstracted interfaces, allowing implementations to be changed and multiple implementations to be polymorphically substituted for each other.</p><h3>The Core Intent</h3><p>The original intent was to protect clients from unexpected changes and ensure the abstraction remains intact. It also aimed to reduce the cost of business change requirements.</p><h3>Strategic Closure</h3><p>Robert C. Martin’s paper, which further elaborates on this principle, states: “It should be clear that no significant program can be 100% closed. For example, consider what would happen to the DrawAllShapes function if we decided that all Circles should be drawn before any Squares. The DrawAllShapes function is not closed against a change like this. In general, no matter how ‘closed’ a module is, there will always be some kind of change against which it is not closed. Since closure cannot be complete, it must be strategic. That is, the designer must choose the kinds of changes against which to close his design.”</p><h3>A Design Ideal</h3><p>This insight was enlightening. The Open/Closed Principle should be strategic, meaning the developer must use their experience to judge which classes should be closed and which ones can’t be closed. It should be seen as a design ideal rather than an absolute rule. It’s great if we can achieve it, but we must acknowledge that not everything can be completely closed.</p><h3>Conclusion</h3><p>The Open/Closed Principle is a vital part of designing robust software, but it requires a strategic approach. By understanding its intent and applying it thoughtfully, we can strike a balance between maintaining flexibility and ensuring stability in our codebases. Embracing this principle as an ideal rather than a rigid rule allows us to adapt to change while protecting the core functionalities of our software.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d07b5fe6c234" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Need for decorator design pattern]]></title>
            <link>https://medium.com/@var.sha/need-for-decorator-design-pattern-1365d27df3a4?source=rss-271ff916bd83------2</link>
            <guid isPermaLink="false">https://medium.com/p/1365d27df3a4</guid>
            <category><![CDATA[decorator-pattern]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[design]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[object-oriented]]></category>
            <dc:creator><![CDATA[Var Sha]]></dc:creator>
            <pubDate>Wed, 13 Nov 2024 10:54:34 GMT</pubDate>
            <atom:updated>2024-11-13T10:54:34.437Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Exploring the Decorator Pattern with a Coffee Shop Example</strong></h3><p>Recently, I was refreshing my knowledge on design patterns using “Head First Design Patterns” book. Among the patterns covered, a refresher on the Decorator Pattern piqued my interest, and I delved into a simplified example to understand its application and alternatives.</p><h4>The Scenario: A Coffee Shop</h4><p>Imagine we have a coffee shop that sells beverages like Decaf and Espresso. Customers can customize their drinks with various condiments such as steamed milk or whipped cream, in any order and quantity. Here’s a simplified version of the example from the book:</p><pre>public interface Beverage {<br>    int cost();<br>}<br>public interface Condiment extends Beverage {<br>    int cost();<br>}<br>public class Decaf implements Beverage {<br>    public int cost() {<br>        return 2;<br>    }<br>}<br>public class Espresso implements Beverage {<br>    public int cost() {<br>        return 1;<br>    }<br>}<br>public class SteamedMilk implements Condiment {<br>    private Beverage beverage;<br><br>    public SteamedMilk(Beverage beverage) {<br>        this.beverage = beverage;<br>    }<br>    public int cost() {<br>        return 2 + beverage.cost();<br>    }<br>}<br><br>public class WhippedCream implements Condiment {<br>    private Beverage beverage;<br>    <br>    public WhippedCream(Beverage beverage) {<br>        this.beverage = beverage;<br>    }<br>    public int cost() {<br>        return 2 + beverage.cost();<br>    }<br>}<br><br>public class MainClass {<br>    public static void main(String[] args) {<br>        Beverage espresso = new Espresso();<br>        Beverage espressoWithWhippedCream = new WhippedCream(espresso);<br>        Beverage espressoWithWhippedCreamAndSteamedMilk = new SteamedMilk(espressoWithWhippedCream); <br>    } <br>}</pre><p>In this example, we can see how beverages can be decorated with condiments, adding to their cost dynamically.</p><h3>The Question: Why is decorator needed when you can design differently and still follow SOLID principles?</h3><p>The question I had in mind was why we couldn’t design the classes to follow SOLID principles without using the decorator pattern. For instance, why not design the Decaf class as follows by passing a list of condiments:</p><pre>public class Decaf implements Beverage {<br>    private static final int DECAF_COST = 2;<br>    private final List&lt;Condiment&gt; condimentList;<br>    public Decaf(List&lt;Condiment&gt; condimentList) {<br>        this.condimentList = condimentList;<br>    }<br>    public int cost() {<br>        return DECAF_COST + condimentList.stream()<br>                .mapToInt(Condiment::cost)<br>                .reduce(Integer::sum)<br>                .orElseThrow();<br>    }<br>}</pre><p>This approach indeed follows SOLID principles, making it possible to pass a list of condiments to a beverage. So, where does the Decorator Pattern fit in?</p><h3>The Answer: When to Use the Decorator Pattern</h3><p>The key reason to use the Decorator Pattern is when the Beverage classes (like Decaf and Espresso) are already defined, either as part of a library or as well-tested classes that cannot be modified. This adherence to the Open-Closed Principle is crucial, as it allows new functionalities to be added to existing classes without altering their structure.</p><p>If you are designing the classes from scratch, passing a list of condiments might work perfectly well without using the Decorator Pattern. However, if you are dealing with existing classes that should remain unchanged, the Decorator Pattern offers a flexible and powerful way to extend their functionalities.</p><h3>Conclusion</h3><p>Understanding the context and constraints of your project is vital when choosing the right design pattern. The Decorator Pattern is a fantastic solution for extending the functionalities of existing, immutable classes. However, for new designs, a more straightforward approach that adheres to SOLID principles can be equally effective.</p><p>By exploring both approaches, we can appreciate the strengths of each and apply them judiciously in our development projects.</p><p>Happy coding! ☕💻</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1365d27df3a4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Cross-Site Request Forgery (CSRF)]]></title>
            <link>https://medium.com/@var.sha/understanding-cross-site-request-forgery-csrf-9cec0fc0dc00?source=rss-271ff916bd83------2</link>
            <guid isPermaLink="false">https://medium.com/p/9cec0fc0dc00</guid>
            <category><![CDATA[csrf]]></category>
            <category><![CDATA[web-security]]></category>
            <dc:creator><![CDATA[Var Sha]]></dc:creator>
            <pubDate>Wed, 13 Nov 2024 08:40:01 GMT</pubDate>
            <atom:updated>2024-11-15T11:28:52.871Z</atom:updated>
            <content:encoded><![CDATA[<p>In today’s digital world, security is paramount, especially when it comes to online transactions and personal data. One of the significant threats in the realm of web security is Cross-Site Request Forgery (CSRF). Let’s dive into how CSRF works and the mechanisms to prevent it.</p><h3>The Scenario</h3><p>Imagine Bob, a regular internet user, is logged into his banking account on <strong>mybank.com</strong>. Bob decides to visit another website, <strong>malicious-website.com</strong>, which has guessed the URL for transferring money on <strong>mybank.com</strong>.</p><h4>The Attack</h4><ol><li><strong>Bob on MyBank</strong>: Bob is logged into <strong>mybank.com</strong>. Following is the POST request to make money transfer.</li></ol><pre>POST: mybank.com/transfer?amount=10000&amp;to=abc</pre><p>2. <strong>Bob Visits Malicious Site</strong>: Bob also visits <strong>malicious-website.com</strong>, which has the malicious intent to steal Bob’s money.</p><p><strong>3. Embedded Request</strong>: <strong>malicious-website.com</strong> has an embedded request which Bob is lured into clicking. This requires some luck. The malicious guy correctly guesses that Bob is actually logged in to his bank website. Following would be the POST request which Bob clicks from his browser unknowingly:</p><pre>POST: mybank.com/transfer?amount=10000&amp;to=malicious</pre><p><strong>4. Automatic Cookie Sending</strong>: Bob’s browser automatically sends the request to <strong>mybank.com</strong> with session details because cookies are sent automatically to the server in each request.</p><p>As a result, £10,000 is transferred from Bob’s account. This is a classic example of Cross-Site Request Forgery.</p><h3>The Defense Mechanism: CSRF Token</h3><p>To counter CSRF attacks, <strong>mybank.com</strong> implements a smart mechanism: the CSRF token.</p><h4>How it Works</h4><p>When <strong>mybank.com</strong> presents a form for a POST request, it includes a hidden CSRF token field:</p><pre>&lt;input type=&quot;hidden&quot; id=&quot;csrfToken&quot; name=&quot;csrfToken&quot; value=&quot;{{alskegf-sjhefg-skdbhf}}&quot; /&gt;</pre><p>The server expects this token to be present in the POST request:</p><pre>mybank.com/transfer/amount=10000&amp;to=abc&amp;csrf=alskegf-sjhefg-skdbhf</pre><p>This token is unique for every user session, making it difficult for attackers to forge requests as this is a hard to guess string.</p><h3>The Role of Same-Origin Policy</h3><p>Why can&#39;t <strong>malicious-website.com</strong> run scripts to steal this CSRF token from the POST form on <strong>mybank.com</strong>? This is where the Same-Origin Policy (SOP) comes into play.</p><p>The Same-Origin Policy ensures that a script on <strong>malicious-website.com</strong> cannot read from <strong>mybank.com</strong> since they are different domains.</p><h3>The Concept of Same Origin</h3><p>Two URLs are considered to have the same origin if the protocol, port (if specified), and host are the same. In this case, since the domains are different, the browser prevents <strong>malicious-website.com</strong> from accessing <strong>mybank.com</strong>.</p><h3>Cross-Origin Resource Sharing (CORS)</h3><p>CORS acts like a screen that prevents peeking between different domains unless explicit permission is given. If <strong>mybank.com</strong> were to allow <strong>malicious-website.com</strong> to read its data using a header called Access-Control-Allow-Origin with a value of *, then yes, the CSRF token could potentially be stolen.</p><h3>Smart Implementation of CSRF and Session Management</h3><p>To further enhance security, <strong>mybank.com</strong> needs a smart implementation of CSRF and session management. The logic should ensure that the session ID and CSRF token match to validate the authenticity of the request.</p><h3>Conclusion</h3><p>Cross-Site Request Forgery is a significant threat in web security. However, with the implementation of CSRF tokens and adherence to the Same-Origin Policy, websites can protect users from such attacks effectively. Smart session management and robust security practices are crucial in maintaining the integrity and security of user data.</p><p>Stay safe online! 🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9cec0fc0dc00" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>