Elvis
Posted on August 3, 2022
Software development principles are a must-know for all software engineers that aim to achieve clean codes. Software evolves with time as new features are required, so it is necessary to build a maintainable, reliable and sustainable code.
SOLID principles are five basic principles for object-oriented programming design. They are set of rules to follow when designing a class structured software. These principles puts maintainability and extensibility into consideration as software evolves.
SOLID STANDS FOR :
S — SINGLE-RESPONSILITY PRINCIPLE
O — OPEN-CLOSED PRINCIPLE
L — LISKOV SUBSTITUTION PRINCIPLE
I — INTERFACE SEGREGATION PRINCIPLE
D — DEPENCY INVERSION PRINCIPLE
The SOLID principles were first introduced by the famous Computer Scientist Robert J. Martin (a.k.a Uncle Bob) in his 2000 paper.
Uncle Bob also authors best selling books Clean Code and Architectures, and he is one of the participant of the Agile Alliance.
The concepts of clean concept of clean coding, object-oriented architecture and design patterns are somehow related and complementary.
The purpose of these concepts is “To create understandable, readable and testable code that many developers can collaboratively work on”.
We all explain the five principles one after the other with examples. In this series, we shall explain the first principle :
SINGLE-RESPONSIBILITY PRINCIPLE (SRP)
It states that a class should have one and only one reason to change, meaning a class should have only job.
This means a class should do only one thing. For example, if a data container like Vehicle class or Book class containing fields regarding the entity should change, it should change only because of the data model.
Consider the example below:
Let’s create a app that takes a collection of shapes, calculates and sums up the areas.
Let create two classes, Square and Circle, with their required parameters
class Square
{
public $length;
function __construct($length)
{
$this->length = $length;
}
}
class Circle
{
public $radius;
function __construct($radius)
{
$this->radius = $radius;
}
}
Let’s create another class to calculate the area
class AreaSumCalculator {
protected $shapes;
public function __construct($shapes = [])
{
$this->shapes = $shapes;
}
public function sum()
{
foreach ($this->shapes as $shape) {
if (is_a($shape, 'Square')) {
$area[] = pow($shape->length, 2);
} elseif (is_a($shape, 'Circle')) {
$area[] = pi() * pow($shape->radius, 2);
}
}
return array_sum($area);
}
public function jsonOutput()
{
$data = [
‘sum’ => $this->areasumcalculator->sum(),
];
return json_encode($data);
}
}
// create objects (instance of classes)
$circle = new Circle(10);
$square = new Square(5);
// create a shape variable to accept an array of shapes (object created)
$shapes = [$circle,$square];
$areas = new AreaCalculator($shapes);
echo $areas->jsonOutput($areas);
The issue with this code is that the AreaCalculator handles, the business logic with the output or presentation logic.
If we need to make any change or add a feature like outputting our answer in another form, we will have to alter the AreaCalculator class.
To solve this, you need to create a class to handle the output or presentation and another to handle the summation.
class AreaSumCalculator{
protected $shapes;
public function __construct($shapes = [])
{
print_r($this->shapes);
$this->shapes = $shapes;
}
public function sum()
{
$area = [];
foreach($this->shapes as $shape)
{
if(is_a($shape, ‘Square’))
{
$area[] = $shape->length * $shape->length;
}elseif(is_a($shape, ‘Circle’))
{
$area[] = pi() * ($shape->radius * $shape->radius);
}
// return array_sum($area);
}
return array_sum($area);
}
}
class AreaSumCalculatorOutput{
public $areasumcalculator;
public function __construct(AreaSumCalculator $areasumcalculator)
{
$this->areasumcalculator = $areasumcalculator;
}
public function jsonOutput()
{
$data = [
‘sum’ => $this->areasumcalculator->sum(),
];
return json_encode($data);
}
}
// create objects (instance of classes)
$circle = new Circle(10);
$square = new Square(5);
// create a shape variable to accept an array of shapes (object created)
$shapes = [$circle,$square];
// calculate the sum of the areas of the shapes
$area_sum_calculator = new AreaSumCalculator($shapes);
// output the sum
$area_sum_output = new AreaSumCalculatorOutput($area_sum_calculator);
// call the output
$json_output = $area_sum_output->jsonOutput();
//echo $json_output;
With this, you have applied the single-responsibility principle of software design. Thanks for following this first part of the series. You can send me a mail at igieborelvis@gmail.com or drop comments.
Posted on August 3, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.