Shamim Shams
Shamim Shams
Published on

উদাহরণসহ সহজ বাংলায় সলিড(SOLID) প্রিন্সিপালস(PART-2)

Authors

৩.) Liskov Substitution Principle (LSP)

এই প্রিন্সিপলটি  Barbara Liskov এর নামে করা হয় যিনি ১৯৮৭ এ behavioral subtyping নামে একটি ধারণাকে পরিচিত করেন। এই প্রিন্সিপল অনুযায়ী প্রত্যেক সাবক্লাস এমন হবে যে সাবক্লাস অবজেক্ট দিয়ে এর প্যারেন্ট ক্লাস অবজেক্ট  কে যেন প্রতিস্থাপন করা যায় যাবে করে সফটওয়্যার এর ফাঙ্কশনালিটি নষ্ট না হয়।

কোড LSP ছাড়া :

<?php
class Rectangle
{
    protected $width = 0;

    protected $height = 0;

    public function setWidth(int $width): void
    {
        $this->width = $width;
    }

    public function setHeight(int $height): void
    {
        $this->height = $height;
    }

    public function getArea(): int
    {
        return $this->width * $this->height;
    }
}

class Square extends Rectangle
{
    public function setWidth(int $width): void
    {
        $this->width = $this->height = $width;
    }

    public function setHeight(int $height): void
    {
        $this->width = $this->height = $height;
    }
}

function printArea(Rectangle $rectangle): void
{
    $rectangle->setWidth(4);
    $rectangle->setHeight(5);

    // BAD: Will return 25 for Square. Should be 20.
    echo sprintf('%s has area %d.', get_class($rectangle), $rectangle->getArea()) . PHP_EOL;
}

$rectangles = [new Rectangle(), new Square()];

foreach ($rectangles as $rectangle) {
    printArea($rectangle);
}

কোড LSP সহ :

<?php 
interface Shape
{
    public function getArea(): int;
}

class Rectangle implements Shape
{
    private $width = 0;
    private $height = 0;

    public function __construct(int $width, int $height)
    {
        $this->width = $width;
        $this->height = $height;
    }

    public function getArea(): int
    {
        return $this->width * $this->height;
    }
}

class Square implements Shape
{
    private $length = 0;

    public function __construct(int $length)
    {
        $this->length = $length;
    }

    public function getArea(): int
    {
        return $this->length ** 2;
    }
}

function printArea(Shape $shape): void
{
    echo sprintf('%s has area %d.', get_class($shape), $shape->getArea()).PHP_EOL;
}

$shapes = [new Rectangle(4, 5), new Square(5)];

foreach ($shapes as $shape) {
    printArea($shape);
}

৪.) Interface Segregation Principle (ISP)

এই প্রিন্সিপল এর মূল কথা হলো বড়ো সাইজের ইন্টারফেস ছোট ছোট ইন্টারফেস এ ভাগ করা উচিত , যাতে করে যে ক্লাস এ যতুটুকু দরকার ঠিক ততটুকুই বাস্তবায়ন করা যায়।

কোড ISP ছাড়া :

<?php
interface Employee
{
    public function work(): void;

    public function eat(): void;
}

class HumanEmployee implements Employee
{
    public function work(): void
    {
        // ....working
    }

    public function eat(): void
    {
        // ...... eating in lunch break
    }
}

class RobotEmployee implements Employee
{
    public function work(): void
    {
        //.... working much more
    }

    public function eat(): void
    {
        //.... robot can't eat, but it must implement this method
    }
}

কোড ISP সহ :

<?php
interface Workable
{
    public function work(): void;
}

interface Feedable
{
    public function eat(): void;
}

interface Employee extends Feedable, Workable
{
}

class HumanEmployee implements Employee
{
    public function work(): void
    {
        // ....working
    }

    public function eat(): void
    {
        //.... eating in lunch break
    }
}

// robot can only work
class RobotEmployee implements Workable
{
    public function work(): void
    {
        // ....working
    }
}

৫.) Dependency Inversion Principle (DIP)

এই প্রিন্সিপল এর মূল কথা হলো সফটওয়্যার এর বিভিন্ন মডিউল কে ডিকাপল করা।  এই ভাবে উপরের দিকের মডিউল সমূহ নিচের দিকের মডিউল সমূহের উপরে নির্ভর করার পরিবর্তে , উভয়ে নির্ভর করবে এবস্ট্রাক্শন এর উপরে।

কোড DIP সহ :

<?php
class Employee
{
    public function work(): void
    {
        // ....working
    }
}

class Robot extends Employee
{
    public function work(): void
    {
        //.... working much more
    }
}

class Manager
{
    private $employee;

    public function __construct(Employee $employee)
    {
        $this->employee = $employee;
    }

    public function manage(): void
    {
        $this->employee->work();
    }
}

কোড DIP সহ :

<?php
interface Employee
{
    public function work(): void;
}

class Human implements Employee
{
    public function work(): void
    {
        // ....working
    }
}

class Robot implements Employee
{
    public function work(): void
    {
        //.... working much more
    }
}

class Manager
{
    private $employee;

    public function __construct(Employee $employee)
    {
        $this->employee = $employee;
    }

    public function manage(): void
    {
        $this->employee->work();
    }
}