UML實例--HELL_WORLD(五)



Mechanisms

機制

The hardest part of mastering a library as rich as Javas is learning how its parts work together. For example, how does HelloWorldspaint operation get invoked? What operations must you use if you want to change the behavior of this applet, such as making it print the string in a different color? To answer these and other questions, you must have a conceptual model of the way these classes work together dynamically.

掌握象Java這樣擁有豐富庫的最難的部分是學習如何讓它的各部分一起工作.例如,如何得到調用HelloWorldpaint操作?如果需要改變這個小應用的行爲,必須使用什麼操作?如用不同的顏色打印這個字符串?想回答這些或是其它的問題,你必須有能動態地讓這些類一起工作的模型概念.

 

Studying the Java library reveals thatHelloWorldspaint operation is inherited from . This still begs the question of how this operation is invoked. The answer is thatpaint is called as part of running the thread that encloses the applet, as Figure 3-5 illustrates.

 

研究Java庫可顯示HelloWorldpaint操作繼承自哪裏.這仍然迴避了這個操作如何被調用的問題.答案是paint被作爲附着在小應用程序裏的運行線程的一部分調用,如圖3-5所顯示的那樣.

 

 

This figure shows the collaboration of several objects, including one instance of the classHelloWorld . The other objects are a part of the Java environment, so, for the most part, they live in the background of the applets you create. This shows a collaboration among objects that can be applied many times. Each column shows a role in the collaboration, that is, a part that can be played by a different object in each execution. In the UML, roles are represented just like classes, except they have both role names and types. The middle two roles in this diagram are anonymous, because their types are enough to identify them within the collaboration (but the colon and the absence of an underline mark them as roles). The initialThread is calledroot , and theHelloWorld role has the nametarget known by theComponentPeer role.

這個圖顯示的是幾個對象之間的交互,包含類HelloWorld的一個實例.其它對象是Java環境的一部分,所以,就絕大部分而言,它們存在於你創建的小應用程序的背景裏.這裏顯示的對象之間的交互可多次應用.每列顯示交互中的一個角色,也就是在每次執行時被不同對象調用的那部分.在UML中,除了名稱和類型,角色的表達方式與類一樣.在這個圖中,中間的兩個角色都是匿名的,因爲在這個交互中,它們的類型已足夠識別它們(但是冒號和沒有下劃線作爲它們是角色的標誌).開始的Thread被命名爲root,HelloWorld這個角色有個名稱叫target,它知道ComponentPeer角色的作用.

 

You can model the ordering of events using a sequence diagram, as in Figure 3-5. Here, the sequence begins by running theThread object, which in turn calls theToolkitsrun operation. TheToolkit object then calls one of its own operation(callbackLoop), which then calls the ComponentPeershandleExpose operation. TheComponentPeer object then calls its targetspaint operation. TheComponentPeer object assumes that its target is aComponent , but in this case the target is actually a child ofComponent (namely,HelloWorld), soHelloWorldspaint operation is dispatched polymorphically.

你能使用序列圖給事件的順序建模,就像圖3-5顯示的那樣.圖3-5顯示的序列開始於運行着的Thread對象,它調用Toolkit的run操作.Toolkit對象然後調用它自己擁有的操作中的一個(callbackLoop),再調用ComponentPeerhandleExpose操作.ComponentPeer對象然後調用它的目標paint操作.ComponentPeer假設它的目標是一個Component,但是在這個案例中,目標實際上是繼承自Component的一個子類(也就是HelloWorld),所以HelloWorldpaint操作以多態的形式調用.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章