天天看點

總結一下最近看設計模式的收獲

最近看了strategy, observer, factory method, abstract factory, decorator, command, singleton, adaptor, facade這些模式,加上原來聽的一些模式,可以說大概對設計模式有了個概念。總的說來我覺得設計模式的初衷就是提高維護的重用性,盡量使得客戶代碼不需要改變。在我看的這些模式中,除了facade之外,基本上都是一個抽象類持有另一個抽象類(接口)的引用,然後在這個抽象基類中調用這個飲用的方法。由于所持有的都是抽象類(接口)的引用,是以很用以借由多态來達到目的。這些類(接口)之間的耦合關系都很松散,因為他們都不知道對方是怎麼實作的,依賴抽象而不是實作就有這個好處。

  設計模式并不是很複雜,但是掌握确實不易,其實自己做的過程中也會有意無意的用到設計模式,或者是programming idioms。其實idioms離patterns的距離很近,或者也可以說很遠,抽象的高度還是不在一個級别上。   嗯~~最近看得感覺也就這些吧,再寫下重要的幾點來提醒自己: 1 favor composition to inheritance 2 encapsulate what varies 3 depend on abstract not implementation 4 keep easy and stupid 5 never use inheritance only in sake of code reuse 6 maintainance resuable is always more important than code 7 one class one goal   K, that's it, I'll complete this remainder later when I got more  

繼續閱讀