• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            子彈 の VISIONS

            NEVER back down ~~

            C++博客 首頁 新隨筆 聯(lián)系 聚合 管理
              112 Posts :: 34 Stories :: 99 Comments :: 0 Trackbacks
            來自: IBM Rational Edge
             

            Illustration

            現(xiàn)在是二月,而且到如今你或許已經(jīng)讀到、或聽到人們談論UML 2.0 —— 包括若干進步的 UML 的新規(guī)范,所做的變化。考慮到新規(guī)范的重要性,我們也正在修改這個文章系列的基礎,把我們的注意力從 OMG 的 UML 1.4 規(guī)范,轉(zhuǎn)移到 OMG 的已采納 UML 2.0草案規(guī)范(又名 UML 2)。我不喜歡在一系列文章的中間,把重點從 1.4 變?yōu)?2.0 ,但是 UML 2.0 草案規(guī)范是前進的重要一步,我感覺需要擴充文字。

            由于一些理由,OMG 改良了 UML 。主要的理由是,他們希望 UML 模型能夠表達模型驅(qū)動架構(gòu)(MDA),這意味著 UML 必須支持更多的模型驅(qū)動的符號。同時, UML 1.x 符號集合有時難以適用于較大的應用程序。此外,為了要使圖變成更容易閱讀,需要改良符號元件。(舉例來說,UML 1.x 的模型邏輯流程太復雜,有時不可能完成。對UML 2 中的序列圖的符號集合的改變,已經(jīng)在序列化邏輯建模方面取得巨大的進步)。

            注意我上面所述的文字:“已采納UML2.0草案規(guī)范。”確實,規(guī)范仍然處于草案狀態(tài),但是關(guān)鍵是草案規(guī)范已經(jīng)被 OMG 采用,OMG是一個直到新標準相當可靠,才會采用它們的組織。 在 UML 2 完全地被采用之前,規(guī)范將會有一些修改,但是這些改變應該是極小的。主要的改變將會是在 UML 的內(nèi)部 —— 包括通常被實施 UML 工具的軟件公司使用的功能。

            本文的主要目的是繼續(xù)把我們的重點放在基礎UML圖上;這個月,我們進一步了解序列圖。再次請注意,下面提供的例子正是以新的 UML 2 規(guī)范為基礎。

            圖的目的
            序列圖主要用于按照交互發(fā)生的一系列順序,顯示對象之間的這些交互。很象類圖,開發(fā)者一般認為序列圖只對他們有意義。然而,一個組織的業(yè)務人員會發(fā)現(xiàn),序列圖顯示不同的業(yè)務對象如何交互,對于交流當前業(yè)務如何進行很有用。除記錄組織的當前事件外,一個業(yè)務級的序列圖能被當作一個需求文件使用,為實現(xiàn)一個未來系統(tǒng)傳遞需求。在項目的需求階段,分析師能通過提供一個更加正式層次的表達,把用例帶入下一層次。那種情況下,用例常常被細化為一個或者更多的序列圖。

            組織的技術(shù)人員能發(fā)現(xiàn),序列圖在記錄一個未來系統(tǒng)的行為應該如何表現(xiàn)中,非常有用。在設計階段,架構(gòu)師和開發(fā)者能使用圖,挖掘出系統(tǒng)對象間的交互,這樣充實整個系統(tǒng)設計。

            序列圖的主要用途之一,是把用例表達的需求,轉(zhuǎn)化為進一步、更加正式層次的精細表達。用例常常被細化為一個或者更多的序列圖。序列圖除了在設計新系統(tǒng)方面的用途外,它們還能用來記錄一個存在系統(tǒng)(稱它為“遺產(chǎn)”)的對象現(xiàn)在如何交互。當把這個系統(tǒng)移交給另一個人或組織時,這個文檔很有用。

            符號
            既然這是我基于 UML 2的 UML 圖系列文章的第一篇,我們需要首先討論對 UML 2 圖符號的一個補充,即一個叫做框架的符號元件。在 UML 2中,框架元件用于作為許多其他的圖元件的一個基礎,但是大多數(shù)人第一次接觸框架元件的情況,是作為圖的圖形化邊界。當為圖提供圖形化邊界時,一個框架元件為圖的標簽提供一致的位置。在 UML 圖中框架元件是可選擇的;就如你能在圖 1 和 2 中見到的,圖的標簽被放在左上角,在我將調(diào)用框架的“namebox”中,一種卷角長方形,而且實際的 UML 圖在較大的封閉長方形內(nèi)部定義。

            圖 1: 空的 UML 2 框架元件

            圖 1: 空的 UML 2 框架元件

            除了提供一個圖形化邊框之外,用于圖中的框架元件也有描述交互的重要的功能, 例如序列圖。在序列圖上一個序列接收和發(fā)送消息(又稱交互),能通過連接消息和框架元件邊界,建立模型(如圖 2 所見到)。這將會在后面“超越基礎”的段落中被更詳細地介紹。

            圖 2: 一個接收和發(fā)送消息的序列圖

            圖 2: 一個接收和發(fā)送消息的序列圖

            注意在圖 2 中,對于序列圖,圖的標簽由文字“sd”開始。當使用一個框架元件封閉一個圖時,圖的標簽需要按照以下的格式:

            圖類型 圖名稱

            UML 規(guī)范給圖類型提供特定的文本值。(舉例來說,sd代表序列圖,activity代表活動圖,use case代表用例圖)。

            基礎
            序列圖的主要目的是定義事件序列,產(chǎn)生一些希望的輸出。重點不是消息本身,而是消息產(chǎn)生的順序;不過,大多數(shù)序列圖會表示一個系統(tǒng)的對象之間傳遞的什么消息,以及它們發(fā)生的順序。圖按照水平和垂直的維度傳遞信息:垂直維度從上而下表示消息/調(diào)用發(fā)生的時間序列,而且水平維度從左到右表示消息發(fā)送到的對象實例。

            生命線
            當畫一個序列圖的時候,放置生命線符號元件,橫跨圖的頂部。生命線表示序列中,建模的角色或?qū)ο髮嵗?a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 1 生命線畫作一個方格,一條虛線從上而下,通過底部邊界的中心(圖 3)。生命線名字放置在方格里。

            圖 3: 用于一個實體名為freshman的生命線的Student類的一個例子

            圖 3: 用于一個實體名為freshman的生命線的Student類的一個例子

            UML 的生命線命名標準按照如下格式:

            實體名 : 類名

            在如圖3所示的例子中,生命線表示類Student的實體,它的實體名稱是freshman。這里注意一點,生命線名稱帶下劃線。當使用下劃線時,意味著序列圖中的生命線代表一個類的特定實體,不是特定種類的實體(例如,角色)。在將來的一篇文章中,我們將會了解結(jié)構(gòu)化建模。現(xiàn)在,僅僅評述序列圖,可能包含角色(例如買方賣方),而不需要敘述誰扮演那些角色(例如BillFred)。這準許不同語境的圖重復使用。簡單拖放,序列圖的實例名稱有下劃線,而角色名稱沒有。

            圖 3 中我們生命線例子是一個命名的對象,但是不是所有的生命線都代表命名的對象。相反的,一個生命線能用來表現(xiàn)一個匿名的或未命名的實體。當在一個序列圖上,為一個未命名的實例建模時,生命線的名字采用和一個命名實例相同的模式;但是生命線名字的位置留下空白,而不是提供一個例圖名字。再次參考圖 3,如果生命線正在表現(xiàn)Student類的一個匿名例圖,生命線會是: “Student”。同時, 因為序列圖在項目設計階段中使用,有一個未指定的對象是完全合法: 舉例來說,“freshman”。

            消息
            為了可讀性,序列圖的第一個消息總是從頂端開始,并且一般位于圖的左邊。然后繼發(fā)的消息加入圖中,稍微比前面的消息低些。

            為了顯示一個對象(例如,生命線)傳遞一個消息給另外一個對象,你畫一條線指向接收對象,包括一個實心箭頭(如果是一個同步調(diào)用操作)或一個棍形箭頭(如果是一個異步訊號)。消息/方法名字放置在帶箭頭的線上面。正在被傳遞給接收對象的消息,表示接收對象的類實現(xiàn)的一個操作/方法。在圖 4 的例子中,analyst對象調(diào)用ReportingSystem 類的一個實例的系統(tǒng)對象。analyst對象在調(diào)用系統(tǒng)對象的 getAvailableReports 方法。系統(tǒng)對象然后調(diào)用secSystem 對象上的、包括參數(shù)userId的getSecurityClearance 方法,secSystem的類的類型是 SecuritySystem。 2

            圖 4: 一個在對象之間傳遞消息的實例

            圖 4: 一個在對象之間傳遞消息的實例

            除了僅僅顯示序列圖上的消息調(diào)用外,圖 4 中的圖還包括返回消息。這些返回消息是可選擇的;一個返回消息畫作一個帶開放箭頭的虛線,向后指向來源的生命線,在這條虛線上面,你放置操作的返回值。在圖 4 中,當 getSecurityClearance 方法被調(diào)用時,secSystem 對象返回 userClearance 給系統(tǒng)對象。當 getAvailableReports 方法被調(diào)用時,系統(tǒng)對象返回 availableReports。

            此外,返回消息是序列圖的一個可選擇部分。返回消息的使用依賴建模的具體/抽象程度。如果需要較好的具體化,返回消息是有用的;否則,主動消息就足夠了。我個人喜歡,無論什么時候返回一個值,都包括一個返回消息,因為我發(fā)現(xiàn)額外的細節(jié)使一個序列圖變得更容易閱讀。

            當序列圖建模時,有時候,一個對象將會需要傳遞一個消息給它本身。一個對象何時稱它本身?一個純化論者會爭辯一個對象應該永不傳遞一個消息給它本身。然而,為傳遞一個消息給它本身的對象建模,在一些情境中可能是有用的。舉例來說,圖 5 是圖 4 的一個改良版本。 圖 5 版本顯示調(diào)用它的 determineAvailableReports 方法的系統(tǒng)對象。通過表示系統(tǒng)傳遞消息“determineAvailableReports”給它本身,模型把注意力集中到過程的事實上,而不是系統(tǒng)對象。

            為了要畫一個調(diào)用本身的對象,如你平時所作的,畫一條消息,但是不是連接它到另外的一個對象,而是你把消息連接回對象本身。

            圖 5: 系統(tǒng)對象調(diào)用它的 determineAvailableReports 方法

            圖 5: 系統(tǒng)對象調(diào)用它的 determineAvailableReports 方法

            圖 5 中的消息實例顯示同步消息;然而,在序列圖中,你也能為異步消息建模。一個異步消息和一個同步的畫法類似,但是消息畫的線帶一個棍形矛頭,如圖 6 所示。

            圖 6: 表示傳遞到實體2的異步消息的序列圖片段

            圖 6: 表示傳遞到實體2的異步消息的序列圖片段

            約束
            當為對象的交互建模時,有時候,必須滿足一個條件,消息才會傳遞給對象。約束在 UML 圖各處中,用于控制流。在這里,我將會討論UML 1.x 及UML 2.0兩者的約束。在 UML 1.x 中,一個約束只可能被分配到一個單一消息。UML 1.x中,為了在一個序列圖上畫一個約束,你把約束元件放在約束的消息線上,消息名字之前。圖 7 顯示序列圖的一個片段,消息addStudent 方法上有一個約束。

            圖 7:UML 1.x 序列圖的一個片段,其中addStudent 消息有一個約束

            圖 7:UML 1.x 序列圖的一個片段,其中addStudent 消息有一個約束

            在圖 7 中,約束是文本“[ pastDueBalance=0]”。通過這個消息上的約束,如果應收帳系統(tǒng)返回一個零點的逾期平衡,addStudent 消息才將會被傳遞。約束的符號很簡單;格式是:

            [Boolean Test]

            舉例來說,

            [pastDueBalance = 0]

            組合碎片(變體方案,選擇項,和循環(huán))
            然而,在大多數(shù)的序列圖中,UML 1.x“in-line”約束不足以處理一個建模序列的必需邏輯。這個功能缺失是 UML 1.x 的一個問題。UML 2 已經(jīng)通過去掉“in-line”約束,增加一個叫做組合碎片的符號元件,解決了這一個問題。一個組合碎片用來把一套消息組合在一起,在一個序列圖中顯示條件分支。UML 2 規(guī)范指明了組合碎片的 11 種交互類型。十一種中的三種將會在“基礎”段落中介紹,另外兩種類型將會在“超越基礎”中介紹,而那剩余的六種我將會留在另一篇文章中介紹。(嗨,這是一篇文章而不是一本書。我希望你在一天中看完這部分!)

            變體
            變體用來指明在兩個或更多的消息序列之間的、互斥的選擇。 3 變體支持經(jīng)典的“if then else”邏輯的建模(舉例來說,如果 我買三個,然后 我得到 我購買的20% 折扣;否則 我得到我購買的 10% 折扣)。

            就如你將會在圖 8 中注意到的,一個變體的組合碎片元件使用框架來畫。單詞“alt”放置在框架的namebox里。然后較大的長方形分為 UML 2 所稱的操作元。 4 操作元被虛線分開。每個操作元有一個約束進行測試,而這個約束被放置在生命線頂端的操作元的左上部。 5 如果操作元的約束等于“true”,然后那個操作元是要執(zhí)行的操作元。

            圖 8:包含變體組合碎片的一個序列圖片段

            圖 8:包含變體組合碎片的一個序列圖片段

            圖 8作為一個變體的組合碎片如何閱讀的例子,顯示序列從頂部開始,即bank對象獲取支票金額和帳戶結(jié)余。此時,序列圖中的變體組合碎片接管。因為約束“[balance >= amount]”,如果余額超過或等于金額,然后順序進行bank對象傳遞 addDebitTransaction 和 storePhotoOfCheck 消息給account對象。然而,如果余額不是超過或等于金額,然后順序的過程就是bank傳遞addInsuffientFundFee 和 noteReturnedCheck 消息給account對象,returnCheck 消息給它自身。因為“else”約束,當余額不大于或者等于金額時,第二個序列被調(diào)用。在變體的組合碎片中,不需要“else”約束;而如果一個操作元,在它上面沒有一個明確的約束,那么將假定“else”約束。

            變體的組合碎片沒被限制在簡單的“if then else”驗證。可能需要大量的變體路徑。 如果需要較多的變體方案,你一定要做的全部工作就是把一個操作元加入有序列約束和消息的長方形中。

            選擇項
            選擇項組合碎片用來為序列建模,這些序列給予一個特定條件,將會發(fā)生的;或者,序列不發(fā)生。一個選擇項用來為簡單的“if then”表達式建模。(例如,如果架上的圈餅少于五個,那么另外做兩打圈餅)。

            選擇項組合碎片符號與變體組合碎片類似,除了它只有一個操作元并且永不能有“else”約束以外(它就是如此,沒有理由)。要畫選擇項組合,你畫一個框架。文字“opt”是被放置在框架的 namebox 里的文本,在框架的內(nèi)容區(qū),選擇項的約束被放置在生命線頂端上的左上角。 然后選擇項的消息序列被放在框架的內(nèi)容區(qū)的其余位置內(nèi)。這些元件如圖 9 所示。

            圖 9:包括選擇項組合碎片的一個序列圖片段

            圖 9:包括選擇項組合碎片的一個序列圖片段

            閱讀選擇項組合碎片很容易。圖 9 是圖 7 的序列圖片段的再加工,但是這次它使用一個選擇項組合碎片,因為如果Student的逾期平衡等于0,需要傳遞更多的消息。按照圖 9 的序列圖,如果Student的逾期平衡等于零,然后傳遞addStudent,getCostOfClass和chargeForClass消息。如果Student的逾期平衡不等于零,那么在選擇項組合碎片中,序列不傳遞任何一個消息。

            例子圖 9的序列圖片段包括一個選擇項約束;然而,約束不是一個必需的元件。在高層次、抽象的序列圖中,你可能不想敘述選擇項的條件。你可能只是想要指出片段是可選擇的。

            循環(huán)
            有時候你將會需要為一個重復的序列建模。在 UML 2 中,為一個重復的序列建模已經(jīng)改良,附加了循環(huán)組合碎片。

            循環(huán)組合碎片表面非常類似選擇項組合碎片。你畫一個框架,在框架的 namebox 中放置文本“loop”。在框架的內(nèi)容區(qū)中,一個生命線的頂部,循環(huán)約束 6 被放置在左上角。然后循環(huán)的消息序列被放在框架內(nèi)容區(qū)的其余部分中。在一個循環(huán)中,除了標準的布爾測試外,一個約束能測試二個特定的條件式。特定的約束條件式是寫作“minint = [the number]”(例如,“minint = 1”)的最小循環(huán)次數(shù),和寫作“maxint = [the number]”(例如,“maxint = 5”)的最大循環(huán)次數(shù)。通過最小循環(huán)檢驗,循環(huán)必須運行至少指定次數(shù),而循環(huán)執(zhí)行次數(shù)不能達到約束指定的最大循環(huán)次數(shù)。

            圖 10:循環(huán)組合碎片的一個序列圖例子

            圖 10:循環(huán)組合碎片的一個序列圖例子 (單擊放大)

            在圖 10 中顯示的循環(huán)運行,直到 reportsEnu 對象的 hasAnotherReport 消息返回false。如果循環(huán)序列應該運行,這個序列圖的循環(huán)使用一個布爾測試確認。為了閱讀這個圖,你和平常一樣,從頂部開始。當你到達循環(huán)組合碎片,做一個測試,看看值 hasAnotherReport 是否等于true。如果 hasAnotherReport 值等于true,于是序列進入循環(huán)片斷。然后你能和正常情況一樣,在序列圖中跟蹤循環(huán)的消息。

            超越基礎

            我已經(jīng)介紹了序列圖的基礎,應該使你可以為將會在系統(tǒng)中通常發(fā)生的大部份交互建模。下面段落將會介紹用于序列圖的比較高階的符號元件。

            引用另外一個序列圖
            當做序列圖的時候,開發(fā)者愛在他們的序列圖中,重用存在的序列圖。 7 在 UML 2 中開始,引進“交互進行”元件。追加交互進行的可以說是 UML 2 交互建模中的最重要的創(chuàng)新。交互進行增加了功能,把原始的序列圖組織成為復雜的序列圖。由于這些,你能組合(重用)較簡單的序列,生成比較復雜的序列。這意味你能把完整的、可能比較復雜的序列,抽象為一個單一的概念單位。

            一個交互進行元件使用一個框架繪制。文字“ref”放置在框架的 namebox 中,引用的序列圖名字放置在框架的內(nèi)容區(qū)里,連同序列圖的任何參數(shù)一起。引用序列圖的名字符號如下模式:

            序列圖名[(參數(shù))] [: 返回值]

            兩個例子:

            1. Retrieve Borrower Credit Report(ssn) : borrowerCreditReport

            或者

            2. Process Credit Card(name, number, expirationDate, amount : 100)

            在例子 1 中,語法調(diào)用叫做Retrieve Borrower Credit Report的序列圖,傳遞給它參數(shù) ssn。序列Retreive Borrower Credit Report返回變量 borrowerCreditReport 。

            在實例 2 中,語法調(diào)用叫做Process Credit Card的序列圖,傳遞給它參數(shù)name,number,expiration date,和 amount。然而,在例子 2 中,amount參數(shù)將會是值100。因為例子2沒有返回值標簽,序列不返回值(假設,建模的序列不需要返回值)。

            圖 11: 一個引用兩個不同序列圖的序列圖

            圖 11: 一個引用兩個不同序列圖的序列圖

            圖 11 顯示一個序列圖,它引用了序列圖“Balance Lookup”和“Debit Account”。序列從左上角開始,客戶傳遞一個消息給teller對象。teller對象傳遞一個消息給 theirBank 對象。那時,調(diào)用Balance Lookup序列圖,而 accountNumber作為一個參數(shù)傳遞。Balance Lookup序列圖返回balance變量。然后檢驗選擇項組合碎片的約束條件,確認余額大于金額變量。在余額比金額更大的情況下,調(diào)用Debit Account序列圖,給它傳遞參數(shù)accountNumber 和amount。在那個序列完成后,withdrawCash 消息為客戶返回cash。

            重要的是,注意在圖 11 中,theirBank 的生命線被交互進行Balance Lookup隱藏了。因為交互進行隱藏生命線,意味著theirBank 生命線在“Balance Lookup”序列圖中被引用。除了隱藏交互進行的生命線之外,UML 2 也指明,生命線在它自己的“Balance Lookup”序列中,一定有相同的 theirBank 。

            有時候,你為一個序列圖建模,其中交互進行會重疊沒有 在交互進行中引用的生命線。在那種情況下,生命線和正常的生命線一樣顯示,不會被重疊的交互進行隱藏。

            在圖 11 中,序列引用“Balance Lookup”序列圖。“Balance Lookup”序列圖在圖 12 中顯示。因為例子序列有參數(shù)和一個返回值,它的標簽 —— 位于圖的 namebox 中 —— 按照一個特定模式:

            圖類型 圖名 [參數(shù)類型:參數(shù)名]
            [: 返回值類型]

            兩個例子:

            1. SD Balance Lookup(Integer : accountNumber) : Real

            2. SD Available Reports(Financial Analyst : analyst) : Reports

            圖 12 舉例說明例子 1,在里面,Balance Lookup序列把參數(shù) accountNumber 作為序列中的變量使用,序列圖顯示返回的Real對象。在類似這種情況下,返回的對象采用序列圖實體名。

            圖 12: 一個使用 accountNumber 參數(shù)并返回一個Real對象的序列圖

            圖 12: 一個使用 accountNumber 參數(shù)并返回一個Real對象的序列圖

            圖 13 舉例說明例子 2,在里面,一個序列圖獲取一個參數(shù),返回一個對象。然而,在圖 13 中參數(shù)在序列的交互中使用。

            圖 13: 一個在它的交互中使用參數(shù)、返回一個Reports對象的序列圖

            圖 13: 一個在它的交互中使用參數(shù)、返回一個Reports對象的序列圖


            前面的段落展示如何通過參數(shù)和返回值傳遞信息,引用另一個序列圖。然而,有另一個方法在序列圖之間傳遞消息。門可能是一個容易的方法,為在序列圖和它的上下文之間的傳遞消息建模。一個門只是一個消息,圖形表示為一端連接序列圖的框架邊緣,另一端連接到生命線。使用門的圖 11 和 12 ,在圖 14 和 15 中可以被看到重構(gòu)。圖 15 的例圖有一個叫做getBalance的入口門,獲取參數(shù) accountNumber。因為是箭頭的線連接到圖的框架,而箭頭連接到生命線,所以 getBalance 消息是一個入口門。序列圖也有一個出囗門,返回balance變量。出口門同理可知,因為它是一個返回消息,連接從一個生命線到圖的框架,箭頭連接框架。

            圖 14: 圖 11 的重構(gòu),這次使用門

            圖 14: 圖 11 的重構(gòu),這次使用門

            圖 15: 圖 12 的重構(gòu),這次使用門

            圖 15: 圖 12 的重構(gòu),這次使用門

            組合碎片(跳轉(zhuǎn)和并行)
            在本文前面“基礎”的段落中呈現(xiàn)的,我介紹了“變體”,“選擇項”,和“循環(huán)”的組合碎片。這些三個組合碎片是大多數(shù)人將會使用最多的。然而,有二個其他的組合碎片,大量共享的人將會發(fā)現(xiàn)有用——跳轉(zhuǎn)和并行。

            跳轉(zhuǎn)
            跳轉(zhuǎn)組合碎片幾乎在每個方面都和選擇項組合碎片一致,除了兩個例外。首先,跳轉(zhuǎn)的框架namebox的文本“break”代替了“option”。其次, 當一個跳轉(zhuǎn)組合碎片的消息運行時,封閉的交互作用的其他消息將不會執(zhí)行,因為序列打破了封閉的交互。這樣,跳轉(zhuǎn)組合碎片非常象 C++ 或 Java 的編程語言中的break關(guān)鍵字。

            圖 16: 來自圖 8 的序列圖片段的重構(gòu),片段使用跳轉(zhuǎn)代替變體

            圖 16: 來自圖 8 的序列圖片段的重構(gòu),片段使用跳轉(zhuǎn)代替變體

            跳轉(zhuǎn)最常用來做模型異常處理。圖 16 是圖 8 的重構(gòu),但是這次圖16使用跳轉(zhuǎn)組合碎片,因為它把balance < amount的情況作為一個異常對待,而不是一個變體流。要閱讀圖 16,你從序列的左上角開始,向下讀。當序列到達返回值“balance”的時候,它檢查看看是否余額比金額更少。如果余額不少于金額,被傳遞的下一個消息是 addDebitTransaction 消息,而且序列正常繼續(xù)。然而,在余額比金額更少的情況下,然后序列進入跳轉(zhuǎn)組合碎片,它的消息被傳遞。一旦跳轉(zhuǎn)組合的消息的已經(jīng)被傳遞,序列不發(fā)送任何其它消息就退出(舉例來說,addDebitTransaction)。

            注意有關(guān)跳轉(zhuǎn)的一件重要的事是,它們只引起一個封閉交互的序列退出,不必完成圖中描述的序列。在這種情況下,跳轉(zhuǎn)組合是變體或者循環(huán)的一部分,然后只是變體或循環(huán)被退出。

            并行
            今天的現(xiàn)代計算機系統(tǒng)在復雜性和有時執(zhí)行并發(fā)任務方面不斷進步。當完成一個復雜任務需要的處理時間比希望的長的時候,一些系統(tǒng)采用并行處理進程的各部分。當創(chuàng)造一個序列圖,顯示并行處理活動的時候,需要使用并行組合碎片元件。

            并行組合碎片使用一個框架來畫,你把文本“par”放在框架的 namebox 中。然后你把框架的內(nèi)容段用虛線分為水平操作元。框架的每個操作元表示一個在并行運行的線程。

            圖 17: oven 是并行做兩個任務的對象實例

            圖 17: oven 是并行做兩個任務的對象實例

            圖 17 可能沒有舉例說明做并行活動的對象的最好的計算機系統(tǒng)實例,不過提供了一個容易理解的并行活動序列的例子。序列如這樣進行:hungryPerson 傳遞 cookFood 消息給oven 對象。當oven 對象接收那個消息時,它同時發(fā)送兩個消息(nukeFood 和 rotateFood)給它本身。這些消息都處理后,hungryPerson 對象從oven 對象返回 yummyFood 。

            總結(jié)
            序列圖是一個用來記錄系統(tǒng)需求,和整理系統(tǒng)設計的好圖。序列圖是如此好用的理由是,因為它按照交互發(fā)生的時間順序,顯示了系統(tǒng)中對象間的交互邏輯。

            參考

            • UML 2.0 Superstructure Final Adopted Specification (第8章部分) http://www.omg.org/cgi-bin/doc?ptc/2003-08-02
            • UML 2 Sequence Diagram Overview http://www.agilemodeling.com/artifacts/sequenceDiagram.htm
            • UML 2 Tutorial http://www.omg.org/news/meetings/workshops/UML%202003%20Manual/Tutorial7-Hogg.pdf

            腳注
            1 在完全建模系統(tǒng)中,對象(類的實例)也將會在系統(tǒng)的類圖中建模。

            2 當閱讀這個序列圖時,假定分析師登錄進入系統(tǒng)之內(nèi)。

            3 請注意,附著在不同的變體操作元上的、兩個或更多的約束條件式的確可能同時是真,但是實際最多只有一個操作元將會在運行時發(fā)生(那種情況下變體的“wins”沒有按照 UML 標準定義)。

            4 雖然操作元看起來非常象公路上的小路,但是我特別不叫它們小路。泳道是在活動圖上使用的 UML 符號。請參考The Rational Edge 早期關(guān)于 活動圖的文章。

            5 通常,附上約束的生命線是擁有包含在約束表達式中的變量的生命線。

            6 關(guān)于選擇項組合碎片,循環(huán)組合碎片不需要在它上放置一個約束條件。

            7 可能重用任何類型的序列圖(舉例來說,程序或業(yè)務)。我只是發(fā)現(xiàn)開發(fā)者更喜歡按功能分解他們的圖。

            參考資料

            • 您可以參閱本文在 developerWorks 全球站點上的 英文原文

             

            關(guān)于作者
            Donald BellDonald Bell是IBM全球服務的一個IT專家,在那兒他和IBM的客戶一起致力于設計和開發(fā)基于軟件解決方案的J2EE。
            posted on 2008-07-25 12:24 子彈のVISIONS 閱讀(2006) 評論(1)  編輯 收藏 引用 所屬分類: 1.x 臨時目錄

            Feedback

            # re: UML序列圖 2008-07-25 12:56 子彈

            UML's Sequence Diagram

            developerWorks
            Document options
            Set printer orientation to landscape mode

            Print this page

            Email this page

            E-mail this page

            Document options requiring JavaScript are not displayed


            Rate this page

            Help us improve this content


            Level: Introductory

            Donald Bell, IT Specialist, IBM

            16 Feb 2004

            from The Rational Edge: Part of a series of articles, .UML basics,. on the essential diagrams in the Unified Modeling Language, this article offers a detailed introduction to the sequence diagram. It also introduces several new notation elements in the recent UML 2.0 specification.

            Illustration


            It's February, and by now you've probably read about, or heard people talk about, making the change to UML 2.0--the new specification for UML that contains a number of improvements. Given the importance of the new spec, we are changing the basis of this article series, too, shifting our attention from OMG's UML 1.4 Specification to OMG's Adopted 2.0 Draft Specification of UML (a.k.a. UML 2). I hate to change emphasis from 1.4 to 2.0 in the middle of a series of articles, but the UML 2.0 Draft Specification is an important step forward, and I feel the need to spread the word.

            There were a couple of reasons that the OMG improved UML. The main reason was that they wanted UML models to be capable of delivering Model Driven Architecture (MDA), which meant that the UML had to function as a more model driven notation. Also, the UML 1.x notation set was at times difficult to apply to larger applications. Furthermore, the notation elements needed to be improved in order to make diagrams more readable. (For example, modeling logical flow in UML 1.x was complicated and at times impossible. Changes to the sequence diagram's notation set in UML 2 have made vast improvements in modeling logic in sequences.)

            Notice the wording in my statement above: "Adopted 2.0 Draft Specification of UML." It is true that the specification is still in draft status, but the key is that the Draft Specification has been adopted by OMG, a consortium that does not adopt new standards until they become pretty solid. There will be some changes to the specification before UML 2 is completely adopted, but these changes should be minimal. The main changes will be in the internals of UML--involving features typically used by software companies who implement UML tools.

            The main purpose of this article is to continue our focus on the essential UML diagrams; this month, we take a close look at the sequence diagram. Please note, again, that the examples provided below are based on the new UML 2 specification.

            The diagram's purpose

            The sequence diagram is used primarily to show the interactions between objects in the sequential order that those interactions occur. Much like the class diagram, developers typically think sequence diagrams were meant exclusively for them. However, an organization's business staff can find sequence diagrams useful to communicate how the business currently works by showing how various business objects interact. Besides documenting an organization's current affairs, a business-level sequence diagram can be used as a requirements document to communicate requirements for a future system implementation. During the requirements phase of a project, analysts can take use cases to the next level by providing a more formal level of refinement. When that occurs, use cases are often refined into one or more sequence diagrams.

            An organization's technical staff can find sequence diagrams useful in documenting how a future system should behave. During the design phase, architects and developers can use the diagram to force out the system's object interactions, thus fleshing out overall system design.

            One of the primary uses of sequence diagrams is in the transition from requirements expressed as use cases to the next and more formal level of refinement. Use cases are often refined into one or more sequence diagrams. In addition to their use in designing new systems, sequence diagrams can be used to document how objects in an existing (call it "legacy") system currently interact. This documentation is very useful when transitioning a system to another person or organization.



            Back to top


            The notation

            Since this is the first article in my UML diagram series that is based on UML 2, we need to first discuss an addition to the notation in UML 2 diagrams, namely a notation element called a frame. The frame element is used as a basis for many other diagram elements in UML 2, but the first place most people will encounter a frame element is as the graphical boundary of a diagram. A frame element provides a consistent place for a diagram's label, while providing a graphical boundary for the diagram. The frame element is optional in UML diagrams; as you can see in Figures 1 and 2, the diagram's label is placed in the top left corner in what I'll call the frame's "namebox," a sort of dog-eared rectangle, and the actual UML diagram is defined within the body of the larger enclosing rectangle.

            Figure 1: An empty UML 2 frame element

            Figure 1: An empty UML 2 frame element

            In addition to providing a visual border, the frame element also has an important functional use in diagrams depicting interactions, such as the sequence diagram. On sequence diagrams incoming and outgoing messages (a.k.a. interactions) for a sequence can be modeled by connecting the messages to the border of the frame element (as seen in Figure 2). This will be covered in more detail in the "Beyond the basics" section below.

            Figure 2: A sequence diagram that has incoming and outgoing messages.

            Figure 2: A sequence diagram that has incoming and outgoing messages

            Notice that in Figure 2 the diagram's label begins with the letters "sd," for Sequence Diagram. When using a frame element to enclose a diagram, the diagram's label needs to follow the format of:

            Diagram Type Diagram Name

            The UML specification provides specific text values for diagram types (e.g., sd = Sequence Diagram, activity = Activity Diagram, and use case = Use Case Diagram).



            Back to top


            The basics

            The main purpose of a sequence diagram is to define event sequences that result in some desired outcome. The focus is less on messages themselves and more on the order in which messages occur; nevertheless, most sequence diagrams will communicate what messages are sent between a system's objects as well as the order in which they occur. The diagram conveys this information along the horizontal and vertical dimensions: the vertical dimension shows, top down, the time sequence of messages/calls as they occur, and the horizontal dimension shows, left to right, the object instances that the messages are sent to.

            Lifelines

            When drawing a sequence diagram, lifeline notation elements are placed across the top of the diagram. Lifelines represent either roles or object instances that participate in the sequence being modeled. 1 Lifelines are drawn as a box with a dashed line descending from the center of the bottom edge (Figure 3). The lifeline's name is placed inside the box.

            Figure 3: An example of the Student class used in a lifeline whose instance name is freshman

            Figure 3: An example of the Student class used in a lifeline whose instance name is freshman

            The UML standard for naming a lifeline follows the format of:

            Instance Name : Class Name

            In the example shown in Figure 3, the lifeline represents an instance of the class Student, whose instance name is freshman. Note that, here, the lifeline name is underlined. When an underline is used, it means that the lifeline represents a specific instance of a class in a sequence diagram, and not a particular kind of instance (i.e., a role). In a future article we'll look at structure modeling. For now, just observe that sequence diagrams may include roles (such as buyer and seller) without specifying who plays those roles (such as Bill and Fred). This allows diagram reuse in different contexts. Simply put, instance names in sequence diagrams are underlined; roles names are not.

            Our example lifeline in Figure 3 is a named object, but not all lifelines represent named objects. Instead a lifeline can be used to represent an anonymous or unnamed instance. When modeling an unnamed instance on a sequence diagram, the lifeline's name follows the same pattern as a named instance; but instead of providing an instance name, that portion of the lifeline's name is left blank. Again referring to Figure 3, if the lifeline is representing an anonymous instance of the Student class, the lifeline would be: " Student." Also, because sequence diagrams are used during the design phase of projects, it is completely legitimate to have an object whose type is unspecified: for example, "freshman."

            Messages

            The first message of a sequence diagram always starts at the top and is typically located on the left side of the diagram for readability. Subsequent messages are then added to the diagram slightly lower then the previous message.

            To show an object (i.e., lifeline) sending a message to another object, you draw a line to the receiving object with a solid arrowhead (if a synchronous call operation) or with a stick arrowhead (if an asynchronous signal). The message/method name is placed above the arrowed line. The message that is being sent to the receiving object represents an operation/method that the receiving object's class implements. In the example in Figure 4, the analyst object makes a call to the system object which is an instance of the ReportingSystem class. The analyst object is calling the system object's getAvailableReports method. The system object then calls the getSecurityClearance method with the argument of userId on the secSystem object, which is of the class type SecuritySystem. 2

            Figure 4: An example of messages being sent between objects.

            Figure 4: An example of messages being sent between objects

            Besides just showing message calls on the sequence diagram, the Figure 4 diagram includes return messages. These return messages are optional; a return message is drawn as a dotted line with an open arrowhead back to the originating lifeline, and above this dotted line you place the return value from the operation. In Figure 4 the secSystem object returns userClearance to the system object when the getSecurityClearance method is called. The system object returns availableReports when the getAvailableReports method is called.

            Again, the return messages are an optional part of a sequence diagram. The use of return messages depends on the level of detail/abstraction that is being modeled. Return messages are useful if finer detail is required; otherwise, the invocation message is sufficient. I personally like to include return messages whenever a value will be returned, because I find the extra details make a sequence diagram easier to read.

            When modeling a sequence diagram, there will be times that an object will need to send a message to itself. When does an object call itself? A purist would argue that an object should never send a message to itself. However, modeling an object sending a message to itself can be useful in some cases. For example, Figure 5 is an improved version of Figure 4. The Figure 5 version shows the system object calling its determineAvailableReports method. By showing the system sending itself the message "determineAvailableReports," the model draws attention to the fact that this processing takes place in the system object.

            To draw an object calling itself, you draw a message as you would normally, but instead of connecting it to another object, you connect the message back to the object itself.

            Figure 5: This shows the system object making a calling its determineAvailableReports method.

            Figure 5: The system object calling its determineAvailableReports method

            The example messages in Figure 5 show synchronous messages; however, in sequence diagrams you can model asynchronous messages, too. An asynchronous message is drawn similar to a synchronous one, but the message's line is drawn with a stick arrowhead, as shown in Figure 6.

            Figure 6: A sequence diagram fragment that shows an asynchronous message being sent to instance2.

            Figure 6: A sequence diagram fragment showing an asynchronous message being sent to instance2

            Guards

            When modeling object interactions, there will be times when a condition must be met for a message to be sent to the object. Guards are used throughout UML diagrams to control flow. Here, I will discuss guards in both UML 1.x as well as UML 2.0. In UML 1.x, a guard could only be assigned to a single message. To draw a guard on a sequence diagram in UML 1.x, you placed the guard element above the message line being guarded and in front of the message name. Figure 7 shows a fragment of a sequence diagram with a guard on the message addStudent method.

            Figure 7: A segment of a UML 1.x sequence diagram in which the addStudent message has a guard.

            Figure 7: A segment of a UML 1.x sequence diagram in which the addStudent message has a guard

            In Figure 7, the guard is the text "[pastDueBalance = 0]." By having the guard on this message, the addStudent message will only be sent if the accounts receivable system returns a past due balance of zero. The notation of a guard is very simple; the format is:

            [Boolean Test]

            For example,

            [pastDueBalance = 0]

            Combined fragments (alternatives, options, and loops)

            In most sequence diagrams, however, the UML 1.x "in-line" guard is not sufficient to handle the logic required for a sequence being modeled. This lack of functionality was a problem in UML 1.x. UML 2 has addressed this problem by removing the "in-line" guard and adding a notation element called a Combined Fragment. A combined fragment is used to group sets of messages together to show conditional flow in a sequence diagram. The UML 2 specification identifies 11 interaction types for combined fragments. Three of the eleven will be covered here in "The Basics" section, two more types will be covered in the "Beyond The Basics" section, and the remaining six I will leave to be covered in another article. (Hey, this is an article, not a book. I want you to finish this piece in one day!)

            Alternatives

            Alternatives are used to designate a mutually exclusive choice between two or more message sequences. 3 Alternatives allow the modeling of the classic "if then else" logic (e.g., if I buy three items, then I get 20% off my purchase; else I get 10% off my purchase).

            As you will notice in Figure 8, an alternative combination fragment element is drawn using a frame. The word "alt" is placed inside the frame's namebox. The larger rectangle is then divided into what UML 2 calls operands. 4 Operands are separated by a dashed line. Each operand is given a guard to test against, and this guard is placed towards the top left section of the operand on top of a lifeline. 5 If an operand's guard equates to "true," then that operand is the operand to follow.

            Figure 8: A sequence diagram fragment that contains an alternative combination fragment

            Figure 8: A sequence diagram fragment that contains an alternative combination fragment

            As an example to show how an alternative combination fragment is read, Figure 8 shows the sequence starting at the top, with the bank object getting the check's amount and the account's balance. At this point in the sequence the alternative combination fragment takes over. Because of the guard "[balance >= amount]," if the account's balance is greater than or equal to the amount, then the sequence continues with the bank object sending the addDebitTransaction and storePhotoOfCheck messages to the account object. However, if the balance is not greater than or equal to the amount, then the sequence proceeds with the bank object sending the addInsuffientFundFee and noteReturnedCheck message to the account object and the returnCheck message to itself. The second sequence is called when the balance is not greater than or equal to the amount because of the "[else]" guard. In alternative combination fragments, the "[else]" guard is not required; and if an operand does not have an explicit guard on it, then the "[else]" guard is to be assumed.

            Alternative combination fragments are not limited to simple "if then else" tests. There can be as many alternative paths as are needed. If more alternatives are needed, all you must do is add an operand to the rectangle with that sequence's guard and messages.

            Option

            The option combination fragment is used to model a sequence that, given a certain condition, will occur; otherwise, the sequence does not occur. An option is used to model a simple "if then" statement (i.e., if there are fewer than five donuts on the shelf, then make two dozen more donuts).

            The option combination fragment notation is similar to the alternation combination fragment, except that it only has one operand and there never can be an "else" guard (it just does not make sense here). To draw an option combination you draw a frame. The text "opt" is placed inside the frame's namebox, and in the frame's content area the option's guard is placed towards the top left corner on top of a lifeline. Then the option's sequence of messages is placed in the remainder of the frame's content area. These elements are illustrated in Figure 9.

            Figure 9: A sequence diagram fragment which includes an option combination fragment.

            Figure 9: A sequence diagram fragment that includes an option combination fragment

            Reading an option combination fragment is easy. Figure 9 is a reworking of the sequence diagram fragment in Figure 7, but this time it uses an option combination fragment because more messages need to be sent if the student's past due balance is equal to zero. According to the sequence diagram in Figure 9, if a student's past due balance equals zero, then the addStudent, getCostOfClass, and chargeForClass messages are sent. If the student's past due balance does not equal zero, then the sequence skips sending any of the messages in the option combination fragment.

            The example Figure 9 sequence diagram fragment includes a guard for the option; however, the guard is not a required element. In high-level, abstract sequence diagrams you might not want to specify the condition of the option. You may simply want to indicate that the fragment is optional.

            Loops

            Occasionally you will need to model a repetitive sequence. In UML 2, modeling a repeating sequence has been improved with the addition of the loop combination fragment.

            The loop combination fragment is very similar in appearance to the option combination fragment. You draw a frame, and in the frame's namebox the text "loop" is placed. Inside the frame's content area the loop's guard 6 is placed towards the top left corner, on top of a lifeline. Then the loop's sequence of messages is placed in the remainder of the frame's content area. In a loop, a guard can have two special conditions tested against in addition to the standard Boolean test. The special guard conditions are minimum iterations written as "minint = [the number]" (e.g., "minint = 1") and maximum iterations written as "maxint = [the number]" (e.g., "maxint = 5"). With a minimum iterations guard, the loop must execute at least the number of times indicated, whereas with a maximum iterations guard the number of loop executions cannot exceed the number.

            Figure 10: An example sequence diagram with a loop combination fragment.

            Figure 10: An example sequence diagram with a loop combination fragment
            click to enlarge

            The loop shown in Figure 10 executes until the reportsEnu object's hasAnotherReport message returns false. The loop in this sequence diagram uses a Boolean test to verify if the loop sequence should be run. To read this diagram, you start at the top, as normal. When you get to the loop combination fragment a test is done to see if the value hasAnotherReport equals true. If the hasAnotherReport value equals true, then the sequence goes into the loop fragment. You can then follow the messages in the loop as you would normally in a sequence diagram



            Back to top


            Beyond the basics

            I've covered the basics of the sequence diagram, which should allow you to model most of the interactions that will take place in a common system. The following section will cover more advanced notation elements that can be used in a sequence diagram.

            Referencing another sequence diagram

            When doing sequence diagrams, developers love to reuse existing sequence diagrams in their diagram's sequences. 7 Starting in UML 2, the "Interaction Occurrence" element was introduced. The addition of interaction occurrences is arguably the most important innovation in UML 2 interactions modeling. Interaction occurrences add the ability to compose primitive sequence diagrams into complex sequence diagrams. With these you can combine (reuse) the simpler sequences to produce more complex sequences. This means that you can abstract out a complete, and possibly complex, sequence as a single conceptual unit.

            An interaction occurrence element is drawn using a frame. The text "ref" is placed inside the frame's namebox, and the name of the sequence diagram being referenced is placed inside the frame's content area along with any parameters to the sequence diagram. The notation of the referenced sequence diagram's name follows the pattern of:

            sequence diagram name[(arguments)] [: return value]

            Two examples:

            1. Retrieve Borrower Credit Report(ssn) : borrowerCreditReport

            or

            2. Process Credit Card(name, number, expirationDate, amount : 100)

            In example 1, the syntax calls the sequence diagram called Retrieve Borrower Credit Report and passes it the parameter ssn. The Retreive Borrower Credit Report sequence returns the variable borrowerCreditReport.

            In example 2, the syntax calls the sequence diagram called Process Credit Card and passes it the parameters of name, number, expiration date, and amount. However, in example 2 the amount parameter will be a value of 100. And since example 2 does not have a return value labeled, the sequence does not return a value (presumably, the sequence being modeled does not need the return value).

            Figure 11: A sequence diagram that references two different sequence diagrams.

            Figure 11: A sequence diagram that references two different sequence diagrams

            Figure 11 shows a sequence diagram that references the sequence diagrams "Balance Lookup" and "Debit Account." The sequence starts at the top left, with the customer sending a message to the teller object. The teller object sends a message to the theirBank object. At that point, the Balance Lookup sequence diagram is called, with the accountNumber passed as a parameter. The Balance Lookup sequence diagram returns the balance variable. Then the option combination fragment's guard condition is checked to verify the balance is greater then the amount variable. In cases where the balance is greater than the amount, the Debit Account sequence diagram is called, passing it the accountNumber and the amount as parameters. After that sequence is complete, the withdrawCash message returns cash to the customer.

            It is important to notice in Figure 11 that the lifeline of theirBank is hidden by the interaction occurrence Balance Lookup. Because the interaction occurrence hides the lifeline, that means that the theirBank lifeline is referenced in the "Balance Lookup" sequence diagram. In addition to hiding the lifeline in the interaction occurrence, UML 2 also specifies that the lifeline must have the same theirBank in its own "Balance Lookup" sequence.

            There will be times when you model sequence diagrams that an interaction occurrence will overlap lifelines that are not referenced in the interaction occurrence. In such cases the lifeline is shown as a normal lifeline and is not hidden by the overlapping interaction occurrence.

            In Figure 11, the sequence references the "Balance Lookup" sequence diagram. The "Balance Lookup" sequence diagram is shown in Figure 12. Because the example sequence has parameters and a return value, its label --located in the diagram's namebox--follows a specific pattern:

            Diagram Type Diagram Name [(Parameter Type : Parameter Name)] :
                                    

            [: Return Value Type]

            Two examples:

            1. SD Balance Lookup(Integer : accountNumber) : Real

            or

            2. SD Available Reports(Financial Analyst : analyst) : Reports

            Figure 12 illustrates example 1, in which the Balance Lookup sequence uses parameter accountNumber as a variable in the sequence, and the sequence diagram shows a Real object being returned. In cases such as this, where the sequence returns an object, the object being returned is given the instance name of the sequence diagram.

            Figure 12: A sequence diagram that takes the parameter of accountNumber and returns a Real object.

            Figure 12: A sequence diagram that takes the parameter of accountNumber and returns a Real object

            Figure 13 illustrates example 2, in which a sequence takes a parameter and returns an object. However, in Figure 13 the parameter is used in the sequence's interaction.

            Figure 13: A sequence diagram that uses its parameter in its interaction and returns a Reports object.

            Figure 13: A sequence diagram that uses its parameter in its interaction and returns a Reports object
            Click to enlarge

            Gates

            The previous section showed how to reference another sequence diagram by passing information through parameters and return values. However, there is another way to pass information between sequence diagrams. Gates can be an easy way to model the passing of information between a sequence diagram and its context. A gate is merely a message that is illustrated with one end connected to the sequence diagram's frame's edge and the other end connected to a lifeline. A reworking of Figures 11 and 12 using gates can be seen in Figures 14 and 15. The example diagram in Figure 15 has an entry gate called getBalance that takes the parameter of accountNumber. The getBalance message is an entry gate, because it is the arrowed line that is connected to the diagram's frame with the arrowhead connected to a lifeline. The sequence diagram also has an exit gate that returns the balance variable. The exit gate is known, because it's a return message that is connected from a lifeline to the diagram's frame with the arrowhead connected to the frame.

            Figure 14: A reworking of Figure 11, but this time using gates.

            Figure 14: A reworking of Figure 11, using gates this time

            Figure 15: A reworking of Figure 12, but this time using gates.

            Figure 15: A reworking of Figure 12, using gates this time

            Combined fragments (break and parallel)

            In the "basics" section presented earlier in this paper, I covered the combined fragments known as "alternative," "option," and "loop." These three combined fragments are the ones most people will use the most. However, there are two other combined fragments that a large share of people will find useful – break and parallel.

            Break

            The break combined fragment is almost identical in every way to the option combined fragment, with two exceptions. First, a break's frame has a namebox with the text "break" instead of "option." Second, when a break combined fragment's message is to be executed, the enclosing interaction's remainder messages will not be executed because the sequence breaks out of the enclosing interaction. In this way the break combined fragment is much like the break keyword in a programming language like C++ or Java.

            Figure 16: A reworking of the sequence diagram fragment from Figure 8, but this time the fragment uses a break instead of an alternative.

            Figure 16: A reworking of the sequence diagram fragment from Figure 8, with the fragment using a break instead of an alternative

            Breaks are most commonly used to model exception handling. Figure 16 is a reworking of Figure 8, but this time Figure 16 uses a break combination fragment because it treats the balance < amount condition as an exception instead of as an alternative flow. To read Figure 16, you start at the top left corner of the sequence and read down. When the sequence gets to the return value "balance," it checks to see if the balance is less than the amount. If the balance is not less than the amount, the next message sent is the addDebitTransaction message, and the sequence continues as normal. However, in cases where the balance is less than the amount, then the sequence enters the break combination fragment and its messages are sent. Once all the messages in the break combination have been sent, the sequence exits without sending any of the remaining messages (e.g., addDebitTransaction).

            An important thing to note about breaks is that they only cause the exiting of an enclosing interaction's sequence and not necessarily the complete sequence depicted in the diagram. In cases where a break combination is part of an alternative or a loop, then only the alternative or loop is exited.

            Parallel

            Today's modern computer systems are advancing in complexity and at times perform concurrent tasks. When the processing time required to complete portions of a complex task is longer than desired, some systems handle parts of the processing in parallel. The parallel combination fragment element needs to be used when creating a sequence diagram that shows parallel processing activities.

            The parallel combination fragment is drawn using a frame, and you place the text "par" in the frame's namebox. You then break up the frame's content section into horizontal operands separated by a dashed line. Each operand in the frame represents a thread of execution done in parallel.

            Figure 17: A microwave is an example of an object that does two tasks in parallel.

            Figure 17: A microwave is an example of an object that does two tasks in parallel

            While Figure 17 may not illustrate the best computer system example of an object doing activities in parallel, it offers an easy-to-understand example of a sequence with parallel activities. The sequence goes like this: A hungryPerson sends the cookFood message to the oven object. When the oven object receives that message, it sends two messages to itself at the same time (nukeFood and rotateFood). After both of these messages are done, the hungryPerson object is returned yummyFood from the oven object.



            Back to top


            Conclusion

            The sequence diagram is a good diagram to use to document a system's requirements and to flush out a system's design. The reason the sequence diagram is so useful is because it shows the interaction logic between the objects in the system in the time order that the interactions take place.



            Back to top


            References



            Back to top


            Notes

            1 In fully modeled systems the objects (instances of classes) will also be modeled on a system's class diagram.

            2 When reading this sequence diagram, assume that the analyst has already logged into the system.

            3 Please note that it is indeed possible for two or more guard conditions attached to different alternative operands to be true at the same time, but at most only one operand will actually occur at run time (which alternative "wins" in such cases is not defined by the UML standard).

            4 Although operands look a lot like lanes on a highway, I specifically did not call them lanes. Swim lanes are a UML notation used on activity diagrams. Please refer to The Rational Edge's earlier article about Activity Diagrams.

            5 Usually, the lifeline to which the guard is attached is the lifeline that owns the variable that is included in the guard expression.

            6 As with the option combination fragment, the loop combination fragment does not require that a guard condition be placed on it.

            7 It's possible to reuse a sequence diagram of any type (e.g. programming or business). I just find that developers like to functionally break down their diagrams more.



            About the author

            Donald Bell

            Donald Bell is an IT Specialist in IBM Global Services, where he works with IBM's customers to design and develop J2EE based software solutions.


              回復  更多評論
              

            久久久久久人妻无码| 2021久久国自产拍精品| 久久亚洲AV无码精品色午夜| 99精品久久久久久久婷婷| 久久无码人妻一区二区三区| 国产精品一区二区久久精品无码| 亚洲精品午夜国产va久久| 99久久久精品免费观看国产| 青青久久精品国产免费看 | 国产精品亚洲综合专区片高清久久久| 久久精品视屏| 国产精品九九九久久九九| 2021国产精品午夜久久| 狠狠人妻久久久久久综合| 欧美噜噜久久久XXX| 久久久久九九精品影院| 国产午夜久久影院| 国产精品美女久久久久久2018| 久久婷婷是五月综合色狠狠| 国产精品gz久久久| 国产成人精品久久免费动漫| 中文字幕久久精品无码| 久久经典免费视频| 无码任你躁久久久久久| 久久精品人妻一区二区三区| 91精品国产91久久久久久青草| 久久人人爽人人爽人人AV东京热| 久久性生大片免费观看性| 一级做a爱片久久毛片| 国产成人久久激情91| 99久久99这里只有免费费精品| 国产A三级久久精品| 怡红院日本一道日本久久| 久久婷婷综合中文字幕| 久久精品成人免费网站| 久久精品国产91久久综合麻豆自制| 天天躁日日躁狠狠久久| 久久国产热精品波多野结衣AV| 久久久久久毛片免费播放| 91精品国产高清久久久久久io| 蜜臀av性久久久久蜜臀aⅴ麻豆|