About UML Class Diagrams

About UML Class Diagrams





About 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 symbols and the rules for combining them.  The UML is process and implementation language independent. 
The UML provides symbols to support various views of the system.  In all, there are nine diagram types included in the UML:
·      Class diagrams,
·      Object diagrams,
·      Use case diagrams,
·      Sequence diagrams,
·      Collaboration diagrams,
·      State-chart diagrams,
·      Activity diagrams,
·      Component diagrams, and
·      Deployment diagrams.
In this guide, we will be concerned with class diagrams, which provide a structural view of the system.



Class Diagrams


Class diagrams are perhaps the most common type of diagram used in modeling object‑oriented systems.  A class diagram shows a set of classes, interfaces, collaborations, and dependencies.  In addition, notes and constraints can appear on a class diagram. You use class diagrams to show the static structure of your system.
A class is represented by a box that is divided horizontally into three sections.  The top section contains the class name, the middle section contains the class attributes and the bottom section contains the class methods.  An example of a class diagram for a Time class is shown below.




Class Name


The class name appears in the top section of the class box and consists of text.  The class name is a noun or noun phrase.  Typically, the first letter of each word in the phrase is capitalized.



Class Attributes


The middle section of the class box contains the class attributes.  An attribute represents some property of the class.  The attribute name consists of text and is a noun or noun phrase.  Typically, the first letter of each word in the phrase is capitalized, except the first. You can, optionally, specify the type of the attribute and possibly a default value.  For example, to specify that the attribute hour is of type integer and has an initial value of 12, replace hour in the above diagram with   hour : Integer = 12 .  Collections (or arrays) of attributes are indicated by giving the multiplicity.  Refer to the discussion of multiplicity in the Association section below.  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.



Class Methods


The bottom section of the class box contains the class methods.  A method is an abstraction of something that an object can do, or have done to it.  The method name consists of text and is a verb or verb phrase, followed by left and right parenthesis.  Typically, you capitalize the first letter of each word in the name, except the first.  You can, optionally, specify the names, types and default of all method arguments as well as the return type of functions. For example, a full specification of the setTime() method might look like
  setTime( newHour : Integer = 12, newMinute : Integer = 0, newSecond : Integer = 0 )



Visibility


The UML provides symbols to indicate the visibility of attributes and methods.  The following symbols, when place immediately before either an attribute name or method name, define the visibility of the attribute or name:
                        +          public
                        -           private
                        #          protected
For example, to give the attribute minute protected visibility, you would write # minute.



Scope


You can specify the scope of an attribute using the UML.  By default, an attribute has instance scope.  That is, each object created from the class has its own value for the attribute.  You can give an attribute class scope, by underlining the attribute name (and type and default value if present).  The most common use for class scope is to share a private variable among all instances of the class.  You might do this, for example, if you needed to give each instance a unique identifier.



Relationships


The UML provides graphic symbols to represent the following relationships:
·      Dependency
·      Association
·      Generalization
between classes.



Dependency


A dependency is a relationship between two classes in which a change to one, called the independent class, causes a change in the other, called the dependent class.  A dependency is represented by a dashed line from the dependent class to the independent class. For example, if you have a CDPlayer player class and a RemoteControl class, you would indicate the dependency by the following diagram




Association

An association is a relationship that specifies that the objects of one class are connected to objects of another class. An association is represented by a solid line connecting the two classes.  For example, suppose you have an Employee class and a Company class.  An association is depicted by the following diagram


The notations above the solid connecting line indicate the multiplicity, or how many.  The above diagram indicates that an instance of the Employee class can work for many companies and that an instance of the Company class has one or more employees.  Some examples of multiplicities and their meaning are:
                        4                      exactly four
                        1 .. 3                one to three
                        *                      many
The same notation is used to indicate a collection or array of attributes.  For example,
           port[4] : SerialOPort
indicates that there are 4 ports of type SerialOPort
One particular one-to-many association (aggregation) frequently arises in object-oriented modeling: the “whole/part” relationship.  An aggregation is represented by a solid line connecting the two classes, with an open diamond at the end of the line adjacent to the  “whole” class.  An example is the aggregation of family members within a household.  The UML diagram is shown below.





There is a variation of aggregation, which is called composition.  Composition is a relationship that has strong ownership and simultaneity of lifetimes of the classes.  That is, if the part class belongs to just one whole class and cannot exist outside the whole object, the relationship is composition. An aggregation is represented by a solid line connecting the two classes, with an solid diamond at the end of the line adjacent to the  “whole” class.  An example of composition is the relationship between a college and its departments.  The UML diagram is shown below.....










0 commentaires: