工具標籤
類別
1. 少用繼承多用組合
正如the Gang of Four 所著的設計模式之前所說, 我們應該盡量優先選擇組合而不是繼承的方式。使用繼承和組合都有許多好處。這個準則的主要意義在於當你本能的使用繼承時,試著思考一下組合是否能更好地對你的需求建模。在一些情況下,是這樣的。 接下來你或許會想,「那我應該在什麼時候使用繼承?」 答案依賴於你的問題,當然下面有一些何時繼承比組合更好的說明:你的繼承表達了“是一個”而不是“有一個”的關係(人類-”動物,用戶-”用戶詳情)你可以復用基類的代碼(人類可以像動物一樣移動)你想透過修改基底類別對所有衍生類別做全域的修改(當動物移動時,修改她們的能量消耗)壞:
class Employee
{
private $name;
private $email;
public function __construct(string $name, string $email)
{
$this->name = $name;
$this->email = $email;
}
// ...
}
// 不好,因为 Employees "有" taxdata
// 而 EmployeeTaxData 不是 Employee 类型的
class EmployeeTaxData extends Employee
{
private $ssn;
private $salary;
public function __construct(string $name, string $email, string $ssn, string $salary)
{
parent::__construct($name, $email);
$this->ssn = $ssn;
$this->salary = $salary;
}
// ...
}好:
class EmployeeTaxData
{
private $ssn;
private $salary;
public function __construct(string $ssn, string $salary)
{
$this->ssn = $ssn;
$this->salary = $salary;
}
// ...
}
class Employee
{
private $name;
private $email;
private $taxData;
public function __construct(string $name, string $email)
{
$this->name = $name;
$this->email = $email;
}
public function setTaxData(string $ssn, string $salary)
{
$this->taxData = new EmployeeTaxData($ssn, $salary);
}
// ...
}2. 避免連貫介面
連貫介面Fluent interface#是一種旨在提高物件導向程式設計時程式碼可讀性的API設計模式,他基於方法鏈Method chaining
mock
diff不好閱讀
壞:
class Car
{
private $make = 'Honda';
private $model = 'Accord';
private $color = 'white';
public function setMake(string $make): self
{
$this->make = $make;
// NOTE: Returning this for chaining
return $this;
}
public function setModel(string $model): self
{
$this->model = $model;
// NOTE: Returning this for chaining
return $this;
}
public function setColor(string $color): self
{
$this->color = $color;
// NOTE: Returning this for chaining
return $this;
}
public function dump(): void
{
var_dump($this->make, $this->model, $this->color);
}
}
$car = (new Car())
->setColor('pink')
->setMake('Ford')
->setModel('F-150')
->dump();#好:
class Car
{
private $make = 'Honda';
private $model = 'Accord';
private $color = 'white';
public function setMake(string $make): void
{
$this->make = $make;
}
public function setModel(string $model): void
{
$this->model = $model;
}
public function setColor(string $color): void
{
$this->color = $color;
}
public function dump(): void
{
var_dump($this->make, $this->model, $this->color);
}
}
$car = new Car();
$car->setColor('pink');
$car->setMake('Ford');
$car->setModel('F-150');
$car->dump();3. 推薦使用final類別
能用時盡量使用 final 關鍵字:
3. 鼓勵單一職責模式.4. 鼓勵開發者用你的公開方法而非透過繼承類別來取得受保護方法的存取權.5. 使得在不破壞使用你的類別的應用程式的情況下修改程式碼成為可能.The only condition is that your class should implement an interface and no other public methods are defined.For more informations you can read the blog post on this topic written by Marco Pivetta (Ocramius).壞:
final class Car
{
private $color;
public function __construct($color)
{
$this->color = $color;
}
/**
* @return string The color of the vehicle
*/
public function getColor()
{
return $this->color;
}
}好: interface Vehicle
{
/**
* @return string The color of the vehicle
*/
public function getColor();
}
final class Car implements Vehicle
{
private $color;
public function __construct($color)
{
$this->color = $color;
}
/**
* {@inheritdoc}
*/
public function getColor()
{
return $this->color;
}
}
相關影片
熱AI工具
Undress AI Tool
免費脫衣圖片
AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。
Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片
Stock Market GPT
人工智慧支援投資研究,做出更明智的決策
熱門文章
如何在Premiere使用AI語音增強? (音訊清理指南)
1 個月前 By 下次还敢
如何將 jQuery 的拖放事件正確遷移到原生 JavaScript
4 週前 By DDD
記事本升級、更便宜的 YouTube TV 以及 Nova Launcher 的新主人:新聞綜述
3 週前 By DDD
'EVM”是什麼?以太坊虛擬機器的意義
1 個月前 By DDD
如何在 PHP 中動態設定嵌套數組的任意深度值
1 個月前 By DDD
熱門話題
# 抖音等級價目表1-75
20518
7
20518
7
# wifi顯示無ip分配
13631
4
13631
4
# 虛擬手機號碼接收驗證碼
11966
4
11966
4
# gmail信箱登陸入口在哪裡
8985
17
8985
17
# windows安全中心怎麼關閉
8505
7
8505
7
熱門工具
記事本++7.3.1
好用且免費的程式碼編輯器
SublimeText3漢化版
中文版,非常好用
禪工作室 13.0.1
強大的PHP整合開發環境
Dreamweaver CS6
視覺化網頁開發工具
SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)












![PHP實戰開發極速入門: PHP快速創建[小型商業論壇]](https://img.php.cn/upload/course/000/000/035/5d27fb58823dc974.jpg)
