物件導向設計 (object-oriented design)

54
物物物物物物 (Object-Oriented Design) 物物物物物物物物 (self-contained) 物物物 (object) 物物物物物 (object class) 物物物物物

Upload: jean

Post on 05-Jan-2016

42 views

Category:

Documents


0 download

DESCRIPTION

物件導向設計 (Object-Oriented Design). 使用自身定義完整 (self-contained) 的物件 (object) 及物件類別 (object class) 來設計系統. 主題. 物件及物件類別 (object and object class) 物件導向設計 (object-oriented design) 的 行 程 設計的演進 (design evolution). 物件導向設計的特徵 (Characteristics of OOD ). - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 物件導向設計 (Object-Oriented Design)

物件導向設計(Object-Oriented Design)

使用自身定義完整 (self-contained) 的物件 (object) 及物件類別 (object class) 來設計系統

Page 2: 物件導向設計 (Object-Oriented Design)

1. 物件及物件類別 (object and object class)

2. 物件導向設計 (object-oriented design) 的行程

3. 設計的演進 (design evolution)

主題

Page 3: 物件導向設計 (Object-Oriented Design)

物件 (object) 是抽象的 (abstraction) 真實世界 (real-world) 或系統實體 (system entities)

物件是各自獨立的,並將狀態 (state) 及表達資訊 (representation information) 封裝 (encapsulate) 起來

系統功能是以物件服務 (object service) 來表示

物件導向設計的特徵(Characteristics of OOD)

Page 4: 物件導向設計 (Object-Oriented Design)

不使用共享資料的區塊 (shared data areas) ,物件透過訊息傳遞 (message passing) 互相溝通

物件可以是分散式 (distributed) 的,可以循序 (sequentially) 或並行 (parallel) 的方式執行

Page 5: 物件導向設計 (Object-Oriented Design)

互動的物件(Interacting Objects)

Page 6: 物件導向設計 (Object-Oriented Design)

容易維護,物件可視為獨立的實體 物件是可被適度重覆使用 (reuse) 的元件 對於某些系統而言,從真實世界的實體 (real

world entities) 到系統的物件 (system objects)有明顯的對應 (mapping)

物件導向設計的優點

Page 7: 物件導向設計 (Object-Oriented Design)

物件導向分析 (analysis) 、設計 (design) 及程式設計 (programming) 是相關但有區別

物件導向分析 (OOA) 是關於發展 (developing) 應用領域的物件模型 (object model)

物件導向設計 (OOD) 是關於發展 (developing) 物件導向系統模型 (object-oriented system model) 以實作需求 (implement requirements)

物件導向程式設計 (OOP) 是關於利用物件導向的程式語言,如 Java或 C++ ,以實現物件導向設計

物件導向的開發(Object-Oriented Development)

Page 8: 物件導向設計 (Object-Oriented Design)

物件 (object) 是軟體系統中的實體 (entities) ,表示真實世界的實例 (instances)

物件類別 (object class) 是物件的樣板(templates) ,可被用來產生 (create) 物件

物件類別可從其它的物件類別繼承 (inherit)屬性 (attribute) 及服務 (services)

1. 物件 (Object)及物件類別 (Object class)

Page 9: 物件導向設計 (Object-Oriented Design)

一個物件是一個實體,它具有一個狀態(state) 及一組被定義好可作用在此狀態的操作 (operations) 狀態是由一組物件屬性來表示 與物件相關聯的操作,可提供服務給其它物件

( 用戶端 ) 在需要計算 (computation) 時使用

物件(Object)

Page 10: 物件導向設計 (Object-Oriented Design)

物件是根據某些物件類別的定義所建立 物件類別的定義被視為物件的樣板 它包含所有屬性 (attributes) 的宣告 (declaration)

及所有與該類別物件有所關聯的服務 (services)

Page 11: 物件導向設計 (Object-Oriented Design)

統一塑模語言(Unified Modelling Language)

在 1980及 1990 年代,有幾種不同用以描述物件導向設計的表示法 (notations) 被提出

統一塑模語言 (UML) 是這些表示法的整合(integration)

UML 用以描述在 OO 分析和設計時,可能用到之不同模型的表示法 (notations)

統一塑模語言如今是物件導向塑模的業界標準 (de facto standard)

Page 12: 物件導向設計 (Object-Oriented Design)

員工的物件類別 (UML)

Page 13: 物件導向設計 (Object-Oriented Design)

物件的溝通(Object Communication)

概念上,物件是透過訊息傳遞 (message passing) 來溝通

物件溝通是透過其它物件提出要求服務(request services)( 呼叫方法 (calling methods)) ,透過交換所需要的資訊提供服務

執行服務所需要的資訊及執行服務後的結果會被當作參數來傳遞 (passed as parameters)

Page 14: 物件導向設計 (Object-Oriented Design)

Message Examples( 訊息的例子 )

// 呼叫 (call) 與暫存物件 (buffer object) 相關聯的方法 (method) ,並回傳暫存器中的下一個值

v = circularBuffer.Get () ;

// 呼叫與自動調溫器物件 (thermostat object)相關聯的方法,並設定維持的溫度

thermostat.setTemp (20) ;

Page 15: 物件導向設計 (Object-Oriented Design)

一般化和繼承(Generalisation and Inheritance)

物件是類別的成員 (members) ,類別定義屬性類型 (attribute types) 和操作 (operations)

類別可被排列成類別階層 (class hierarchy) 架構,其中一個類別 ( 父類別 (super-class)) 是其它一個或多個類別 ( 子類別 ) 的一般化 (generalization)

子類別繼承父類別的屬性和操作,自己也可增加新的方法 (methods) 或屬性 (attributes)

在 UML 中,一般化 (generalisation) 是實作成OO 程式語言中的繼承 (inheritance)

Page 16: 物件導向設計 (Object-Oriented Design)

一般化的階層架構(Generalisation Hierarchy)

Page 17: 物件導向設計 (Object-Oriented Design)

繼承的優點 繼承是抽象 (abstraction) 的機制,被用來分

類實體 (classify entities) 繼承是在設計 (design) 和程式設計

(programming) 層次中重覆使用的機制 繼承圖 (inheritance graph) 是組織領域

(domains) 和系統 (systems) 知識的源頭(source)

Page 18: 物件導向設計 (Object-Oriented Design)

繼承的問題 物件類別不能自身完整說明 (not self-

contained) ,若無參照它們的父類別 (super-classes) 無法獨立的被理解

Page 19: 物件導向設計 (Object-Oriented Design)

UML 關聯(UML Association)

物件與物件類別可參與和其它物件與物件類別的關係 (relationships)

在 UML 中,一般化關係 (generalized relationship) 是由關聯 (association) 來表示

關聯 (association) 可以加註 (annotate) 一些描述此關聯的資訊

關聯 (association) 雖是一般性的 (general) ,但可指出一個物件的屬性 (object attribute) 是一個關聯物件(associated object) ,或一個方法 (method) 必須依賴(rely on) 一個關聯物件

Page 20: 物件導向設計 (Object-Oriented Design)

關聯模型(Association Model)

Page 21: 物件導向設計 (Object-Oriented Design)

2. 物件導向設計行程 (Object-Oriented Design)

定義系統使用的內容 (context) 和模型(model)

設計系統架構 (system architecture) 辨識主要的系統物件 (principal system

objects) 發展設計模型 (design model) 指定物件介面 (specify object interface)

Page 22: 物件導向設計 (Object-Oriented Design)

天氣系統描述(Weather System Description)

A weather data collection system is required to generate weather maps on a regular basis using data collected from remote, unattended weather stations and other data sources such as weather observers, balloons and satellites. Weather stations transmit their data to the area computer in response to a request from that machine.

The area computer validates the collected data and integrates it with the data from different sources. The integrated data is archived [ 把 ...存檔 ] and, using data from this archive and a digitised map database, a set of local weather maps is created. Maps may be printed for distribution on a special-purpose map printer or may be displayed in a number of different formats.

Page 23: 物件導向設計 (Object-Oriented Design)

氣象台描述(Weather Station Description)

氣象台 (weather station) 受軟體程式控制的儀器,用來收集資料、處理資料

和傳輸資料,以做進一步的處理 儀器 (instrument)

包括空氣和地面溫度計 (air and ground thermometer) 、風速計 (anemometer) 、風向標 (wind vane) 、氣壓計(barometer) 和測量儀器 (rain gauge)

資料 (data) 每五分鐘採樣一次

Page 24: 物件導向設計 (Object-Oriented Design)

當下達命令 (command) 要傳送氣象資料時 氣象站開始處理和彙整採樣的資料 當接收到要求時,彙整的資料被傳送到圖象電腦 (mapping computer)

Page 25: 物件導向設計 (Object-Oriented Design)

分層架構(Layered Architecture)

分層架構反應系統在不同階段時的處理(processing) 資料收集 (data collection) 資料整合 (data integration) 資料記錄 (data archiving) 氣象圖的產生 (map generation)

Page 26: 物件導向設計 (Object-Oriented Design)

天氣圖系統的分層架構(Layered Architecture for Weather Mapping System)

Page 27: 物件導向設計 (Object-Oriented Design)

系統內容和系統使用模型(System Context and Models of Use)

系統內容 一個靜態模型,用來描述在環境中的其它系統 用子系統模型 (subsystem model) 展示其它系統 接續的投影片展示氣象站系統周圍的系統

系統使用的模型 一個動態的系統,描述系統如何和它所在環境

的溝通 (interface) 利用”使用案例” (use-cases) 來描繪互動

(interactions) 的情形

Page 28: 物件導向設計 (Object-Oriented Design)

氣象圖系統的子系統(Subsystems in the Weather Mapping System)

Page 29: 物件導向設計 (Object-Oriented Design)

氣象台使用案例(Use-Cases for the Weather Station)

Page 30: 物件導向設計 (Object-Oriented Design)

使用案例描述

System Weather station Use-case Report Actors Weather data collection system, Weather station Data The weather station sends a summary of the weather data that has been collected from the

instruments in the collection period to the weather data collection system. The data sent are the maximum minimum and average ground and air temperatures, the maximum, minimum and average air pressures, the maximum, minimum and average wind speeds, the total rainfall and the wind direction as sampled at 5 minute intervals.

Stimulus The weather data collection system establishes a modem link with the weather station and requests transmission of the data.

Response The summarised data is sent to the weather data collection system Comments Weather stations are usually asked to report once per hour but this frequency may differ

from one station to the other and may be modified in future.

Page 31: 物件導向設計 (Object-Oriented Design)

架構設計(Architecture Design)

在了解系統和系統環境的互動後,開始系統架構設計

分層式架構 (layered architecture) 適用於氣象台系統 介面層 (interface layer)負責處理溝通 (handle

communications) 資料收集層 (data collection layer)負責管理儀器

(manage instruments) 儀器層 (instrument)負責採樣資料 (collect date)

Page 32: 物件導向設計 (Object-Oriented Design)

氣象台架構(Weather Station Architecture)

Page 33: 物件導向設計 (Object-Oriented Design)

物件識別(Object Identification)

物件導向設計最困難的部份是識別物件(object) 或物件類別 (object class)

物件識別 (object identification)沒有神奇的公式 (magic formula) 它依賴系統設計者的技巧 (skill) 、經驗

(experience) 及對該領域所具備的知識 物件識別是一個反覆的行程 (iterative

process) 不可能第一次就識別的完全

Page 34: 物件導向設計 (Object-Oriented Design)

識別方法(Approaches to Identification)

對系統用自然語言的描述,使用符合語法(grammatical) 的方法

在應用領域中識別可觸摸的實體 利用行為方法 (behavior approach) ,識別哪

些物件參與哪些行為 利用情境為基礎的分析 (scenario-based

analysis) 識別在各情境中的物件 (objects) 、屬性

(attributes) 和方法 (methods)

Page 35: 物件導向設計 (Object-Oriented Design)

氣象台物件類別(Weather Station Object Classes)

地面溫度計 (ground thermometer) 、風速計(anemometer) 和氣壓計 (barometer) 應用領域的物件,是在系統中與儀器相關的硬

體物件 氣象台 (weather station)

氣象台和其環境的基本介面 (basic interface) 它反映使用案例模型 (use-case model) 中識

別出的互動 (interactions)

Page 36: 物件導向設計 (Object-Oriented Design)

氣象資料 封裝 (encapsulate) 來自儀器的彙整資料

Page 37: 物件導向設計 (Object-Oriented Design)

氣象台物件類別(Weather Station Object Classes)

Page 38: 物件導向設計 (Object-Oriented Design)

更深一層的物件和物件的細緻化(Further Objects and Object Refinement)

使用領域知識以識別更多物件和操作 氣象台需要有唯一 (unique) 的識別碼 氣象台坐落於遙遠的地區,必須自動回報儀器

的錯誤,因此必須有自我檢查的屬性和操作 主動或被動物件 (active or passive objects)

本例中,物件是被動的,當被要求時才進行採集資料,而非獨立自主的 (autonomously)

Page 39: 物件導向設計 (Object-Oriented Design)

設計模型(Design Models)

設計模型表示物件、物件類別及這些實體間的關係 (relationships)

靜態模型 (static model) 以物件類別和關係描述系統的靜態架構 (static

structure) 動態模型 (dynamic model)

描述物件間的動態互動 (dynamic interactions)

Page 40: 物件導向設計 (Object-Oriented Design)

設計模型的實例 子系統模型 (sub-system model)

表示將物件邏輯性地組成緊密結合的子系統(logical groupings of objects into coherent subsystems)

循序模型 (sequence model) 表示物件互動的順序 (sequence of object interactions)

狀態機模型 (state machine model) 展示個別物件 (individual objects) 如何因回應事件而改變狀態 (change state in response to events)

Page 41: 物件導向設計 (Object-Oriented Design)

其他模型,包括使用案例模型 (use-case models)、聚集模型 (aggregation models) 和一般化模型(generalization models)等

Page 42: 物件導向設計 (Object-Oriented Design)

子系統模型(Subsystem Models)

展示如何把設計組織成邏輯相關的物件群組(logically related groups of objects)

在 UML 中,它用套件 (package) 來表示,是一種封裝結構 (encapsulation construct) 它是一個邏輯模型 (logical model) 實際物件的組織 (organization) 可能會有所不

Page 43: 物件導向設計 (Object-Oriented Design)

氣象台子系統(Weather Station Subsystems)

Page 44: 物件導向設計 (Object-Oriented Design)

循序模型(Sequence Model)

循序模型表示物件互動發生的順序 物件水平式地排列在最頂端 垂直方向則代表時間,模型是由上而下 (top to

bottom)閱讀 標示的箭頭 (labeled arrows) 代表物件間的互

動,不同樣式的箭頭代表不同型態的互動 物件時間軸 (object lifeline) 上的細長方形代

表系統中主控物件的時間

Page 45: 物件導向設計 (Object-Oriented Design)

資料採集順序(Data Collection Sequence)

Page 46: 物件導向設計 (Object-Oriented Design)

狀態圖(Statecharts)

表示物件如何回應 (respond) 不同的服務請求(service request) 以及這些請求如何觸發(trigger) 狀態轉換 (state transition) 若物件狀態是 Shutdown ,則回應 Startup()

訊息 (message) 若物件處於等待狀態 (waiting state) ,則該

物件是等待後續的訊息 (further message)

Page 47: 物件導向設計 (Object-Oriented Design)

若 reportWeather()被呼叫,則系統轉移到彙整狀態 (summarizing state)

若 calibrate()被呼叫,則系統移轉到calibrating狀態

當接受到系統時鐘 (clock)的訊號,則進入收集狀態 (collecting state)

Page 48: 物件導向設計 (Object-Oriented Design)

氣象台狀態圖(Weather Station State Diagram)

Page 49: 物件導向設計 (Object-Oriented Design)

物件介面規格(Object Interface Specification)

物件介面須被指定 (specified) ,以便同時設計物件和其它元件

物件可能會有許多介面 (several interfaces) ,他們是方法 (methods) 所提供的觀點(viewpoints)

UML 使用類別圖 (class diagrams) 制訂介面規格 (interface specification)

Page 50: 物件導向設計 (Object-Oriented Design)

氣象台介面(Weather Station Interface)

interface WeatherStation {

public void WeatherStation () ;

public void startup () ;public void startup (Instrument i) ;

public void shutdown () ;public void shutdown (Instrument i) ;

public void reportWeather ( ) ;

public void test () ;public void test ( Instrument i ) ;

public void calibrate ( Instrument i) ;

public int getID () ;

} //WeatherStation

Page 51: 物件導向設計 (Object-Oriented Design)

3. 設計的演進(Design Evolution)

將資訊隱藏 (hiding information) 在物件中,意謂物件變更不會對其它物件產生非預期性的影響

假設氣象台要加入污染的監控設施 這些空氣的樣本,計算大氣中不同污染物質的

含量 污染指數跟著氣象資料傳回

Page 52: 物件導向設計 (Object-Oriented Design)

所需的改變(Changes Required)

增加一個物件類別 (object class) 名為 ‘ Air quality’ ,是氣象台 (WeatherStation)

的一部份 增加操作 (operation)

加入一個 reportAirQuality 的操作到氣象站(WeatherStation)

修改收集污染指數的控制軟體 增加物件 (objects)

表示污染監控的儀器

Page 53: 物件導向設計 (Object-Oriented Design)

污染監控(Pollution Monitoring)

Page 54: 物件導向設計 (Object-Oriented Design)

參考資料 Ian Sommerville, Software Engineering, 7th ed.,

Addison-Wesley, 2004.