UML – Class Diagrams

Long time ago far far away in galaxy there was a fussy company called Rational Software Corporation. They always used to cry about existing object oriented modelling systems. With all fussing and crying and some more fussing and crying they somehow manage to frustrate the GOD. Now since GOD cannot directly intervene in our day to day business (not sure read some where) so he came up with a plan. He came into dreams of Three Amigos and gave them Idea about theUML. Inspired by the dream Three Amigos contacted Rational and preached them the gospel of UML and delivered the GOD of all the sufferings. Here is an exert from that gospel.

Since I am always honest I will tell you again that I copied bit and pieces from other places and wrote this one. But I feel it is good one so happy reading and in advance I thank you  for writing appreciative comments.

A UML Class diagram is a static structural diagram that describes structure of a system by showing system classes, their attributes, operations and the relationships  they have with other  classes in system.

In a Class Diagram each class is represented by a box with three component.

  • First compartment – Name of the class
  • Second compartment – Properties/attributes of the class.
  • Third compartment – Methods or operation of the class.
            

Attributes – Attributes of classes are the properties representing features of the class. For example a car class can have attribute like color, make, model etc. In UML the attributes are present in the second compartment of the class box. Attribute can be classified as inline or static. Inline attributes are specific to instance of the class, while the static attribute belongs to the class. Attributes are typically represented as
[visibility]/[attributeName]:[attributeType] [multiplicity=defaultValue] {properties and constraints}
For example consider a library with many members, all the members are ordered by their names and they are unique.
-mMembers:Member[o..*]{ordered,unique}
The static attributes are represented by underlining it.
Visibility – Attribute as well as operations visibility can be expressed using the following

  • + public member
  • – private member
  • # protected member
  • ~ package
  • / derived
  • underline – static
Properties – Apart from ordered and unique their are many other properties which an attribute can have. To list them
  1. ordered – used when multiplicity is greater than 1. It means that attributes should be ordered sequentially using some rule.
  2. unique – This property is also used with attributes with multiplicity greater than 1. It means that all the values in the collection are unique.
  3. readOnly – The attribute value cannot change once the value has been set (final variables in java).
  4. union – This is mostly used for attributes derived from other attributes.
  5. redefine <attribute_name>- Means that attribute is alias for some other attribute.
  6. subset<attribute_name> – This means that attribute is a subset of all the valid values of attribute_name.
  7. composite  – This property is used to signify that attribute is a part class which composes the main class.  
Class Mutliplicity – Multiplicity of a class can be defined by placing a number on upper right corner of top most compartment. It signifies the number of allowable instances of class in the system. For example multiplicity 1 indicates a singleton class
                               
Class Relationships – The different classes in a system share a distinct and different kind of relationships. These relationships are denoted by bunch of line and different start and end points. These relationships can be categorized into following
 
  • Dependency
  • Association
  • Aggregation
  • Composition
  • Realization
Dependency– Dependency is the weakest relation that two class can have. It is indicative of a loose coupling between two classes. It signifies a “uses a” kind of relationship. For exmaple a FileReader uses a FileUtils class to get the absolute path of a file. Dependency is indicated by a dashed line followed by an arrow pointing to the target class.
 dependency
Association – Association is a stronger relation than dependency. It indicates a “has a” type of relation. Association is represented by a drawing a solid line between two classes. Association relation may persist longer than a single method call.  For example A class has many student. 
Self Association Some times a class can be associated with itself. Consider a team of employee. It is possible that team can have only one manager.
Multiplicity in Association – Some times Two classes can have multiple associations and each association has different multiplicity. For example consider a team of football players. A player can belong to only one team and each team has 10 players and exactly one captain.
Constrained Association – A class can have many similar elements and relationship between the elements can be expressed using constraints. Constraints can be shown using a dotted line between the two element and nature of constraint is expressed using a text enclosed within curly braces. For example consider a airline reservation system. This system can book a ticket which can be a business class ticket or economy class ticket.
 
 
 
There are many other constraints that are defined by UML’s own Object Constraint Language. There is an awesome pdf which you can find here http://www.omg.org/spec/OCL/2.2/. This PDF will give all the information you need regarding the OCL syntax.
 
Named Association – And finally we can have a named association. The association can be named using short meaningful phrases which describes the association. UML advises us to put an triangle at end of the phrase which tells the reader about the direction in which name should be read. The following example will explain it all.
 
 
Aggregation – Aggregation is a relationship that is stronger than dependency and association. Aggregation signifies a “owns a” relationship i.e. Class A owns a Class B. It is denoted by a solid line with diamond at end of owner and a arrow at the end of class that is owned. As is case of association, an aggregation can be directional but in most situation it is navigable only from container to contained. For example a Department owns an employee
 
 
An employee can be transferred to other department but at any given period he is owned by the department or department aggregates a bunch of employees. As is case of association can be refined using constraints, multiplicity, tagged values and names.
 
Composition – Composition is the strongest relationship that two classes can have. It signifies “is made up of” kind of relationship and is denoted by a dotted line with a solid diamond at end of composed and arrow at end of the composer. Composition is a special kind of aggregation the only difference it that the relationship exists for lifetime of objects. So if a Class A is made up of Class B and Class A is destroyed then Class B will also get destroyed. Class B simply cannot exist without Class A. A typical example of composition is a Connection Pool. A connection pool is made up of many connections and once connection pool is shut down the connection objects are destroyed. 
 
Generalization – Generalization is used to show inheritance. It is typically read as “is a” kind of relationship. So is Class B is subclass of Class A then this relationship can be read as Class B is a Class A. Generalization is denoted by a solid line with a closed arrow pointing towards the super class. Example below should be sufficient to explain the diagram.  
 
 
Realization – And Last but not the least we have Realization. Realization indicates that a Class implements or realizes an Interface. In other words realization ties an interface with its implementation. It is indicated by a dotted line with a closed arrow pointing towards the interface.
 
 
Association Class – Sometime it happens that while associating two classes it becomes necessary to bring in a third class in picture as it contains a valuable information about the association. To explain this let me take up an example of a bank. A bank has many customers and each of the customers has an account is one of the bank’s branch. So association between a bank and customer is tied up using a third entity called branch. The branch class can be termed as association class.
 
To denote a association class a dotted line is drawn between association line of primary classes to the association class.
 
Association Qualifiers –  Often a one to many associations are related via a unique value in the target class. For books in library may be related to its members via a memberId.  This unique key relation can be depicted by something known as association qualifier. Qualifiers are represented by a small rectangle attached to the bigger class with name of the unique key written in it.
Abstract Operation – In abstract classes and interfaces the operations are abstract. An abstract operation is written in italics. In example below convertToFlv() is an abstract method hence it is written in italics.
 
Phew! that was quite something. Writing a blog post is very difficult and I am sure I have missed many things. But I know that my fans (err I meant dear readers :)) will find few missing points or mistake and they will notify me about it. But for now I feel bit tired and want to go out for a coffee. 
 
I my next post on UML I will be producing an awesomely beautiful essay on Package Diagram. I will surely remember to out a link out here for easy navigation. Till then bye bye and take care :):).
Print Friendly, PDF & Email

About Niraj Singh

I am CEO and CoFounder of a startup "Aranin Software Private Limited, Bangalore. I completed my graduation in 2002 as an Aerospace Engineer from IIT Kharagpur. I love working on new ideas and projects and recently released my first open source project JaiomServer "http://jaiomserver.org". I have 9 years of experience in IT industries most of which I have spent in developing community applications for various clients using java. Some of the sites in which I have actively involved with are hgtv.com, food.com, foodnetwork.com, pickle.com, diynetwork.com etc.
This entry was posted in Class Diagram, UML and tagged , . Bookmark the permalink.

One Response to UML – Class Diagrams

  1. This text is worth everyone

Comments are closed.