Unraveling SOLID: An introduction to the principles with Dart - Part 2: The Open/Closed Principle

Unraveling SOLID: An introduction to the principles with Dart - Part 2: The Open/Closed Principle

So here we are again! Welcome to part 2 of our series about SOLID. Today, we are going to discuss more about our second principle, the Open/Closed Principle. Now that you already know what the "S" letter in SOLID means, we are going to discover what the "O" letter stands for. Let's begin our journey.

O: Open/Closed Principle (OCP)

When we talk about the Open/Closed Principle, the first thing that we must have in mind is "open for extension and closed for modification". This means that the behavior of a software entity can be extended without modifying its source code.

Let's take a look at some examples to make it clear:

Here we have the GeometricForm class with two parameters and one method calculateArea. In the main function, we instantiate a new GeometricForm into the variable square. Nothing wrong with that, right? Let's take it to a real-world example. Imagine that you are building a new app, and the first version is on the stores. Now, in your app, you have to add another Geometric Form, a Circle, for example. The area of a circle is calculated by the formula A = (r*r) * PI. How can you do that? Let's see one way to do it:

Now, as you can see, we created an enum that has the two possible types in our app, square and circle. In our GeometricForm class, we added two variables, radius and type. Inside our calculateArea method, we now use an if statement to check which type of Geometric Form we want to calculate the area for. We are checking if the type is a circle and if the radius is different from null (Radius can be null though because a square doesn't have a radius).

Note: Take a look at our if condition. We are checking if the radius is different from null and inside the if clause in the return, we are using the null assertion operator (!) or the "bang" operator. This operator is used to tell Dart that the value is not null. We have to pay attention when using this operator or we can crash an entire program. We are only using it in this case because we already checked if the value is different from null, guaranteeing that inside that if, the value is not going to be null. To understand more about that, you can search about Null Safety in Dart.

If the condition is true, we want to calculate the area of a circle; if it's false, we know that it's a square. But look, we modified the entire class, added two variables, changed the calculate area method. This violates the OCP and should not be done. Let's look at the right way of doing it by following the OCP:

Now, we have an abstract class called GeometricForm with an abstract method calculateForm. Now, every class that extends/implements the GeometricForm interface must implement the calculateForm method. We created two classes that extend GeometricForm, the Square class and the Circle class. Each one has everything needed to calculate its own area. Now, we are respecting the OCP, and our classes also have Single Responsibility. The Square class only calculates the area of a square, and the Circle class only calculates the area of a circle. Using the OCP is essential because it promotes software extensibility and maintainability. By allowing software entities to be extended without modification, developers can add new functionality without the risk of breaking existing code. This results in code that is easier to maintain, extend, and reuse. Now, you can try to implement and use the OCP in your daily bases. I hope you understood and liked what we saw today! See you in the next article.

要查看或添加评论,请登录

Matheus Felipe的更多文章

社区洞察

其他会员也浏览了