Vous êtes sur la page 1sur 5

A Brief Guide to the UML Class Diagrams

Introduction The Unified Modeling Language (UML) is a graphical language for creating diagrams that are useful in the software development process The UML provides a set of standard graphical s!m"ols and the rules for com"ining them The UML is process and implementation language independent The UML provides s!m"ols to support various views of the s!stem #n all$ there are nine diagram t!pes included in the UML% Class diagrams$ &"'ect diagrams$ Use case diagrams$ (e)uence diagrams$ Colla"oration diagrams$ (tate*chart diagrams$ Activit! diagrams$ Component diagrams$ and Deplo!ment diagrams #n this guide$ we will "e concerned with class diagrams$ which provide a structural view of the s!stem Class Diagram s Class diagrams are perhaps the most common t!pe of diagram used in modeling o"'ect*oriented s!stems A class diagram shows a set of classes$ interfaces$ colla"orations$ and dependencies #n addition$ notes and constraints can appear on a class diagram +ou use class diagrams to show the static structure of !our s!stem A class is represented "! a "o, that is divided hori-ontall! into three sections The top section contains the class name$ the middle section contains the class attri"utes and the "ottom section contains the class methods An e,ample of a class diagram for a Time class is shown "elow
Time hour minute second setTime() displayMilitaryTime() displayStandardTime()

Class Name

The class name appears in the top section of the class "o, and consists of te,t The class name is a noun or noun phrase T!picall!$ the first letter of each word in the phrase is capitali-ed

*.*

Class Attribute s

The middle section of the class "o, contains the class attri"utes An attri"ute represents some propert! of the class The attri"ute name consists of te,t and is a noun or noun phrase T!picall!$ the first letter of each word in the phrase is capitali-ed$ e,cept the first +ou can$ optionall!$ specif! the t!pe of the attri"ute and possi"l! a default value /or e,ample$ to specif! that the attri"ute hour is of t!pe integer and has an initial value of .0$ replace hour in the a"ove diagram with hour : Integer = 12 Collections (or arra!s) of attri"utes are indicated "! giving the multiplicit! 1efer to the discussion of multiplicit! in the Association section "elow Nota Bene: By specifying the type and default values, you are increasing the information content of the diagram at the possible expense of cluttering the diagram. This same comment is applicable to other optional information. The "ottom section of the class "o, contains the class methods A method is an a"straction of something that an o"'ect can do$ or have done to it The method name consists of te,t and is a ver" or ver" phrase$ followed "! left and right parenthesis T!picall!$ !ou capitali-e the first letter of each word in the name$ e,cept the first +ou can$ optionall!$ specif! the names$ t!pes and default of all method arguments as well as the return t!pe of functions /or e,ample$ a full specification of the setTime() method might loo2 li2e
setTime( new our : Integer = 12! newMinute : Integer = "! new#econd : Integer = " )

Class Methods

$isibilit%

The UML provides s!m"ols to indicate the visi"ilit! of attri"utes and methods The following s!m"ols$ when place immediatel! "efore either an attri"ute name or method name$ define the visi"ilit! of the attri"ute or name% & pu"lic ' private ( protected /or e,ample$ to give the attri"ute minute protected visi"ilit!$ !ou would write
( minute

#co)e

+ou can specif! the scope of an attri"ute using the UML B! default$ an attri"ute has instance scope That is$ each o"'ect created from the class has its own value for the attri"ute +ou can give an attri"ute class scope$ "! underlining the attri"ute name (and t!pe and default value if present) The most common use for class scope is to share a private varia"le among all instances of the class +ou might do this$ for e,ample$ if !ou needed to give each instance a uni)ue identifier The UML provides graphic s!m"ols to represent the following relationships% Dependenc! Association Generali-ation "etween classes

*elationshi)s

*0*

De)endenc%

A dependenc! is a relationship "etween two classes in which a change to one$ called the independent class$ causes a change in the other$ called the dependent class A dependenc! is represented "! a dashed line from the dependent class to the independent class /or e,ample$ if !ou have a CD+la%er pla!er class and a *emoteControl class$ !ou would indicate the dependenc! "! the following diagram
CDPlayer RemoteControl

Association

An association is a relationship that specifies that the o"'ects of one class are connected to o"'ects of another class An association is represented "! a solid line connecting the two classes /or e,ample$ suppose !ou have an ,m)lo%ee class and a Com)an% class An association is depicted "! the following diagram
Employee 1 .. * * Company

The notations a"ove the solid connecting line indicate the multiplicit!$ or how man! The a"ove diagram indicates that an instance of the ,m)lo%ee class can wor2 for man! companies and that an instance of the Com)an% class has one or more emplo!ees (ome e,amples of multiplicities and their meaning are% e,actl! four 1 .. / one to three * man! The same notation is used to indicate a collection or arra! of attri"utes /or e,ample$
)ort0-1 : #erial2+ort

indicates that there are 3 ports of t!pe (erial&4ort &ne particular one*to*man! association (aggregation) fre)uentl! arises in o"'ect* oriented modeling% the 5whole6part7 relationship An aggregation is represented "! a solid line connecting the two classes$ with an open diamond at the end of the line ad'acent to the 5whole7 class An e,ample is the aggregation of famil! mem"ers within a household The UML diagram is shown "elow
Household amily Mem!er

*8*

There is a variation of aggregation$ which is called composition Composition is a relationship that has strong ownership and simultaneit! of lifetimes of the classes That is$ if the part class "elongs to 'ust one whole class and cannot e,ist outside the whole o"'ect$ the relationship is composition An aggregation is represented "! a solid line connecting the two classes$ with an solid diamond at the end of the line ad'acent to the 5whole7 class An e,ample of composition is the relationship "etween a college and its departments The UML diagram is shown "elow
Colle"e Department

3enerali4atio n

A generali-ation is a relationship "etween a general class and a more specific class Generali-ation is the inheritance relationship A generali-ation is represented "! an arrow$ with an unfilled head$ from the specific class (derived class) to the general class ("ase class) /or e,ample$ suppose !ou have a 3raduate#tudent class and a #tudent class The generali-ation is depicted "! the following diagram
#raduateStudent Student

Notes

A note is used to place comments on the class diagram #f the note applies to a specific item on the diagram$ connect the note with the item "! a dashed line An e,ample of a note
See al"or.doc $or a description o$ this al"orithm

Constraints

Constraints are specific conditions that must "e true for the model to "e valid Constraints are enclosed in "races 9 : /or e,ample$ suppose that we had a +la%ingCard class and one of the properties is the suit ;e would li2e to constrain suit to hold onl! the values% clubs$ diamonds$ hearts$ or s)ades An e,ample of a constraint is
+la%ingCard ... suit % & clu!s' diamonds' hearts' spades( ... ...

Constraints can appl! to other items in the diagram$ in addition to attri"utes *3*

#ummar%

This "rief guide is onl! an introduction to class diagrams< it is "! no means complete /urther more$ class diagrams are "ut one of the nine diagrams supported "! the UML /or more details on class diagrams$ or information on the other eight diagrams$ refer to The Unified Modeling Language User Guide "! Grad! Booch$ =ames 1um"augh$ and #var =aco"son

*>*

Vous aimerez peut-être aussi