一直想找個好方式跟同事解釋怎麼 decoupling 和 design better code,這篇文章涵蓋了最重要的兩個概念而且用很簡單易懂的 code 解釋了,很不錯。
cohesion: class 裡的 methods 是不是 share 一樣的 context?
coupling: dependency
http://www.rubyguides.com/2018/05/ruby-cohesion-and-coupling/
2018年5月21日 星期一
2017年4月7日 星期五
OOP - interface segregation principle ISP 介面分離原則
用戶不應該被迫相依於他們用不到的函示
如果有用不到的函示,應該做成不同的 interface 或是 protocal
有要用到再裝上去就好
https://danielkjchen.wordpress.com/2016/02/05/%E7%89%A9%E4%BB%B6%E5%B0%8E%E5%90%91%E4%BB%8B%E9%9D%A2%E9%9A%94%E9%9B%A2%E5%8E%9F%E5%89%87-interface-segregation-principle/
http://ithelp.ithome.com.tw/articles/10101106
標籤:
Design Pattern,
OOP
OOP - Law of Demeter 最小知識原則
意思就是假設 A 要問 B 一個問題,但是 B 要問 C 才能知道答案,那麼 A 應該只需要問 B 就好,A 不需要知道 B 還需要問 C,對 A 來說問 B 就能知道答案了
範例
A.askB #=> Answer
違反此原則的範例:
A.askB.askC #=> Answer
http://ithelp.ithome.com.tw/articles/10101265
標籤:
Design Pattern,
OOP
OOP - Dependency-Inversion Principle
重點:program to an interface, not an implementation
原本上層的類別會依賴下層的類別,就如同要蓋二樓就必須蓋好一樓
但 Dependency-Inversion Principle 的意思就是應該要讓上層和下層都依賴於抽象層,也就是上面重點說的 interface
http://teddy-chen-tw.blogspot.com.au/2012/01/5dependency-inversion-principle.html
標籤:
Design Pattern,
OOP
OOP - LSP(Liskov Substitution Principle)Liskov替換原則
Liskov替換原則的定義是:『子類別必須能夠替代基礎類別』
不要繼承不必要的遺產,沒用到而去繼承反而是種累贅甚至會搞壞了整個系統也不一定。
http://ithelp.ithome.com.tw/articles/10100827
標籤:
Design Pattern,
OOP
OOP - SRP(Single Responsibility Principle)單一責任原則
『你只有一個理由需要更改這個class,如果有一個以上的理由就表示:這個class負責超過一個以上的責任。』
http://ithelp.ithome.com.tw/articles/10100557
標籤:
Design Pattern,
OOP
OOP - OCP (Open/Close Principle)
概念:讓程式「可以擴增」但是「不能修改」
http://ithelp.ithome.com.tw/articles/10100008
標籤:
Design Pattern,
OOP
訂閱:
文章 (Atom)