UML Diagram - Basics

UML (Unified Modeling Language) Diagram - Basic Concepts


Classification of UML Diagram types:



UML Tells how to express an OO Design (Object Oriented Design). 
Graphical model follow Waterfall process which is: Analysis, Design and Coding.

Requirement Analysis:
1.Use Case: It describes how people interact with the System.
2. Class Diagram: It draws from the conceptual perspective, which can be a good way of building up a rigorous vocabulary of the domain.
3. Activity Diagram: It show the workflow of organization. It shows how software and human activities interact.
4. State Diagram: It is useful if a concept has an interesting life cycle with various states and events that changes that state.


Class Diagram:

In the Class Diagram there are three parts:
1) Name of the Class, 
2) Attributes, 
3) Operations

And, there are two kind of relationship: 
1. Association
2. Generalization

Properties of the Class Diagram represent structural features of a class. Properties are a single concept. It appears in two locations: Attributes & Associations.

Class Diagram:

Class Diagram

1. Attribute:

Syntax:
[visibility] <attribute name><attribute type>  [multiplicity] = [default value] {property string}

Visibility (Modifier in Java Language):
Type of Visibility:
Public (+)
Protected (#)
Default/ Package (~)
Private (-)
Derived (/)
static attribute is described in class diagram with adding Underline on the attribute declaration.
Derived attribute is not stored in variable but can be computed from the other attribute value.

Example of Attribute declaration:

- name : String[1]="UML" {readOnly}
Here, (-) : visibility is declared i.e. the attribute is Private
name : Variable name/attribute name. It is mandatory here.
String : This is the attribute type.
[1] : This is the Multiplicity of that attribute. This will describe in details later.
="UML" : In this attribute this UML value is initialize i.e. this is the default value of this newly created object.
{readOnly} :  This is the property string. It indicates additional property of that attribute. "readOnly" means the value of this attribute will never change throughout the program. (In Java language, readOnly attribute declared as "final")

Multiplicity:
It is an indication of property of how many objects may fill the property.
(i) Multiplicity 1 : It's means, it must have exactly one object.
(ii) Multiplicity 0..1 : It's means, It may or may not have one object.
(iii) Multiplicity * : It's means, there is no upper bound or limit, zero or more object(s) can have.
Note: The default Multiplicity is [1]. If lower bound and upper bound is same value, then we can put one number only, like: 1..1 is same as 1. * is same as 0..* as well. (zero represent optional)

A. Optional => Zero is in lower bound.
B. Mandatory => 1 or more digit implies it.
C. Single Value => It implies an upper bound of that value.
D. Multi Value => It implies an upper bound is more than one, normally it is '*'.

{property-string}:
(i) {unique} => Don't allow duplicate value.
(ii) {notUnique} => Allow duplicate value.
(iii) {readOnly} => The value of the variable can't alter, i.e. final variable in Java Language.
(iv) {ordered} => In the Collection of the variable, the value entry should be in order way.

2. Operation

Operations are methods in Class.
Syntax:
[visibility] <operation name>  (Parameter-List) : <return type>  {property string}

Here,
Visibility of Operation is same like Attribute.
Visibility (Modifier in Java Language):
Type of Visibility:
Public (+)
Protected (#)
Default/ Package (~)
Private (-)

Operation Name: Name of the Operation

Parameter-List:
This is the list of parameters for the operation.
Parameter forms:
direction <parameter name> <parameter type>   [= defalut_value]
Direction : It indicates whether the parameter is Input/ Output/ Both Input Output. Default is "Input". It can be declare as: in, out and inout.
Parameter Name: Name of the parameters
Parameter Type: Type of the parameter.
Default Value: Optionally default value can be assign.

Return type: Return type of the operation.

Property String: It is indicate additional properties of the operation. Example:
{query}: It's means, this operation returns value.
{modifier}: The operation which alter the data members or attributes.

Different between Operation and Method:
Operation is something that is invoked on an object - the procedure declaration. Where as, Method is the body of the procedure. For Example: In Polymorphism, if one super type have three sub-type, each one overrides the super-type - So, here one Operation and four Method implementation.


A. Association:

The other way to notate a property of attribute is as an Associations. Almost same information like in Attribute is appeared on an association.
The association is shown arrow line directed from source class to the target class.



Example of Association in Class Diagram:

Association
Example of Association in Java Language:

class SourceClass {
TargetClass tClass;

}

class TargetClass {
}

Example of using Attribute and Association with two separate way:


Example: Use Attribute in Class Diagram

Example: Use Association in Class Diagram

A.a. Bi-Directional Association :

It is a pair of properties that are linked together as inverses.

Example: Bi-Directional Association
Here,
Car class => owner : Person[1]
Person class => cars : Car[*]

A.b Qualified Association:

This is equivalent to associative arrays, maps, hashes and directories.
Example of qualified association diagram:
Example: Qualified Association

Example in Java Language:
class Order {
   public OrderLine getLineItem(Product product);
   public void addLineItem(Number amount, Product product);
}


So, all access to a given OrderLine requires a Product as an argument, suggesting an implementation using Key and Value data structure.



B. Generalization: 

In Inheritance mechanism, the super class is Generalization with respect to all sub-classes or, implement the interface as well. Here interface is generalization.
It is refer to inheritance. If many similar existing objects are combined to form a super class to do the job of its sub-class then this super class is called Generalization.

Specialization:
If some new sub-classes are created from an existing super class to do specific job of the super class, then it is called "Specialization".

   In Generalization, it is not necessary that the super class should know about the existing of its sub-classes. In case of Specialization, the sub-classes will always know its super class.
Example:
Example: Generalization
Here, Train class (or, Train interface) is Generalization, where as, Passenger class and Goods class are Specialization.
Example code in Java Language:

class Train { }
class Passenger extends Train { }
class Goods extends Train { }

Notes and Comments in UML :

If user want to provide some notes or comments on UML diagram, normally it is done by this below sample diagram:

Example: Notes in UML Diagram

Dependency:

A dependency exists between two elements, if changes to the definition of one element (supplier) may cause changes to the other (client).
Example:
One class send a message to another.
- One class has it as part of its data
- One class mentioned it as a parameter to an operation.

Diagram:
Example: Dependency
Here,
- Dependency always one direction only.
- If any interface is changed in Class B, then Class A need to be changed accordingly, But not vice-versa.
- If Class C or Class D is need to change the interface, this effect(directly) is not goes to Class A. Only Class B need to modify, if there is no need to interface change in Class B.
- A sub-class is dependent on its super class but not vice-versa.

Dependency Keywords:
1. <<call>> => The source calls an operation in the target.
2. <<create>> => The source creates instances of the target.
3. <<derive>> => The source is derived from the target.
4. <<instantiate>> => The source is an instance of the target (If source is class then it itself is an instance and target is then meta-class).
5. <<permit>> => The target allows the source to access the target's private features.
6. <<realize>> => The source is implementation of interface defined by target.
7. <<substitute>> => The source is substitutable for the target
8. <<trace>> => Use to track the classes
9. <<use>> => The source requires the target for its implementation.




Class Diagram - Advance Concepts

1. Aggregation and Composition

Aggregation is the "part-of" relationship.Aggregation is the special form of Association. If the relationship is "Has-a" relation, then it is called Aggregation. However Aggregation is a "Directional Association", it is "one way association". To maintain code-re-usability Aggregation is needed.
Example of Aggregation Diagram:
Example: Aggregation
Java Code Example of Aggregation:
class Person {
   getMembers() {
   ...
   }
}

class Club {
  Person person; // This is Aggregation
  person.getMembers(); // Here, code re-usability
}

Composition is a special type of Aggregation. A restricted aggregation is called Composition. When one object contains the other object and contained object can't exist without container object then it is called Composition. It is also "Has-a" relationship.

Example of Composition Diagram:
Example: Composition
Java Code of Composition:

class LinkedList {
   Node head;  // Composition

}

- Both Aggregation and Composition are actually type of Association. It is "Has-a" relationship.
- Composition is strong has-a relationship, whereas Aggregation is weak "has-a" relationship.


2. Abstract Class and Interface:

Abstract class is drawn in UML as Italic style or used label {abstract}
For Interface, it mark as <<interface>>

Classes have two kind of relationship with interface:
1. Providing an interface
2. Requiring an interface

1. Providing an Interface:
If a class is substitutable for the interface then it is called providing an interface. In Java, is an interface is implemented then it is call Providing an interface.

2. Requires an Interface:
If an instance of that interface is required, then it is called requiring an interface. This is having dependency on the interface.

Example of two type of Interface Diagram in UML:
Example of Abstract Class and Interface UML Diagram
Java class structure according to this UML diagram:

interface Collection {
   public boolean equals() { ... }
   public void add() { ... }
}

interface List  extends Collection {
   public List get() { ... }
}

abstract class AbstractList implementes List {
   public abstract List get();
   public boolean equals() { ... }
   public void add() { ... }
}

class ArrayList extends AbstractList {
   public List get() { ... }
   @Overrride public void add() { ... }
}

class Order { List item; }




Template or Parameterized class (Generic class) :


This concept is mostly used with Collections with strongly typed defined. Using this, we can define the behaviour for sets in general.

Example in Java Language:

class Set <T> {
    void insert (T element);
    void remove (T element);

}

Set <Employee> empSet ;

Diagram of Template class :
Example: Template or Parameterized class



Enumeration:

It is a fixed set of values that don't have any property other than their symbolic value. It is shown in class diagram with Set <<enumeration>> key.
Example of Enumeration Diagram:


Example: Enumeration diagram


Association Class :

Association class is used to capture certain characteristics of an association between two classes. These characteristic do not belong to the classes being associated but instead belong to the relationship between the classes. Association class make sense only if they represent associations with some behaviour and state.
Example of Association Class Diagram:
Example: Association Class




Package Diagram:

Example-1 of Package Diagram:
Example 1


Example-2 of Package Diagram:
Example 2

No comments:

Post a Comment