Practical UML: A Hands-On Introduction for Developers/UML实践----用例图、顺序图、状态图、类图、包图、协作图

Abstract: This tutorial provides a quick introduction to the Unified Modeling Language™

The heart of object-oriented problem solving is the construction of a model. The model abstracts the essential details of the underlying problem from its usually complicated real world. Several modeling tools are wrapped under the heading of the UML™, which stands for Unified Modeling Language™. The purpose of this course is to present important highlights of the UML.

At the center of the UML are its nine kinds of modeling diagrams, which we describe here.

 

Some of the sections of this course contain links to pages with more detailed information. And every section has short questions. Use them to test your understanding of the section topic.

 

Why is UML important?

Let's look at this question from the point of view of the construction trade. Architects design buildings. Builders use the designs to create buildings. The more complicated the building, the more critical the communication between architect and builder. Blueprints are the standard graphical language that both architects and builders must learn as part of their trade.

Writing software is not unlike constructing a building. The more complicated the underlying system, the more critical the communication among everyone involved in creating and deploying the software. In the past decade, the UML has emerged as the software blueprint language for analysts, designers, and programmers alike. It is now part of the software trade. The UML gives everyone from business analyst to designer to programmer a common vocabulary to talk about software design.

The UML is applicable to object-oriented problem solving. Anyone interested in learning UML must be familiar with the underlying tenet of object-oriented problem solving -- it all begins with the construction of a model. A model is an abstraction of the underlying problem. The domain is the actual world from which the problem comes.

Models consist of objects that interact by sending each other messages. Think of an object as "alive." Objects have things they know (attributes) and things they can do (behaviors or operations). The values of an object's attributes determine its state.

Classes are the "blueprints" for objects. A class wraps attributes (data) and behaviors (methods or functions) into a single distinct entity. Objects are instances of classes.

 

Use case diagrams

Use case diagrams describe what a system does from the standpoint of an external observer. The emphasis is on what a system does rather than how.

Use case diagrams are closely connected to scenarios. A scenario is an example of what happens when someone interacts with the system. Here is a scenario for a medical clinic.

"A patient calls the clinic to make an appointment for a yearly checkup. The receptionist finds the nearest empty time slot in the appointment book and schedules the appointment for that time slot. "

A use case is a summary of scenarios for a single task or goal. An actor is who or what initiates the events involved in that task. Actors are simply roles that people or objects play. The picture below is a Make Appointment use case for the medical clinic. The actor is a Patient. The connection between actor and use case is a communication association (or communication for short).


Actors are stick figures. Use cases are ovals. Communications are lines that link actors to use cases.

A use case diagram is a collection of actors, use cases, and their communications. We've put Make Appointment as part of a diagram with four actors and four use cases. Notice that a single use case can have multiple actors.


Use case diagrams are helpful in three areas.

  • determining features (requirements). New use cases often generate new requirements as the system is analyzed and the design takes shape.
  • communicating with clients. Their notational simplicity makes use case diagrams a good way for developers to communicate with clients.
  • generating test cases. The collection of scenarios for a use case may suggest a suite of test cases for those scenarios.


 

Class diagrams

A Class diagram gives an overview of a system by showing its classes and the relationships among them. Class diagrams are static -- they display what interacts but not what happens when they do interact.

The class diagram below models a customer order from a retail catalog. The central class is the Order. Associated with it are the Customer making the purchase and the Payment. A Payment is one of three kinds: Cash, Check, or Credit. The order contains OrderDetails (line items), each with its associated Item.


UML class notation is a rectangle divided into three parts: class name, attributes, and operations. Names of abstract classes, such as Payment, are in italics. Relationships between classes are the connecting links.

Our class diagram has three kinds of relationships.

  • association -- a relationship between instances of the two classes. There is an association between two classes if an instance of one class must know about the other in order to perform its work. In a diagram, an association is a link connecting two classes.
  • aggregation -- an association in which one class belongs to a collection. An aggregation has a diamond end pointing to the part containing the whole. In our diagram, Order has a collection of OrderDetails.
  • generalization -- an inheritance link indicating one class is a superclass of the other. A generalization has a triangle pointing to the superclass. Payment is a superclass of Cash, Check, and Credit.

An association has two ends. An end may have a role name to clarify the nature of the association. For example, an OrderDetail is a line item of each Order.

A navigability arrow on an association shows which direction the association can be traversed or queried. An OrderDetail can be queried about its Item, but not the other way around. The arrow also lets you know who "owns" the association's implementation; in this case, OrderDetail has an Item. Associations with no navigability arrows are bi-directional.

The multiplicity of an association end is the number of possible instances of the class associated with a single instance of the other end. Multiplicities are single numbers or ranges of numbers. In our example, there can be only one Customer for each Order, but a Customer can have any number of Orders.

This table gives the most common multiplicities.

Multiplicities

Meaning

0..1

zero or one instance. The notation n . . m indicates n to m instances.

0..*  or  *

no limit on the number of instances (including none).

1

exactly one instance

1..*

at least one instance

Every class diagram has classes, associations, and multiplicities. Navigability and roles are optional items placed in a diagram to provide clarity.


 

Packages and object diagrams

To simplify complex class diagrams, you can group classes into packages. A package is a collection of logically related UML elements. The diagram below is a business model in which the classes are grouped into packages.


Packages appear as rectangles with small tabs at the top. The package name is on the tab or inside the rectangle. The dotted arrows are dependencies. One package depends on another if changes in the other could possibly force changes in the first.

Object diagrams show instances instead of classes. They are useful for explaining small pieces with complicated relationships, especially recursive relationships.

This small class diagram shows that a university Department can contain lots of other Departments.


The object diagram below instantiates the class diagram, replacing it by a concrete example.


Each rectangle in the object diagram corresponds to a single instance. Instance names are underlined in UML diagrams. Class or instance names may be omitted from object diagrams as long as the diagram meaning is still clear.


 

Sequence diagrams

Class and object diagrams are static model views. Interaction diagrams are dynamic. They describe how objects collaborate.

A sequence diagram is an interaction diagram that details how operations are carried out -- what messages are sent and when. Sequence diagrams are organized according to time. The time progresses as you go down the page. The objects involved in the operation are listed from left to right according to when they take part in the message sequence.

Below is a sequence diagram for making a hotel reservation. The object initiating the sequence of messages is a Reservation window.


The Reservation window sends a makeReservation() message to a HotelChain. The HotelChain then sends a makeReservation() message to a Hotel. If the Hotel has available rooms, then it makes a Reservation and a Confirmation.

Each vertical dotted line is a lifeline, representing the time that an object exists. Each arrow is a message call. An arrow goes from the sender to the top of the activation bar of the message on the receiver's lifeline. The activation bar represents the duration of execution of the message.

In our diagram, the Hotel issues a self call to determine if a room is available. If so, then the Hotel creates a Reservation and a Confirmation. The asterisk on the self call means iteration (to make sure there is available room for each day of the stay in the hotel). The expression in square brackets, [ ], is a condition.

The diagram has a clarifying note, which is text inside a dog-eared rectangle. Notes can be put into any kind of UML diagram.


 

Collaboration diagrams

Collaboration diagrams are also interaction diagrams. They convey the same information as sequence diagrams, but they focus on object roles instead of the times that messages are sent. In a sequence diagram, object roles are the vertices and messages are the connecting links.

The object-role rectangles are labeled with either class or object names (or both). Class names are preceded by colons ( : ).

Each message in a collaboration diagram has a sequence number. The top-level message is numbered 1. Messages at the same level (sent during the same call) have the same decimal prefix but suffixes of 1, 2, etc. according to when they occur.


 

Statechart diagrams

Objects have behaviors and state. The state of an object depends on its current activity or condition. A statechart diagram shows the possible states of the object and the transitions that cause a change in state.

Our example diagram models the login part of an online banking system. Logging in consists of entering a valid social security number and personal id number, then submitting the information for validation.

Logging in can be factored into four non-overlapping states: Getting SSN, Getting PIN, Validating, and Rejecting. From each state comes a complete set of transitions that determine the subsequent state.


States are rounded rectangles. Transitions are arrows from one state to another. Events or conditions that trigger transitions are written beside the arrows. Our diagram has two self-transition, one on Getting SSN and another on Getting PIN.

The initial state (black circle) is a dummy to start the action. Final states are also dummy states that terminate the action.

The action that occurs as a result of an event or condition is expressed in the form /action. While in its Validating state, the object does not wait for an outside event to trigger a transition. Instead, it performs an activity. The result of that activity determines its subsequent state.


 

Activity diagrams

An activity diagram is essentially a fancy flowchart. Activity diagrams and statechart diagrams are related. While a statechart diagram focuses attention on an object undergoing a process (or on a process as an object), an activity diagram focuses on the flow of activities involved in a single process. The activity diagram shows the how those activities depend on one another.

For our example, we used the following process.

"Withdraw money from a bank account through an ATM."

The three involved classes (people, etc.) of the activity are Customer, ATM, and Bank. The process begins at the black start circle at the top and ends at the concentric white/black stop circles at the bottom. The activities are rounded rectangles.


Activity diagrams can be divided into object swimlanes that determine which object is responsible for which activity. A single transition comes out of each activity, connecting it to the next activity.

A transition may branch into two or more mutually exclusive transitions. Guard expressions (inside [ ]) label the transitions coming out of a branch. A branch and its subsequent merge marking the end of the branch appear in the diagram as hollow diamonds.

A transition may fork into two or more parallel activities. The fork and the subsequent join of the threads coming out of the fork appear in the diagram as solid bars.

 

Component and deployment diagrams

A component is a code module. Component diagrams are physical analogs of class diagram. Deployment diagrams show the physical configurations of software and hardware.

The following deployment diagram shows the relationships among software and hardware components involved in real estate transactions.


The physical hardware is made up of nodes. Each component belongs on a node. Components are shown as rectangles with two tabs at the upper left.

 

UML Tools and Modeling Tools

Creating and modifying UML diagrams can be labor and time intensive. The UML modeling tools from Embarcadero Technologies make it easy to create diagrams like the ones in this article plus they provide a great amount of other functionality including model and code synchronization.

Embarcadero Delphi is the fastest way to build native Windows applications. The Professional edition includes UML code visualization. The Enterprise edition includes modeling with two way synchronization between model and code. The Architect edition includes the ability to create language-neutral UML modeling projects. Learn more about Delphi or download a trial edition of Delphi and try it for yourself.

Similar UML modeling functionality is available for C++ in C++Builder, for Java in JBuilder and for Windows in Embarcadero RAD Studio. Database modeling and business modeling are available in Embarcadero ER/Studio and ER/Studio Business Architect.

 

For the latest up-to-date techniques in the Unified Modeling Language and Agile Software Development Processes, and for all of the latest information on how to deliver better software faster, visit The Embarcadero Developer Network

 转载至:http://edn.embarcadero.com/article/31863

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

中文版:

   面向对象的问题的处理的关键是建模问题。建模可以把在复杂世界的许多重要的细节给抽象出。许多建模工具封装了UML(也就是Unified Modeling Language™),这篇课程的目的是展示出UML的精彩之处。

UML中有九种建模的图标,即:

l         用例图

l         类图

l         对象图

l         顺序图

l         协作图

l         状态图

l         活动图

l         组件图

l         配置图

本课程中的某些部分包含了这些图的细节信息的页面链接。而且每个部分都有一个小问题,测试一下你对这个部分的理解。

―――――――――――――――――――――――――――――――――――――――

为什么UML很重要?

为了回答这个问题,我们看看建筑行业。设计师设计出房子。施工人员使用这个设计来建造房子。建筑越复杂,设计师和施工人员之间的交流就越重要。蓝图就成为了这个行业中的设计师和施工人员的必修课。

写软件就好像建造建筑物一样。系统越复杂,参与编写与配置软件的人员之间的交流也就越重要。在过去十年里UML就成为分析师,设计师和程序员之间的“建筑蓝图”。现在它已经成为了软件行业的一部分了。UML提供了分析师,设计师和程序员之间在软件设计时的通用语言。

UML被应用到面向对象的问题的解决上。想要学习UML必须熟悉面向对象解决问题的根本原则――都是从模型的建造开始的。一个模型model就是根本问题的抽象。domain就是问题所处的真实世界。

模型是由对象objects组成的,它们之间通过相互发送消息messages来相互作用的。记住把一个对象想象成“活着的”。对象有他们知道的事(属性attributes)和他们可以做的事(行为或操作behaviors or operations)。对象的属性的值决定了它的状态state

Classes是对象的“蓝图”。一个类在一个单独的实体中封装了属性(数据)和行为(方法或函数)。对象是类的实例instances

――――――――――――――――――――――――――――――――――――――

用例图

用例图Use case diagrams描述了作为一个外部的观察者的视角对系统的印象。强调这个系统是什么而不是这个系统怎么工作。

用例图与情节紧紧相关的。情节scenario是指当某个人与系统进行互动时发生的情况。下面是一个医院门诊部的情节。

“一个病人打电话给门诊部预约一年一次的身体检查。接待员找出在预约记录本上找出最近的没有预约过的时间,并记上那个时间的预约记录。”

用例Use case是为了完成一个工作或者达到一个目的的一系列情节的总和。角色actor是发动与这个工作有关的事件的人或者事情。角色简单的扮演着人或者对象的作用。下面的图是一个门诊部Make Appointment用例。角色是病人。角色与用例的联系是通讯联系communication association(或简称通讯communication

 

 


角色是人状的图标,用例是一个椭圆,通讯是连接角色和用例的线。

一个用例图是角色,用例,和它们之间的联系的集合。我们已经把Make Appointment作为一个含有四个角色和四个用例的图的一部分。注意一个单独的用例可以有多个角色。

 

 


    用例图在三个领域很有作用。

l         决定特征(需求)。当系统已经分析好并且设计成型时,新的用例产生新的需求

l         客户通讯。使用用例图很容易表示开发者与客户之间的联系。

l         产生测试用例。一个用例的情节可能产生这些情节的一批测试用例。

 

类图

类图Class diagram通过显示出系统的类以及这些类之间的关系来表示系统。类图是静态的-它们显示出什么可以产生影响但不会告诉你什么时候产生影响。

下面是一个顾客从零售商处预定商品的模型的类图。中心的类是Order。连接它的是购买货物的CustomerPaymentPayment有三种形式:CashCheck,或者Credit。订单包括OrderDetails(line item),每个这种类都连着Item

 

UML类的符号是一个被划分成三块的方框:类名,属性,和操作。抽象类的名字,像Payment是斜体的。类之间的关系是连接线。

类图有三种关系。

l         关联association-表示两种类的实例间的关系。如果一个类的实例必须要用另一个类的实例才能完成工作时就要用关联。在图中,关联用两个类之间的连线表示。

l         聚合aggregation-当一个类属于一个容器是的一种特殊关系。聚合用一个带菱形的连线,菱形指向具有整体性质的类。在我们的图里,OrderOrderDetails的容器。

l         泛化generalization-一个指向以其他类作为超类的继承连线。泛化关系用一个三角形指向超类。PaymentCashCheckCredit的超类。

一个关联有两个尾端。每个尾端可以有一个角色名role name来说明关联的作用。比如,一个OrderDetail实例是一个Order实例的项目。

关联上的方向性navigability箭头表示该关联传递或查询的方向。OrderDetail类可以查询他的Item,但不可以反过来查询。箭头方向同样可以告诉你哪个类拥有这个关联的实现;也就是,OrderDetail拥有Item。没有方向性的箭头的关联是双向。

关联尾端的数字表示该关联另一边的一个实例可以对应的数字端的实例的格数,通过这种方式表达关联的多样性multiplicity。多样性的数字可以是一个单独的数字或者是一个数字的范围。在例子中,每个Order只有一个Customer,但一个Customer可以有任意多个Order

下面这个表给出了最普遍的多样性示例。

多样性

意义

0..1

01个实例. n..m符号表示有nm个实例

0..* or *

没有实例格数的限制(包括没有).

1

只有一个实例

1..*

最少一个实例

每个类图包括类,关联和多样性表示。方向性和角色是为了使图示得更清楚时可选的项目。

包和对象图

为了简单地表示出复杂的类图,可以把类组合成packages。一个包是UML上有逻辑关系的元件的集合。下面这个图是是一个把类组合成包的一个商业模型。

 

dependencies关系。如果另一个的包B改变可能会导致一个包A改变,则包A依赖包B。


包是用一个在上方带有小标签的矩形表示的。包名写在标签上或者在矩形里面。点化线箭头表示依赖

对象图Object diagrams用来表示类的实例。他们在解释复杂关系的细小问题时(特别是递归关系时)很有用。

这个类图示一个大学的Department可以包括其他很多的Departments

 

 


   这个对象图示上面类图的实例。用了很多具体的例子。

 

UML中实例名带有下划线。只要意思清楚,类或实例名可以在对象图中被省略。


   每个类图的矩形对应了一个单独的实例。在

协作图

协作图也是互动的图表。他们像序列图一样也传递相同的信息,但他们不关心什么时候消息被传递,只关心对象的角色。在序列图中,对象的角色放在上面而消息则是连接线。

 

 


   对象角色矩形上标有类或对象名(或者都有)。类名前面有个冒号(:)。

协作图的每个消息都有一个序列号。顶层消息的数字是1。同一个等级的消息(也就是同一个调用中的消息)有同样的数字前缀,再根据他们出现的顺序增加一个后缀1,2等等。

状态图

对象拥有行为和状态。对象的状态是由对象当前的行动和条件决定的。状态图statechart diagram显示出了对象可能的状态以及由状态改变而导致的转移。

我们的模型例图建立了一个银行的在线登录系统。登录过程包括输入合法的密码和个人账号,再提交给系统验证信息。

登录系统可以被划分为四种不重叠的状态:Getting SSN, Getting PIN, Validating, 以及 Rejecting。每个状态都有一套完整的转移transitions来决定状态的顺序。

 

状态是用圆角矩形来表示的。转移则是使用带箭头的连线表示。触发转移的事件或者条件写在箭头的旁边。我们的图上有两个自转移。一个是在Getting SSN,另一个则在上Getting PIN

初始状态(黑色圆圈)是开始动作的虚拟开始。结束状态也是动作的虚拟结束。

事件或条件触发动作时用(/动作)表示。当进入Validating状态时,对象并不等外部事件触发转移。取而代之,它产生一个动作。动作的结果决定了下一步的状态。

活动图

活动图activity diagram是一个很特别的流程图。活动图和状态图之间是有关系的。状态图把焦点集中在过程中的对象身上,而活动图则集中在一个单独过程动作流程。活动图告诉了我们活动之间的依赖关系。

对我们的例子来说,我们使用如下的过程。

“通过ATM来取钱。”

这个活动有三个类Customer, ATMBank。整个过程从黑色圆圈开始到黑白的同心圆结束。活动用圆角矩形表示。

活动图可以被分解成许多对象泳道swimlanes ,可以决定哪些对象负责那些活动。每个活动都有一个单独的转移transition连接这其他的活动。

转移可能分支branch成两个以上的互斥的转移。保护表达式(在[]中)表示转移是从一个分支中引出的。分支以及分支结束时的合并merge在图中用菱形表示。

转移也可以分解fork成两个以上的并行活动。分解以及分解结束时的线程结合join在图中用粗黑线表示

组件与配置图

组件component是代码模块。组件图是是类图的物理实现。

配置图Deployment diagrams则是显示软件及硬件的配置。

下面的配置图说明了与房地产事务有关的软件及硬件组件的关系。

 



   物理上的硬件使用节点nodes
表示。每个组件属于一个节点。组件用左上角带有两个小矩形的矩形表示。

转载from:http://hi.baidu.com/brady_home/blog/item/6e6875604f9bdd41ebf8f89e.html

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值