solid 是計(jì)算機(jī)編程中五個(gè)良好原則(規(guī)則)的縮寫。 solid 允許程序員編寫更易于理解和稍后更改的代碼。 solid 通常與使用面向?qū)ο笤O(shè)計(jì)的系統(tǒng)一起使用。
讓我們使用車輛示例來(lái)解釋 solid 原理。想象一下,我們正在設(shè)計(jì)一個(gè)系統(tǒng)來(lái)管理不同類型的車輛,例如汽車和電動(dòng)汽車,以提供運(yùn)輸服務(wù)。
s?-?單一職責(zé)原則(srp)
車輛示例:想象你有一輛汽車。它負(fù)責(zé)駕駛,但不應(yīng)該負(fù)責(zé)處理自己的維護(hù)(例如換油或輪胎旋轉(zhuǎn))。相反,由一名單獨(dú)的機(jī)械師負(fù)責(zé)。
說(shuō)明:在我們的代碼中,vehicle 類應(yīng)該只處理與車輛本身相關(guān)的事情,比如存儲(chǔ)其品牌和型號(hào)。如果我們需要管理維護(hù),我們會(huì)為此創(chuàng)建一個(gè)單獨(dú)的維護(hù)類。這樣,每個(gè)類都有一個(gè)工作或職責(zé),使代碼更易于管理。
class vehicle
def initialize(make, model)
@make = make
@model = model
end
end
class maintenance
def initialize(vehicle)
@vehicle = vehicle
end
def perform_maintenance
puts "performing maintenance on #{@vehicle.make} #{@vehicle.model}"
end
end
登錄后復(fù)制
o?-?開/閉原理(ocp)
車輛示例:假設(shè)您有一輛基本型汽車,現(xiàn)在您想在系統(tǒng)中添加一輛電動(dòng)汽車。您不必修改現(xiàn)有的汽車類別即可添加電動(dòng)汽車的功能。相反,您可以通過(guò)創(chuàng)建新的電動(dòng)汽車類來(lái)擴(kuò)展現(xiàn)有功能。
說(shuō)明:vehicle類是開放擴(kuò)展的(你可以創(chuàng)建新類型的車輛,如electricvehicle),但它是封閉修改的(你不需要更改vehicle類本身來(lái)添加新類型)。
class vehicle
def initialize(make, model)
@make = make
@model = model
end
def description
"#{@make} #{@model}"
end
end
class electricvehicle
<hr><h2>
l?-?里氏替換原理 (<a style="color:#f60; text-decoration:underline;" href="https://www.php.cn/zt/79544.html" target="_blank">lsp</a>)
</h2>
<p><strong>車輛示例</strong>:假設(shè)您有一支車隊(duì),并且您可以毫無(wú)問(wèn)題地用電動(dòng)汽車替換任何普通汽車。兩者都應(yīng)該能夠在不破壞系統(tǒng)的情況下執(zhí)行其基本功能?-?駕駛?-?。<br><strong>說(shuō)明</strong>:任何子類(如 electricvehicle)都應(yīng)該能夠替換其父類(vehicle),而不改變程序的行為。這確保我們的代碼可以以相同的方式處理不同類型的車輛。<br></p>
<pre class="brush:php;toolbar:false">class vehicle
def initialize(make, model)
@make = make
@model = model
end
def drive
puts "driving the #{@make} #{@model}"
end
end
class electricvehicle
<hr><h2>
i?-?接口隔離原則(isp)
</h2>
<p><strong>車輛示例</strong>:想象一下您有不同類型的車輛:有些可以充電(如電動(dòng)汽車),有些只能駕駛(如汽油車)。你不希望汽油車必須處理充電相關(guān)的方法。<br><strong>說(shuō)明</strong>:類應(yīng)該只實(shí)現(xiàn)它們需要的接口(或行為)。例如,電動(dòng)車輛可能同時(shí)實(shí)現(xiàn)可駕駛和可充電接口,而普通車輛僅實(shí)現(xiàn)可駕駛。<br></p>
<pre class="brush:php;toolbar:false">module drivable
def drive
raise notimplementederror, "this #{self.class} cannot drive"
end
end
module chargeable
def charge
raise notimplementederror, "this #{self.class} cannot be charged"
end
end
class vehicle
include drivable
def initialize(make, model)
@make = make
@model = model
end
def drive
puts "driving the #{@make} #{@model}"
end
end
class electricvehicle
<hr><h2>
d?-?依賴倒置原理(dip)
</h2>
<p><strong>車輛示例</strong>:想象一輛汽車可以有不同類型的發(fā)動(dòng)機(jī):燃?xì)獍l(fā)動(dòng)機(jī)或電動(dòng)發(fā)動(dòng)機(jī)。汽車不應(yīng)該直接依賴于特定的引擎類型,而應(yīng)該依賴于更通用的引擎接口,這樣它就可以使用任何類型的引擎。<br><strong>說(shuō)明</strong>:高級(jí)模塊(如車輛)不應(yīng)依賴于低級(jí)模塊(如 gasengine 或 electricengine)。兩者都應(yīng)該依賴于抽象(如引擎接口)。這使得系統(tǒng)更加靈活并且更容易更改。<br></p>
<pre class="brush:php;toolbar:false">class Engine
def start
raise NotImplementedError, "This #{self.class} cannot start"
end
end
class GasEngine
<p>通過(guò)遵循此車輛示例中的 solid 原則,我們可以構(gòu)建一個(gè)易于維護(hù)、擴(kuò)展和適應(yīng)新需求的系統(tǒng)。</p>
<p>領(lǐng)英:https://www.linkedin.com/in/anandsoni11/</p>
登錄后復(fù)制






