Abstraction 抽象化是指解決問題時,通常與引入相關事物;當描述這些事物時,我們通常僅專注與問題相關的部分,而忽略其他的細節,以免增加問題的難度或干擾解題者的思緒。
實際生活中的例子
當你第一天報到新公司並且要去上班的時候,你只會使用 Google Map 去查詢搭乘路線及適合的交通工具,但你不需要知道真正實際的地形跟地物,或是了解交通工具本身是怎麼運作的(引擎、排檔…)。
此時,Google Map 就是一種 Abstraction 抽象化。
軟體工程中的抽象化
軟體工程中的抽象化
軟體工程特別強調模組化 (modularity) 概念,以便控制軟體發展時的複雜度,通常模組指的是 method 與 class,描述這些模組時,僅說明其規格而非實作的細節。
Functional Abstraction 功能抽象化
僅描述某個 method 提供哪些功能 (what the function does) ,而非此 method 如何實現這些功能 (how the function does)。
Data Abstraction 資料抽象化
描述你可以如何操作一組資料,而非如何實作這些操作與資料儲存的方式。實踐的方式通常是經由系統分析階段去產生 Abstract Data Types (ADT) 抽象資料型態。
Abstract Data Types (ADT) 抽象資料型別
是一堆資料 (data) 的集合,和一組可以在那堆資料上執行的操作 (operation)。
ADT is a collection of data and a set of operations on that data.
Ex 1–1 定義一個 ADT Bag 的規格
[ADT Bag]
- Data: a set of unordered object
- Operations: Add an object, Remove an object, Find an object and Display the Bag
Ex 1–2 根據 ADT Bag 的規格撰寫一個抽象的流程去使用它
create a Bag b
add int 5, 6, 15, 20 to b
remove 6 from b
display b
— — -
result -> 5, 20
可以從上述流程知道我們並不需要清楚了解到實作細節 (Add, Remove…) 就能使用 Bag 這個 ADT 抽象化的資料型別。
Information Hiding 資訊隱藏
information hiding is the principle of segregation of the design decisions in a computer program that are most likely to change, thus protecting other parts of the program from extensive modification if the design decision is changed. (Wiki)
❓ Data Structure 資料結構是不是等於Abstract Data Types (ADT) 抽象資料型別?
其實不盡然,因為 Data Structure 是描述在程式語言中如何儲存一組資料集合的方式,所以資料結構是 ADT 的部分實作細節。
❓ 在發展軟體系統的各階段,嚴格保持抽象化精神有何好處?
非到必要時機,不必討論細節。