<?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 Abhishek Jain on Medium]]></title>
        <description><![CDATA[Stories by Abhishek Jain on Medium]]></description>
        <link>https://medium.com/@abhijain2472?source=rss-3ea06581737f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*UTB9a142p8OsZ4Gf0pR7Lw.jpeg</url>
            <title>Stories by Abhishek Jain on Medium</title>
            <link>https://medium.com/@abhijain2472?source=rss-3ea06581737f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 07:18:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@abhijain2472/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[ Mastering SOLID Principles in Dart: Real-World Examples Explained]]></title>
            <link>https://medium.com/@abhijain2472/mastering-solid-principles-in-dart-real-world-examples-explained-1be7dad076fd?source=rss-3ea06581737f------2</link>
            <guid isPermaLink="false">https://medium.com/p/1be7dad076fd</guid>
            <category><![CDATA[solid-principles]]></category>
            <category><![CDATA[flutter-app-development]]></category>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[dart-programming-language]]></category>
            <dc:creator><![CDATA[Abhishek Jain]]></dc:creator>
            <pubDate>Wed, 16 Jul 2025 05:52:05 GMT</pubDate>
            <atom:updated>2025-07-16T05:52:32.050Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yQNtz6czdgE1MhUbffmqKw.png" /></figure><blockquote><em>Want to write cleaner, scalable, and maintainable Dart/Flutter code? The SOLID principles are your blueprint to building better software. In this article, we’ll demystify each principle with practical Dart examples — highlighting common mistakes and how to fix them.</em></blockquote><h3>🔍 What are SOLID Principles?</h3><p><strong>SOLID</strong> is an acronym for the five principles of object-oriented programming that help developers create robust, flexible, and easy-to-maintain code. They are:</p><p><strong>S</strong>: Single Responsibility Principle</p><p><strong>O</strong>: Open/Closed Principle</p><p><strong>L</strong>: Liskov Substitution Principle</p><p><strong>I</strong>: Interface Segregation Principle</p><p><strong>D</strong>: Dependency Inversion Principle</p><p>Let’s understand each with the violated and fixed Dart code examples. 👇</p><h3>1️⃣ Single Responsibility Principle (SRP)</h3><p><strong>Definition:</strong><br>A class should have only one reason to change. That means it should do only one thing.</p><h4>❌ Violation:</h4><pre>class Report {<br>  void generateReport() =&gt; print(&#39;Generating report...&#39;);<br>  void saveToFile() =&gt; print(&#39;Saving report to file...&#39;);<br>}</pre><p>This class is doing two unrelated things: generating and saving the report.</p><h4>✅ Fix:</h4><pre>class ReportGenerator {<br>  void generate() =&gt; print(&#39;Generating report...&#39;);<br>}</pre><pre>class ReportSaver {<br>  void saveToFile() =&gt; print(&#39;Saving report to file...&#39;);<br>}</pre><p>Now, each class handles one responsibility. Cleaner and more maintainable.</p><h3>2️⃣ Open/Closed Principle (OCP)</h3><p><strong>Definition:</strong><br>Software entities should be open for extension but closed for modification. Add new behavior without changing existing code.</p><h4>❌ Violation:</h4><pre>class Shape {<br>  String type;<br>  Shape(this.type);<br>}</pre><pre>class AreaCalculator {<br>  double calculateArea(Shape shape) {<br>    if (shape.type == &#39;circle&#39;) { /*...*/ }<br>    else if (shape.type == &#39;square&#39;) { /*...*/ }<br>  }<br>}</pre><p>Adding new shapes means modifying the AreaCalculator.</p><h4>✅ Fix:</h4><pre>abstract class Shape {<br>  double calculateArea();<br>}</pre><pre>class Circle extends Shape {<br>  double radius;<br>  Circle(this.radius);<br>  double calculateArea() =&gt; 3.14 * radius * radius;<br>}<br>class Square extends Shape {<br>  double side;<br>  Square(this.side);<br>  double calculateArea() =&gt; side * side;<br>}<br>class AreaCalculator {<br>  double calculate(Shape shape) =&gt; shape.calculateArea();<br>}</pre><p>Now, new shapes can be added by extending Shape—No need to modify AreaCalculator.</p><h3>3️⃣ Liskov Substitution Principle (LSP)</h3><p><strong>Definition:</strong><br>Subtypes must be substitutable for their base types without breaking functionality.</p><h4>❌ Violation:</h4><pre>class MediaFile {<br>  void playAudio() {}<br>  void playVideo() {}<br>}</pre><pre>class MP3File extends MediaFile {<br>  void playVideo() =&gt; throw Exception(&quot;Not supported&quot;);<br>}</pre><p>Using MP3File where MediaFile is expected can cause runtime errors.</p><h4>✅ Fix:</h4><pre>abstract class MediaFile {}</pre><pre>abstract class AudioPlayable {<br>  void playAudio();<br>}<br>abstract class VideoPlayable {<br>  void playVideo();<br>}<br>class MP3File extends MediaFile implements AudioPlayable {<br>  void playAudio() =&gt; print(&quot;Playing audio from MP3&quot;);<br>}<br>class MP4File extends MediaFile implements AudioPlayable, VideoPlayable {<br>  void playAudio() =&gt; print(&quot;Playing audio from MP4&quot;);<br>  void playVideo() =&gt; print(&quot;Playing video from MP4&quot;);<br>}</pre><p>Now, only classes with video support implement VideoPlayable. No runtime surprises!</p><h3>4️⃣ Interface Segregation Principle (ISP)</h3><p><strong>Definition:</strong><br>Clients should not be forced to depend on interfaces they don’t use. Split large interfaces into smaller ones.</p><h4>❌ Violation:</h4><pre>abstract class Worker {<br>  void work();<br>  void eat();<br>}</pre><pre>class Robot implements Worker {<br>  void work() =&gt; print(&quot;Robot working&quot;);<br>  void eat() =&gt; throw Exception(&quot;Robot can&#39;t eat&quot;);<br>}</pre><p>The robot doesn’t need to implementeat, but is forced to.</p><h4>✅ Fix:</h4><pre>abstract class Workable {<br>  void work();<br>}</pre><pre>abstract class Eatable {<br>  void eat();<br>}<br>class Human implements Workable, Eatable {<br>  void work() =&gt; print(&quot;Human working&quot;);<br>  void eat() =&gt; print(&quot;Human eating&quot;);<br>}<br>class Robot implements Workable {<br>  void work() =&gt; print(&quot;Robot working&quot;);<br>}</pre><p>Now, classes implement only what they need.</p><h3>5️⃣ Dependency Inversion Principle (DIP)</h3><p><strong>Definition:</strong><br>High-level modules should not depend on low-level modules. Both should depend on abstractions.</p><h4>❌ Violation:</h4><pre>class DebitCard {<br>  void makePayment() =&gt; print(&quot;Paying with Debit Card&quot;);<br>}</pre><pre>class PaymentService {<br>  final DebitCard debitCard;<br>  PaymentService(this.debitCard);<br>  void makePayment() =&gt; debitCard.makePayment();<br>}</pre><p>Tightly coupled to a specific payment method.</p><h4>✅ Fix:</h4><pre>abstract class PaymentMethod {<br>  void makePayment();<br>}</pre><pre>class DebitCard implements PaymentMethod {<br>  void makePayment() =&gt; print(&quot;Paid with Debit Card&quot;);<br>}<br>class GPay implements PaymentMethod {<br>  void makePayment() =&gt; print(&quot;Paid with GPay&quot;);<br>}<br>class CreditCard implements PaymentMethod {<br>  void makePayment() =&gt; print(&quot;Paid with Credit Card&quot;);<br>}<br>class PaymentService {<br>  final PaymentMethod method;<br>  PaymentService(this.method);<br>  void makePayment() =&gt; method.makePayment();<br>}</pre><p>Now PaymentService Works with any payment type via abstraction.</p><h3>🧠 Final Thoughts</h3><p>By applying SOLID principles:</p><ul><li>Your code becomes easier to read and test</li><li>Your architecture remains flexible as requirements grow</li><li>Bugs are easier to isolate and fix</li></ul><p>Whether you’re building a Dart library or a full-fledged Flutter app, keeping SOLID in mind can elevate the quality of your codebase.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1be7dad076fd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>