S.O.L.I.D. Principles in Dart

When you are working with an object oriented language like Dart, there are a couple of rules you need to keep in mind as you are coding

These rules or principles rather, are known as the SOLID principles. Let us look at them

S.O.L.I.D stands for

  • Single responsibility principle

  • Open closed principle

  • Liskov substitution principle

  • Interface segregation principle

  • Dependency inversion principle

Single Responsibility Principle

This principles says that a class should only have one reason to change. In other words, a class should only have one responsibility. This is important because it helps to keep your codebase clean and maintainable.

If you have a class with multiple responsibilities, it is likely that changing one thing will break another. However, if a class only has one responsibility, then changing it should not break anything else.

Open and Closed Principle

This states that a class should be open for extension but closed for modification. This means that you should be able to extend a class to add new functionality without having to modify the existing code.

This is important because it allows you to add new features to your code without breaking existing features.

Liskov Substitution Principle

This states that a subclass should be able to be used in place of a superclass. This is important because it allows you to replace existing code with new code without breaking anything.

Interface Segregation Principle

this states that a class should only implement the interfaces it needs. This is important because it prevents a class from being forced to implement unnecessary interfaces, which can lead to code bloat.

Dependency Inversion Principle

this states that a class should not depend on concrete classes, but should instead depend on abstractions. This is important because it allows you to change the concrete classes that are used without changing the code that uses them.

In summary

  • Single responsibility principle: A class should only have one responsibility.

  • Open closed principle: A class should be open for extension but closed for modification.

  • Liskov substitution principle: A subclass should be able to be used in place of a superclass.

  • Interface segregation principle: A class should only implement the interfaces it needs.

  • Dependency inversion principle: A class should not depend on concrete

classes, but should instead depend on abstractions.

Let us now take a deep dive into the code examples;

Code Example 1

Let us take a look at a simple example of a class that violates the single responsibility principle.

singleresponsibility.png Here we have a class called Employee and this class has two responsibilities.

The first responsibility is to track the employee's name and the second responsibility is to track the employee's salary.

It also has two methods, one to set the name and one to set the salary.

This class violates the single responsibility principle because it has two responsibilities.

Code Example 2

Let us take a look at a simple example of a class that violates the open closed principle.

Here we have a class called Employee and this class has a method called calculatePay.

openclosed.png This method calculates the employee's pay based on their salary and the number of hours they worked.

This class violates the open closed principle because if we want to change how the pay is calculated, we have to modify the calculatePay method.

Code Example 3

Let us take a look at a simple example of a class that violates the Liskov substitution principle.

Here we have a class called Rectangle and this class has two methods, getWidth and getHeight.

liskov.png

We also have a class called Square, which inherits from Rectangle.

liskov2.png

This class violates the Liskov substitution principle because a Square is not a Rectangle.

A Square has a different width and height than a Rectangle.

Code Example 4

Let us take a look at a simple example of a class that violates the interface segregation principle.

Here we have an interface called Shape and this interface has two methods, draw and get

Area.

interfacesegregation.png We also have a class called Rectangle that implements the Shape interface.

interfacesegregation2.png

This class violates the interface segregation principle because the Rectangle class is forced to implement the draw method even though it doesn't need it.

Code Example 5

Let us take a look at a simple example of a class that violates the dependency inversion principle.

Here we have a class called Employee and this class has a method called calculatePay.

This method calculates the employee's pay based on their salary and the number of hours they worked.

dependencyInversion.png This class violates the dependency inversion principle because it depends on the concrete class, Employee.

It should instead depend on an abstraction.

Conclusion

In this article, we have looked at the five principles of SOLID code.

We have also looked at some code examples of each principle.

Keep these principles in mind as you are coding in Dart and you will be well on your way to writing code that is SOLID.