"深入解析 PHP 面向对象编程设计模式中的 SOLID 原则和实践"
作者:RiverSoul
时间:2024-02-25
来源:互联网
浏览:0
SOLID原则是面向对象编程设计模式中的一组指导原则,旨在提高软件设计的质量和可维护性。由罗伯特·马丁(RobertC.Martin)提出,SOLID原则包括:单一职责原则(SingleResponsibilityPrinciple,SRP):一个类应该只负责一项任务,并且这个任务应该被封装在类中。这样可以提高类的可维护性和可复用性。classUser{private$id;private$name;private$email;publicfunction__construct($id,$name,$em
SOLID 原则是面向对象编程设计模式中的一组指导原则,旨在提高软件设计的质量和可维护性。由罗伯特·马丁(Robert C. Martin)提出,SOLID 原则包括:
- 单一职责原则(Single Responsibility Principle,SRP):一个类应该只负责一项任务,并且这个任务应该被封装在类中。这样可以提高类的可维护性和可复用性。
class User {
private $id;
private $name;
private $email;
public function __construct($id, $name, $email) {
$this->id = $id;
$this->name = $name;
$this->email = $email;
}
public function getId() {
return $this->id;
}
public function getName() {
return $this->name;
}
public function getEmail() {
return $this->email;
}
}
class UserRepository {
private $connection;
public function __construct($connection) {
$this->connection = $connection;
}
public function save(User $user) {
$stmt = $this->connection->prepare("INSERT INTO users (name, email) VALUES (?, ?)");
$stmt->execute([$user->getName(), $user->getEmail()]);
}
public function find($id) {
$stmt = $this->connection->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$id]);
$result = $stmt->fetch();
if ($result) {
return new User($result["id"], $result["name"], $result["email"]);
}
return null;
}
}
- 开放-封闭原则(Open-Closed Principle,OCP):软件实体(类、模块等)应该对扩展开放,对修改关闭。这样可以提高软件的灵活性,降低软件维护成本。
interface Shape {
public function getArea();
}
class Rectangle implements Shape {
private $width;
private $height;
public function __construct($width, $height) {
$this->width = $width;
$this->height = $height;
}
public function getArea() {
return $this->width * $this->height;
}
}
class Circle implements Shape {
private $radius;
public function __construct($radius) {
$this->radius = $radius;
}
public function getArea() {
return pi() * $this->radius ** 2;
}
}
class ShapeCalculator {
public function calculateTotalArea($shapes) {
$totalArea = 0;
foreach ($shapes as $shape) {
$totalArea += $shape->getArea();
}
return $totalArea;
}
}
- 里氏替换原则(Liskov Substitution Principle,LSP):子类可以替换其父类而不会影响程序的正确性。这样可以提高软件的灵活性,使软件更容易重构。
class Animal {
public function eat() {
echo "Animal is eating.";
}
}
class Dog extends Animal {
public function bark() {
echo "Dog is barking.";
}
}
class Cat extends Animal {
public function meow() {
echo "Cat is meowing.";
}
}
function feedAnimal(Animal $animal) {
$animal->eat();
}
feedAnimal(new Dog()); // prints "Dog is eating."
feedAnimal(new Cat()); // prints "Cat is eating."
- 接口隔离原则(Interface Segregation Principle,ISP):应该使用多个专门的接口,而不是一个通用的接口。这样可以提高软件的可读性,降低软件维护成本。
interface Printable {
public function print();
}
interface Scannable {
public function scan();
}
interface Faxable {
public function fax();
}
class Printer implements Printable {
public function print() {
echo "Printer is printing.";
}
}
class Scanner implements Scannable {
public function scan() {
echo "Scanner is scanning.";
}
}
class FaxMachine implements Faxable {
public function fax() {
echo "Fax machine is faxing.";
}
}
class MultifunctionDevice implements Printable, Scannable, Faxable {
public function print() {
echo "Multifunction device is printing.";
}
public function scan() {
作者最新文章
微软推出Project Zenith:面向Windows 11开发者的AI硬件加速方案
2026-09-08 18:15
打破流量垄断,让平台经济释放普惠红利
2026-09-08 18:07
Arm AGI CPU详解:136核Neoverse V3,3nm双芯粒架构与AI数据中心部署
2026-09-08 17:18
Windows安装Docker教程:启用WSL2并运行第一个容器验证
2026-09-04 09:26
PDF转Word操作指南:在线与本地转换方法及格式检查
2026-09-03 16:03
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















