close
close

first Drop

Com TW NOw News 2024

SOLID – DEV community
news

SOLID – DEV community

  1. Single Responsibility Principle (SRP) – Nguyên tắc Trách nhiệm Đơn lẻ
    If you want to use a user, you can send an email message.

php
Copy code
`class User {
public function getUserInfo() {
// Lấy string tin người dùng
}

public function sendEmailNotification() {
    // Gửi email thông báo
}
Go to full screen mode

Exit full screen

}`
What’s going on:
If the user uses the SRP card, you can no longer use it: you can use your email.

What is this:
You can send an email to more information.

php
Copy code
`class User {
public function getUserInfo() {
// Lấy string tin người dùng
}
}

class EmailNotification {
public function sendEmail() {
// Send an email to [email protected]
}
}`

  1. Open/Closed Principle (OCP) – Nguyên tắc Mở/Đóng
    class Shape {
    public function getArea($shape) {
    if ($shape instanceof Circle) {
    return pi() * $shape->radius * $shape->radius;
    } elseif ($shape instanceof Rectangle) {
    return $shape->width * $shape->height;
    }
    }
    }

What’s going on:
If you want to use the getArea, you can use the OCP.

What is this:
Sử dụng tính đa hình (polymorphism) để mở rộng chức năng mà không cần đổi mã nguồn cũ.

`abstract class Shape {
abstract public function getArea();
}

class Circle extends Shape {
public $radius;
public function getArea() {
return pi() * $this->radius * $this->radius;
}
}

class Rectangle extends Shape {
public $width;
public $height;
public function getArea() {
return $this->width * $this->height;
}
}`

  1. Liskov Substitution Principle (LSP) – The Liskov Substitution Principle (LSP) is an example of a bird and a penguin.

php
Copy code
`class Bird {
public function fly() {
// Logic is a chim bay
}
}

class Penguin extends Bird {
public function fly() {
throw new Exception(“Penguins can’t fly”);
}
}`

abstract class Bird {
// Các phương thức chung cho tất cả các loài chim
}

abstract class FlyingBird extends Bird {
abstract public function fly();
}

class Sparrow expands FlyingBird {
public function fly() {
// Logic is one of the best
}
}

class Penguin extends Bird {
// Penguin can fly
}

  1. Interface Segregation Principle (ISP) – You can use the WorkerInterface to make calls and work.

php
Copy code
interface WerkerInterface {
public function work();
maintain public function();
}

What’s going on:
If you want to do a job (), if you want to maintain a job (), if you want to use a triển khai nó.

What is this:
Tách WorkerInterface is no longer needed.

interface Workable {
public function work();
}

interface Maintainable {
maintain public function();
}

class OfficeWorker implements Workable {
public function work() {
// Logic if you want to make a phone call
}
}

class MaintenanceWorker implements Workable, Maintainable {
public function work() {
// Logic if you want to come home
}

public function maintain() {
    // Logic bảo trì của nhân viên bảo trì
}
Go to full screen mode

Exit full screen

}

  1. Dependency Inversion Principle (DIP) – A report you can use to report PDFFormatter.

php
Copy code
class PDFFormatter {
public function format($data) {
// Logic hereafter PDF
}
}

class Report {
private $formatter;

public function __construct() {
    $this->formatter = new PDFFormatter();
}

public function generate($data) {
    return $this->formatter->format($data);
}
Go to full screen mode

Exit full screen

}
What’s going on:
If you want to download a report with PDFFormatter, you can do so with the data from PDFFormatter (for example: HTML).

What is this:
If you want to use the interface, you can use it in the following way.

php
Copy code
interface LayoutInterface {
public function format($data);
}

class PDFFormatter implements FormatterInterface {
public function format($data) {
// Logic hereafter PDF
}
}

class HTMLFormatter implements FormatterInterface {
public function format($data) {
// Logic and HTML
}
}

class Report {
private $formatter;

public function __construct(FormatterInterface $formatter) {
    $this->formatter = $formatter;
}

public function generate($data) {
    return $this->formatter->format($data);
}
Go to full screen mode

Exit full screen

}
If you want to create a report using the FormatterInterface, you can use the DIP.

You can do the following: SOLID:
SRP (Single Responsibility Principle): singularity. It is best to use the right amount of water.
OCP (Open/Closed Principle): Open to expansion, but closed to change. You may want to do something different.
LSP (Liskov substitution principle): like-for-like substitution. Các lớp con có thể thế thớp cơ sở mà không gây lỗi.
ISP (Interface Segregation Principle): Interface should be small. Các giao diện hỏ, cụ thể hơn.
DIP (Dependency Inversion Principle): Dependence on abstractions, not on concretes. You can do the following: it is possible to do this.