1. 程式人生 > >23種設計模式圖片以及設計模式六大原則

23種設計模式圖片以及設計模式六大原則

設計模式六大原則:

  1. 單一職責原則(Single Responsibility Principle,SRP )
    There should never be more than one reason for a class to change.

  2. 里氏替換原則(Liskov Substitution Principle,LSP )
    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的子型別。)
    Functions that use pointers or references to base classes must beable to use objects of derived classes without knowing it.(所有引用基類的地方必須能透明地使用其子類的 物件。)

  3. 依賴倒置原則(Dependence Inversion Principle,DIP)
    High level modules should not depend upon low level modules.Both should depend upon
    abstractions.Abstractions should not depend upon details.Details
    翻譯過來,包含三層含義:
    ●高層模組不應該依賴低層模組,兩者都應該依賴其抽象;
    ● 抽象不應該依賴細節;
    ● 細節應該依賴抽象。

  4. 介面隔離原則 (Interface Segregation Principle,ISP)
    ● Clients should not be forced to depend upon interfaces that
    they don’t use.(客戶端不應該依 賴它不需要的介面。)
    ● The dependency of one class to
    another one should depend on the smallest possible interface.(類間的依賴關係應該建立在最小的介面上。)

  5. 迪米特法則(Law of Demeter,LoD)也稱為最少知識原則(Least Knowledge Principle,LKP)
    Only talk to your immediate friends(只與直接的朋友通 信。)

  6. 開閉原則 (Open Closed Principle,OCP)
    Software entities like classes,modules and functions should be open for extension but closed for
    modifications.(一個軟體實體如類、模組和函式應該對擴充套件開放,對修改關閉。)