2010年5月31日 星期一

Head First OOAD Ch5-2 tips

Good design = flexible software

在本章節剛開頭就開始複習了一下Aggregation vs Composition
這裡很妙,先前剛開始看Aggregation時只知道很明確的是Has -A
的關係,所以Class中有一個private attribute來hold the "Has -A"關係
的對象是剛剛好,但Composition就沒這麼單純,他是個"Is - A"關係,
在這裡並不是說明多型或繼承而是說一個組件的關係,兩者是同時
存在的,詳細的範例可以參考以下網站 !! (討論串相當精闢XD)
其中又不小心逛到一個討論串講到
Composition means it cannot be shared, and when the composite is deleted the parts will be also deleted.

# When you have a set of properties that vary  across your objects , use a collection , like a Map,
   to store those properties dynamically.
# You'll remove lots of methods from your classes , and avoid having to change your code when
   new properties are added to your app.
# A cohesive class does one thing really well and does not try to do or be something else.
# Cohesion : Cohesion measures the degree of connectivity among the elements of single module,
                      class , or object. The Higher the cohesion of your software is, the more well-designed
                      and related the responsibilities of each individual class in your application. Each class
                      has a very specific set of closely related action it performs.
本章總結
OO princiles
1. Encapsulate what varies.
2. Code to an interface rather than to an implementation.
3. Each class in your application should have only one reason to change.
4. Classes are about behavior ad functionality.

沒有留言: