Dev To Lionnelt Solid Principles in Dartflutter 2g21 Text TH
Dev To Lionnelt Solid Principles in Dartflutter 2g21 Text TH
The SOLID principles are a set of principles for software development aimed at
making software more maintainable, scalable, and flexible. In this article, we will
discuss how to apply the SOLID principles in Flutter with code examples.
The Single Responsibility Principle (SRP) states that a class should have only one
reason to change. In other words, a class should have only one responsibility. A class
that has multiple responsibilities is hard to maintain and modify.
class User {
int id;
String name;
void save() {
// save user to database
}
void sendEmail() {
// send email to user
}
}
This class violates the SRP because it has two responsibilities: saving the user to the
database and sending an email to the user. A better approach would be to split the
class into two separate classes:
class User {
int id;
String name;
}
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
class UserRepository {
void save(User user) {
// save user to database
}
}
class EmailService {
void sendEmail(User user) {
// send email to user
}
}
The Open/Closed Principle (OCP) states that a class should be open for extension
but closed for modification. In other words, you should be able to extend a class's
behavior without modifying its source code.
class Rectangle {
double width;
double height;
double area() {
return width * height;
}
}
If we want to create a new class for calculating the area of a circle, we would have to
modify the Rectangle class which is currently violating the OCP. A better approach
would be to create an interface for calculating the area and have each shape
implement the interface:
double area() {
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
return width * height;
}
}
double area() {
return pi * pow(radius, 2);
}
}
The Liskov Substitution Principle (LSP) states that objects of a superclass should be
able to be replaced with objects of a subclass without affecting the correctness of
the program. In other words, a subclass should be able to replace its superclass
without breaking the code.
class Rectangle {
double width;
double height;
double area() {
return width * height;
}
}
@override
double set width(double value) => side = value;
@override
double set height(double value) => side = value;
}
This class violates the LSP because a Square cannot be used in place of a
Rectangle without affecting the correctness of the program. A better approach
would be to create a separate class for Square :
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
abstract class Shape {
double area();
}
double area() {
return width * height;
}
}
double area() {
return pow(side, 2);
}
}
double area() {
return width * height;
}
double perimeter() {
return 2 * (width + height);
}
}
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
class Circle implements Shape {
double radius;
double area() {
return pi * pow(radius, 2);
}
double perimeter() {
return 2 * pi * radius;
}
}
This class violates the ISP because a Circle does not have a perimeter. A better
approach would be to create separate interfaces for area and perimeter:
double area() {
return width * height;
}
double perimeter() {
return 2 * (width + height);
}
}
double area() {
return pi * pow(radius, 2);
}
}
class UserRepository {
void save(User user) {
// save user to database
}
}
class UserService {
UserRepository userRepository;
UserService(this.userRepository);
This class violates the DIP because UserService depends on the concrete
implementation of UserRepository . A better approach would be to depend on an
abstraction:
class UserService {
UserRepository userRepository;
UserService(this.userRepository);
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
By depending on an abstraction, UserService is no longer tied to a specific
implementation of UserRepository , making it more flexible and maintainable.
Conclusion
The SOLID principles are important for building maintainable, scalable, and flexible
software. By following these principles in your Flutter code, you can create code that
is easier to maintain and modify. Remember, the SOLID principles are not rules, but
rather guidelines that can help you write better code.
Sentry PROMOTED
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
📊 A side-by-side product comparison between Sentry
and Crashlytics
A free guide pointing out the differences between Sentry and Crashlytics, that’s it.
See which is best for your mobile crash reporting needs.
See Comparison
Read next
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Receiving voice input with Amazon Connect
Matt Lewis - Feb 5
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com