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操作以多态的形式调用.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值