SOLID is an Object-Oriented Design Principle that makes software programs easy to read, understand, maintain and extend. Today I want to discuss and learn a little bit about SOLID Principles with you. I remember, In my first interview, I was once asked by a recruiter a question “What do you know about SOLID Principles?” And I failed in this question. I used to know SOLID. But I have never known SOLID. Now find out. Surely there are many people like me. Since this is a common situation, if you’re just self-taught, SOLID doesn’t seem like a big deal to you. But, for businesses. SOLID is again one of the most important principles in software development. So today, I want to cover this topic with some basic definitions and easy examples to help us understand the concept. general concept of SOLID.

Watching: What is Solid

What is S.O.L.I.D

?S.O.L.I.D is the first word of the 5 principles of object-oriented design introduced by Robert C. Martin.

S — Single responsibility principleO — Open closed principleL — Liskov substitution principleI — Interface segregation principleD — Dependency Inversion principle

This is a set of principles that we can keep in mind in terms of programming, tweaking, and structuring the code.

What does SOLID do

?By reading its name, you, like me, cannot understand what it is for. In simple terms, these principles help programmers develop the software is easier and cleaner, easier to maintain and modify as the scale of the software grows larger, more complex. So we can really understand this concept as a programming approach object-oriented by cutting unnecessary parts and organizing the code in a refined and refactorable way.

Some examples of SOLID

Principle #1:Single-responsibility Principle

The Single-responsibility Principle is the single-responsibility principle.

A class should have one and only one reason to change, meaning a class should have only one responsibility.

It is simply a class that should only do a single job, not multiple jobs in the same class. Consider the example below: class Book { protected $Author; public getAuthor($Author) { return $this->Author; } public function formatJson() { return json_encode($this->getAuthor()); }} What do you think? We can clearly see that the class named “Book” is doing more than one job. It is taking the Author of a book and it is also encoding the output into Json format. But the thing is what if we want to have an ouput in another format, not just Json? In that case, maybe we have to write a few more lines to add methods or modify existing code. It might looks like a small job at the moment because this example we have right here is super simple. But imagine, what if you had hundreds or even thousands of classes? Better yet prevent confusion in its infancy by implementing single responsibility principle. So the above example we should change to this: class Book { protected $Author; public getAuthor($Author){ return $this->Author; }}class JsonBookForm { public function format(Book $Author) { return json_encode($Author->getAuthor()); }} This way, if we want to get a different ouput type, we don’t need to change it directly in the Book class.

READ MORE  Software Agent

Principle #2:Open-closed Principle

I know it’s a bit confusing, but this means that classes should be extended to be able to change the functionality, rather than being modified within it. That is to say, when someone wants to add more functionality should extend and add, without having to modify the existing class directly. Check out a popular example of the Open-Close principle: class Triangle{ public $width; public $height;}class Board { public $triangles= <>; public function calculateArea() { $area = 0; foreach ($this->triangles as $triangle) { $area += $triangle->width * $triangle->height* 1/2; } }} We have a Triangle class that holds data for the triangle with width ($width) and height ($height) and a class Board that is used as an array of Triangle objects. This looks perfectly fine but when you think about the shape and the area function. You’ll probably want to reuse this function to calculate different shapes, not just to calculate the area of ​​a triangle. Currently, this code doesn’t allow this because it restricts the Board class to only work. with class Triangle. New approach we can try here is: interface Shape { public function area();}class Triangle implements Shape { public function area() { return $this->width * $this->height *1/2; }}class Circle implements Shape { public function area() { return $this->radius * $this->radius * pi(); }}class Board { public $shapes; public function calculateArea() { $area = 0; foreach ($this->shapes as $shape) { $area+= $shape->area(); } return $area; }}

In this way, we don’t need only define class name in class name Board So whenever we want to calculate other area like rectangle. We can easily add a class named Rectangle and implement the interface shape so that we can use area() in a Board class.

READ MORE  And Why Is It Necessary To Design Beautiful Leaflet

See also: What is Bias – What is Bias in Kpop?

Let q(x) be a provable property about objects x of type B. Then q(y) is provable for objects y of type A. Where A is a type child of B.

Does this look familiar? We may have seen this in school and I’m sure you’ve learned it in math. Where this diagram might be implied in our world is something. That’s related to the previous example..If we talk about shapes, there are a lot of shapes, right? There are circles, triangles and rectangles and squares..Wait a minute…, we all know that square is a special form of rectangle. Means rectangle + some conditions to become square. So it will be like this.

While programming according to this principle is the same, for example, normally we would do this: class Rectangle { public function setW($w) { $this->width = $w; } public function setH($h) { $this->height = $h; } public function Area() { return $this->height * $this->width; }}

class Square extends Rectangle { public function set($w) { $this->width = $w; $this->height = $w; } public function setH=($h) { $this->height = $h; $this->width = $h; }} But do you realize that many lines of code are exactly the same? And in fact we are trying our best to make the program easily maintainable and highly efficient. So that should be avoided. above and try this way: interface Setshape { public function setH($h); public function setW($w); public function Area();}class Rectangle implements Setshape ;class Square implements Setshape ;

This way we don’t have to write the same code over and over again. We simply implement the interface for the class.

Principle #4:Interface segregation principle

A client should never be forced to implement an interface it doesn’t use, or clients shouldn’t be forced to depend on methods they don’t use.

We’re almost there, the fourth rule means that classes shouldn’t be forced to implement interfaces they don’t use. For example, suppose there’s some class that looks like: interface Employee { public function generatereport () public function clockin() public function clockout() public function customerservice() public function getPaid()}And assuming there is a manager that does most of the above but doesn’t need clockin and clockout, so two functions This will never be used.To follow this principle again, we should approach it this way.interface Generalemployee{ public function clockin() public function clockout()}interface Employee{ public function customerservice( ) public function getPaid()}interface management{ public function generatereport()}class Staff implements Generalemployee, Employee{}class Manager implements Employee, management{}

READ MORE  Market Segmentation Market Segmentation

Principle #5:Dependency Inversion principle

Entities must depend on the abstract not on the concrete. It says that high level modules should not depend on low level modules, they should depend on abstractions.

I’ll go straight to the example: class Getuserlists { private $dbConn; public function __construct(MySQLConn, $dbConn) { $this->dbConn= $dbConn; }} It is a common mistake because according to the hierarchy, class Getuserlists is higher module and MySQLConn is lower module. However, you can easily see that this class depends entirely on MySQLConn and it will be confusing, if later we want to use other database than just MySQL.So the solution to this problem will be …interface DbConnectionInterface { public function connect();} class MySqlConn implements DbConnectionInterface { public function connect() {}}class Getuserlists { private $dbConn; public function __construct(DbConnectionInterface $dbConn) { $this->dbConn= $dbConn; }} According to the above code, you can now see that both high level and low level modules depend on abstraction.

Congratulations!! We’ve gone one step further in the world of object-oriented programming with SOLID!!

The SOLID principles may seem difficult to understand at first, and it will still take a lot of practice before we can fully follow the 5 principles of SOLID to apply it to programming.

See also: Download Game Super Hero Tiga, Download Game Superhero Earth Hero

Let’s grasp a language before knowing how to use SOLID in a “Pure Voice of Fire”. If you are not sure about the language,…Join now: JAVA PROGRAMMING COURSE (Full Stack), a course guided by a leading business expert.

It is very good to have a dedicated guide, like-minded classmates to conquer a certain language. When you’re just starting out, don’t study alone Please!