5

SOLID Principles Using Dart: Everything You Need to Know

 6 months ago
source link: https://hackernoon.com/solid-principles-using-dart-everything-you-need-to-know
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

@mariusatasiei

Marius Atasiei

💎 Senior Mobile Applications Engineer • Flutter, Dart • iOS, Android • Contractor, Freelancer


Receive Stories from @mariusatasiei

In the world of software development, crafting maintainable, scalable, and adaptable code is crucial. SOLID principles provide a time-tested blueprint for achieving this in object-oriented programming languages like Dart. Let’s explore the principles and how they empower you to build better Flutter applications.

Single Responsibility Principle

👉 One job, one class! The Single Responsibility Principle (SRP) advises that each class in your code should focus on just one task. This makes your code easier to understand, maintain, and extend. Keep things simple, and watch your software grow smoothly!

Example:

Before SRP

➡️ Car handles both driving and tire maintenance.

After applying SRP

➡️ Now, Car focuses on driving, while TireService handles tire maintenance.

Open/Closed Principle

🔓 Keep it open for extension, but closed for modification! The Open/Closed Principle (OCP) states that your code should be easily extendable without needing to modify existing code. This way, you can add new features without risking existing functionality.

Example:

Before OCP

➡️ Vehicle handles gear change for both ManualCar and AutomaticCar.

After OCP

➡️ Now, the Vehicle delegates gear changes to separate Transmission implementations, allowing for easier addition of new transmission types without modifying the Vehicle.

Liskov Substitution Principle

🔄 Subtypes should be swappable! The Liskov Substitution Principle (LSP) states that objects of a derived class should be able to replace objects of the base class without affecting the correctness of your program. This ensures your code is flexible and robust.

Example:

Before LSP

➡️ Penguin inherits from Bird and overrides the fly method, but penguins can’t fly, causing issues when using Penguin as a Bird.

After applying LSP

➡️ Now, we have a more suitable hierarchy: Bird class provides a generic move method, while FlyingBird and Penguin implement their specific movement behavior, making it safe to use them interchangeably.

Interface Segregation Principle

🎯 Keep interfaces focused! The Interface Segregation Principle (ISP) advises that classes should not be forced to implement interfaces they don’t use. Instead, split large interfaces into smaller, more specific ones. This promotes a cleaner and more modular design.

Example:

Before ISP

➡️ Vehicle interface contains both drive and fly methods, forcing Car to implement a method it doesn’t need.

After applying ISP

➡️ Now, we have separate Drivable and Flyable interfaces, allowing Car and Airplane to implement only the methods they need, resulting in cleaner and more modular code.

Dependency Inversion Principle

🔌 Depend on abstractions, not concretions! The Dependency Inversion Principle (DIP) suggests that high-level modules should not rely on low-level modules directly. Instead, both should depend on abstractions. This leads to more flexible and easily maintainable code.

Example:

Before DIP

➡️ Lamp class directly depends on the concrete LightBulb class, making it less flexible.

After applying DIP

➡️ Now, Lamp depends on an abstract Switchable interface, allowing it to work with any Switchable implementation, making the code more flexible and maintainable.


Also published here


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK