Share
Embed
Transcript
31 slides
-
Clean Architecture – Part I & Part II Blackie Tsai
7 June, 2018 -
目錄與分配 • Part I 簡介 Chapter 1 什麼是設計與結構 Chapter 2
兩種價值觀的故事 • Part II 從基礎開始:程式設計範式 Chapter 3 範式概述 Chapter 4 結構化程式設計 Chapter 5 物件導向程式設計 Chapter 6 函數式程式設計 -
Architecture is Shape • 《Clean Architecture》的原文如下: • The architecture of
a software is the shape given to that system by those who build it. • The form of that shape is in the division of the system into components, the arrangement of those components, and the ways in which those components communicate with each other. -
來談談形狀 • 形狀會跟隨著功能而改變,換句話說,軟體架構的形狀應該也會 跟著功能(use case或user story)而改變。但架構基本上與功 能無關。 • 形狀的目的是為了幫助: •
開發(development) • 佈署(deployment) • 維運(operation) • 維護(maintenance ) • 但,好的形狀(架構)並要能反應系統功能,因為這樣才可以協助 開發、佈署、維運與維護。 -
軟體架構的目標 • The goal of software architecture is to minimize
the human resources required to build and maintain the required system. (軟體架構的目標是最小化建構與維護所需系統的人力 資源) • 最小化軟體生命周期的總成本 • 最大化程式設計師的生產力 -
功能 > 架構 ? • 不一定。一個極端的案例: • 如果您給我一個完美的程式,但不可能修改,那麼當需求改變時,他就 不能工作了,而我也無法使其工作,所以這個程式將變得毫無用處因 •
如果您給我的程式不能工作但易於更改,那麼我可以使其工作,並隨著 需求的變化而持續工作。因此,該程式將持續是有用的 -
Dwight D. Eisenhower Matrix • 軟體的第一個價值 – “行為” 是迫切的,但並不總是特別重 要
• 軟體的第二個價值 – “架構” 是重要的,但從不特別迫切 • 開發人員/團隊有責任主張價 購的重要性高於功能的急迫性 https://www.basecamp.sg/blog/do-you-know-the-difference-between-important-and-urgent -
結構化程式設計(structured programming) • 循序結構:由上而下,依序 逐一執行。(Sequence Structure) • 選擇結構:如If…then…else。 (Selection or
Condition Structure) • 重覆結構:如For…Next, Do while… Loop等 (Iteration or Repetion Structure) http://ms2.ctjh.ntpc.edu.tw/~luti/105str-programing.htm -
總結 • 結構化程式設計的優點 • 程式之邏輯性由上而下之架構,層次分明,易於了解,減少程式邏輯錯 誤。 • 每一模組為一獨立功能,可單獨針對各模組設計、測試,如此可減輕設 計者負擔,維護容易,降低開發成本。 •
結構化程式設計的缺點 • 指令數量較非結構化程式設計多。 • 所佔用記憶體空間較多。 • 執行時間較長。 -
物件導向程式設計(object-oriented programming) • 封裝 (Encapsulation) • 一種將抽象性函式介面的實作細節部份包裝、隱藏起來的方法 • 封裝 (物件導向程式設計)
• 繼承 (Inheritance) • 繼承可建立新類別以重複使用、擴充和修改其他類別中定義的行為 • 繼承 (C# 程式設計手冊) • 多型(Polymorphism) • 相同的函式呼叫介面,可根據不同傳入的物件變數,提供多種類別不同 的方式來實作相同名稱的屬性或方法 • 多型 (C# 程式設計手冊) -
C# 範例 Building building = SomeQueryThatShouldReturnBuilding(); var phoneNumber = null;
// Object-Oriented Programming if(building != null){ if(building.Manager.ContactInfo != null){ if(building.Manager.ContactInfo.PhoneNumber != null){ phoneNumber = building.Manager.ContactInfo.PhoneNumber; } } } // Functional Programming phoneNumber = SomeQueryThatShouldReturnBuilding() .With(b=>b.Manager) .With(m=>m.ContactInfo) .With(c=>c.PhoneNumber); -
JavaScript 範例 [1, 4, 9].map(Math.sqrt).map( function(x){ return x + 1
} ); => [2, 3, 4] -
Object-Oriented 與 Functional Object-Oriented Programming Functional Programming 程式開發抽象的核心 資料結構 函式
資料與操作的關係 緊耦合 鬆耦合 溝通方式 物件(Objects) 透過介面 函式(Functions)透過協定 是否有狀態 有狀態(Stateful) 無狀態(Stateless) 回應值是否透明、可預測 回應值由輸入與狀態確定 回應值總是由輸入確定 核心活動 通過向其添加新方法來組合新的物件對象和 擴展現有物件對象 撰寫更多新函式 -
剩餘章節與分配 • Part III 設計原則 Chapter 7 SRP:單一職責原則 Chapter 8
OCP:開放-封閉原則 Chapter 9 LSP:Liskov 替換原則 Chapter 10 ISP:介面隔離原則 Chapter 11 DIP:依賴反向原則 • Part IV 元件原則 Chapter 12 元件 Chapter 13 元件內聚性 Chapter 14 元件耦合性 • Part V 架構 … • Part VI 細節 … -
推薦閱讀 • [還少一本書] Clean Architecture • Clean Architecture(1):軟體架構的定義與目的 • Rosie
- Android framework to create applications following the principles of Clean Architecture. • Android Architecture • 使用Clean Architecture模式开发Android应用的详细教程