設計模式原則

單一職責原則(Single Responsibility Principle)code

1.定義
    There should never be more than one reason for a class to change.
    每一個類都應該實現單一的職責,即有且只有一個緣由引發類的變動。
2.優勢
    類的複雜性下降,可讀性提升,維護性提升。變動引發的風險下降。

里氏替換原則(Liskov Substitution Principle)對象

1.定義1
    If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T. 
    若是對每個類型爲S的對象o1,都有一個類型爲T的對象o2,使得以T定義的全部程序P,在全部的對象o1都轉換爲o2時,程序P的行爲沒有發生變化,那麼類型S就是類型T的子類型。
2.定義2
    functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it. 
    全部引用基類的地方必須能透明的使用其子類的對象。
3.優勢
    增強程序的健壯性,升級時的兼容性。

依賴倒轉原則(Dependence Inversion Principle)繼承

1.定義
    High level modules should not depend upon low level modules,Both should depend upon abstractions.Abstractions should not depend upon details.Details should depend upon abstracts.
    高層次的模塊不該該依賴於低層次的模塊,他們都應該依賴於抽象。抽象不該該依賴於具體實現,具體實現應該依賴於抽象。

接口隔離原則(Interface Segregation Principle)接口

1.定義1
    Clients should not be forced to depend upon interfaces that they don't use. 
    客戶端不該該依賴它不需用的接口。
2.定義2
    The dependency of one class to another one should depend on the smallest possible interface.
    類間的依賴關係應該創建在最小的接口上。

迪米特法則(Low Of Demeter)ip

1.定義1
    Least Knowledge Principle.
    一個對象應該對其餘對象有最少的瞭解。
2.定義2
    Only talk to your immedate friends.
    只和直接的朋友通訊。

合成複用原則(Composite Reuse Principle)ci

1.定義
    Composite/Aggregate Reuse Principle (CARP).
    儘可能首先使用合成/聚合的方式,而不是使用繼承。

開閉原則it

1.定義
     Software entities like classes, modules and functions should be open for extension but closed for modifications. 
     軟件實體應該對擴展開放,對修改關閉。
2.優勢
   提升複用性,提升可維護性。
相關文章
相關標籤/搜索