面向對象的設計原則
1)“開-閉”原則(Open-Closed Principle, 或者OCP)
Software entities should be open for extension, but closed for modification
2)裡氏代換原則(Liskov Substitution Principle, 或者LSP)
如果對每一個類型為T1的對象o1, 都有類型為T2的對象o2, 使得以T1定義的所有程式P在所有的對象o1都代換成o2時, 程式P的行為沒有變化,那麼類型T2是類型T1的子類型
3)依賴倒轉原則(Dependency Inversion Principle, 或者DIP)
Abstraction should not depend upon details. Details should depend upon abstractions
4)接口隔離原則(Interface Segregation Principle, 或者ISP)
使用多個專門的接口比使用單一的總接口要好
5)合成/聚合複用原則(Composition/Aggregation Reuse Principle, 或者CARP)
要盡量使用合成/聚合, 盡量不要使用繼承
6)迪米特法則(Law of Demeter, 或者LoD)
Least Knowledge Principle, LKP
Only talk to your immediate friends
Don't talk to strangers
設計模式
一、建立模式
1)簡單工廠模式(Simple Factory)
2)工廠方法模式(Factory Method)
3)抽象工廠模式(Abstract Factory)
4)單例模式(Singleton)
5)多例模式(Multiton)
6)建造模式(Builder)
7)原型模式(Prototype)
二、結構模式
1)擴充卡模式(Adapter)
2)預設擴充卡模式(Default Adapter)
3)合成模式(Composite)
4)裝飾模式(Decorator)
5)代理模式(Proxy)
6)享元模式(Flyweight)
7)門面模式(Facade)
8)橋梁模式(Bridge)
三、行為模式
1)不變模式(Immutable)
2)政策模式(Strategy)
3)模闆方法模式(Template Method)
4)觀察者模式(Observer)
5)疊代子模式(Iterator)
6)責任鍊模式(Chain of Responsibility)
7)指令模式(Command)
8)備忘錄模式(Memento)
9)狀态模式(State)
10)通路者模式(Visitor)
11)解釋器模式(Interpreter)
12)調停者模式(Mediator)