1. 開閉原則(Open Closed Principle,OCP)
軟體物體應當對擴展開放,對修改關閉(Software entities should be open for extension,but closed for modification)
合成復用原則、里氏替換原則相輔相成,都是開閉原則的具體實作規范
擴展新類而不是修改舊類
2. 里氏替換原則(Liskov Substitution Principle,LSP)
繼承必須確保超類所擁有的性質在子類中仍然成立(Inheritance should ensure that any property proved about supertype objects also holds for subtype objects)
繼承父類而不去改變父類
3. 依賴倒置原則(Dependence Inversion Principle,DIP)
高層模塊不應該依賴低層模塊,兩者都應該依賴其抽象;抽象不應該依賴細節,細節應該依賴抽象(High level modules shouldnot depend upon low level modules.Both should depend upon abstractions.Abstractions should not depend upon details. Details should depend upon abstractions)
面向介面編程,而不是面向實作類
4.單一職責原則(Single Responsibility Principle,SRP)
一個類應該有且僅有一個引起它變化的原因,否則類應該被拆分(There should never be more than one reason for a class to change)
每個類只負責自己的事情,而不是變成萬能
5. 介面隔離原則(Interface Segregation Principle,ISP)
一個類對另一個類的依賴應該建立在最小的介面上(The dependency of one class to another one should depend on the smallest possible interface)
各個類建立自己的專用介面,而不是建立萬能介面
6 迪米特法則(Law of Demeter,LoD)
最少知識原則(Least Knowledge Principle,LKP) 只與你的直接朋友交談,不跟“陌生人”說話(Talk only to your immediate friends and not to strangers)
無需直接互動的兩個類,如果需要互動,使用中間者
過度使用迪米特法則會使系統產生大量的中介類,從而增加系統的復雜性,使模塊之間的通信效率降低
7. 合成復用原則(Composite Reuse Principle,CRP)
又叫組合/聚合復用原則(Composition/Aggregate Reuse Principle,CARP) 軟體復用時,要盡量先使用組合或者聚合等關聯關系來實作,其次才考慮使用繼承關系來實作
優先組合,其次繼承
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/287588.html
標籤:其他
