📚 Design Patterns Explained - CH1

📚 Design Patterns Explained - CH1
Design Patterns Explained: A New Perspective on Object Oriented Design

Object-Oriented Paradigm

Purpose: Explain why object-oriented paradigm over functional decomposition.

Naive Problem

If I were to give you the task of writing code to access a description of shapes that were stored in a database and then display them.


Structural Programming thinking - possible steps

  1. Locate the list of shapes in the database. (在資料庫中找到形狀清單)。
  2. Open up the list of shapes. (打開形狀清單)。
  3. Sort the list according to some rules. (依照某種規則將清單排序)。
  4. Display the individual shapes on the monitor. (在螢幕上顯示各種形狀)。

Functional Decomposition (功能分解)

4a. Identify the type of shape. (識別形狀的類型)。 4b. Get the location of the shape. (獲取形狀的位置)。

4c. Call the appropriate function that will display the shape, giving it the shape’s location. (以形狀的位置為參數,呼叫顯示形狀的 Function)。

Functional Decomposition 的挑戰

  1. 它通常會導致一個主程式負責控制子程式(subprograms)。 這使得主程式所承受的責任太多了:要協調各 subprograms,還要控制他們的順序 (Why not delegation?)。 Â«ä¸Šå¸ç‰©ä»¶ï¼šèƒ½è€…多勞»
  2. 如果將實作各步驟的所有邏輯都放在一個大 Function 或大 module 的話,則這些步驟的實質性變化,都必須對這個 Function 或 module 進行修改。 Â«å› æ‡‰è®ŠåŒ–的能力差»


  1. 需求總是在改變 (不變的真理)。
  2. 我們可能不知道會發生什麼變化,但我們能夠猜到哪裡會變化。(OO 最大的優點之一,就是可以封裝這些變化區域)。
  3. 與其抱怨需要總是改變,不如改變開發流程,有效的因應變化。

Dealing with Changes: Functional Decomposition

4c. Call the appropriate function that will display the shape, giving it the shape’s location. (以形狀的位置為參數,呼叫顯示形狀的 Function)。

Using Modularity to Contain Variation

function: display shape
input: type of shape, description of shape
   switch (type of shape)
      case square: put display function for square here
      case circle: put display function for circle here


  1. 如果要顯示一個新的形狀(例如三角形),我可能只需要改這個模組(希望是)。
  2. 這個模組的輸入是 type of shape, description of shape,如果今天使用不同方式提供輸入(例如 array of points 或其他方式),該怎麼辦?

Functional Decomposition 兩個可能的問題

  1. Weak cohesion (Cohesion refers to how “closely the operations in a routine are related; cohesion as clarity because the more that operations are related in a routine (or a class), the easier it is to understand things.)
  2. Tight Coupling (Coupling refers to “the strength of a connection between two routines. Coupling is a complement to cohesion.)

The goal is to create routines with internal integrity (strong cohesion) and small, direct, visible, and flexible relations to other routines (loose coupling).


You are an instructor at a conference. People in your class have another class to attend following yours, but don’t know where it is located. One of your responsibilities is to make sure everyone knows how to get to the next class.



  1. Get list of people in the class.
  2. For each person on this list, do the following:
    1. Find the next class he or she is taking.
    2. Find the location of that class.
    3. Find the way to get from your classroom to the person’s next class.
    4. Tell the person how to get to his or her next class.


  1. A way of getting the list of people in the class.
  2. A way of getting the schedule for each person in the class.
  3. A program that gives someone directions from your classrom to any other classroom.
  4. A control program that works for each person in the class and does the reuqired steps for each person.

不! 你應該會這樣做


You would probably post directions to go from this classroom to the other classrooms and then tell everyone in the class, I have posted the locations of the classes following this in the back of the room, as well as the locations of the other classrooms. Please use them to go to your next classroom.


  1. Giving explicit directions to everyone.
  2. Giving general instructions and then expect the each person will figure out how to do the task individually. (責任轉移 delegation)



  1. 第一種方式,不得不改控製程式,區別研究生跟大學生,然後給研究生特別的指示。
  2. 第二種方式,只需要為研究生再編寫一個程式,但控製程式仍然只需要說: æ‰¾åˆ°ä½ å€‘的下一堂教室。


  1. 人們對自己的行為負責,而不再由一個中央控製程式負責決定。
  2. 控製程式可以與不同類型的學生交流(大學生或研究生)。
  3. 控製程式不需要知道學生從這個教室到下一個教室可能需要採取的任何特殊步驟。


  1. Conceptual: è¡¨ç¤º domain concepts,不管如何實作。回答這個問題:What am I responsibile for?
  2. Specification: é—œæ³¨è»Ÿé«”çš„ interface,但還是不管如何實作。回答這個問題:How am I used?
  3. Implementation: æˆ‘們都很熟。回答這個問題: How do I fulfill my responsibilities?"


概念層次上的溝通:你告訴學生的是「你要他們怎麼做」,而不是「如何去做」。 (you are telling people what you want, not how to do it.)



Object-Oriented Paradigm



The objects were identified by looking at the entities in the problem domain. I identified the responsibilities (or methods) for each object by looking at what these entities need to do.

– ☆☆☆ 理解物件的最佳方式 ☆☆☆ å°‡å…¶çœ‹ç‚ºå…·æœ‰è²¬ä»»çš„東西。物件應該自己負責自己,而且應該清楚地定義責任。

Apply Fowler’s Perspectives

  • At the conceptual level, an object is a set of responsibilities. (一組責任)
  • At the specification level, an object is a set of methods (behaviors) that can be invoked by other objects or by itself. (一組行為)
  • At the implementation level, an object is code and data and computational interactions between them.

Object-Oriented Approach

  1. Start the control program.
  2. Instantiate the collection of students in the classroom.
  3. Tell the collection to have the students go to their next class.
  4. The collection tells each student to go to his or her next class.
  5. Each student:
    1. Finds where his next class is.
    2. Determines how to get there.
    3. Goes there.
  6. Done.

Abstract and Polymorphism

  • Student abstract class, GraduageStudent and RegularStudent classes.
  • Abstract classes define what other, related, classes can do. Abstract classes act as placeholders for other classes.
  • Collection 只需要處理 Student 這個 Concept (Abstract),每一種 Student 都可以按照自己的方式實作功能 (Concrete)。
  • 當我們透過抽象參考,概念性的要求物件做什麼時,將得到不同的行為,具體行為取決於 Derived Class 的實作。(Polymorphism)


  1. Locate the list of shapes in the database. (在資料庫中找到形狀清單)。
  2. Open up the list of shapes. (打開形狀清單)。
  3. Sort the list according to some rules. (依照某種規則將清單排序)。
  4. Display the individual shapes on the monitor. (在螢幕上顯示各種形狀)。

Object-Oriented Approach (1/2)

Object-Oriented Approach (2/2)

  1. 主程式建立一個 ShapeDataBase ç‰©ä»¶ã€‚
  2. 主程式要求 database object 找到我感興趣的一組形狀,並且 instantiate 一個保存這些形狀的 collection 物件.
  3. 主程式要求 Collection 物件排序所存放的形狀。
  4. 主程式要求 Collection 物件顯示形狀。
  5. Collection 物件要求所存放的每個形狀顯示自己。
  6. 每個形狀根據形狀種類顯示自己。


  • 增加新的形狀 (三角形)
    1. 增加一個新的三角形 Concrete Class
    2. 在此 Derived Class 實作自己的顯示方法。
  • 更改排序方式
    1. 修改 Collection 的 Sorting algorithm

結果:The object-oriented approach has limited the impact of changing requirements.


  1. 使用更容易,因為使用者不再需要擔心實作問題
  2. 可以在不考慮呼叫者的情況下修改實作 (因為它一開始就不知道物件怎麼實作的 depend on interface, not implementation)
  3. The internals of an object are unknown to other objects. (封裝使物件內部行為的變化對其他物件變得透明了 <不可見>)。

Take away

  1. 將物件看為具有責任的東西。物件應該自己負責自己,而且應該清楚地定義責任。
  2. 物件真正的威力不在於繼承,而是來自於封裝行為(採取更廣義的封裝定義)。
  3. OO 最大的優點之一,就是可以封裝這些變化區域。
  4. 抽象與責任轉嫁的好處 (placeholder and delegation)。
  5. 三種視角的應用 (在抽象層次上溝通,在實作層次上執行)。