Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unified Modelling Language

Similar presentations


Presentation on theme: "Unified Modelling Language"— Presentation transcript:

1 Unified Modelling Language
Class & Object Diagrams

2 Class Diagrams

3 UML class diagrams What is a UML class diagram?
A picture of the classes in an OO system, their fields and methods, and connections between the classes that interact or inherit from each other What are some things that are not represented in a UML class diagram? details of how the classes interact with each other algorithmic details; how a particular behavior is implemented

4 Diagram of one class A class is the description of a set of objects having similar attributes, operations, relationships and behavior. Class Name Window size: Size visibility: boolean display() hide() Attributes Operations

5 Class attributes attributes (fields, instance variables)
visibility (name ) : type [count] = default_value visibility: + public # protected - private ~ package (default) / derived underline static attributes derived attribute: not stored, but can be computed from other attribute values attribute example: - balance : double = 0.00 +Rectangle(width: int, height: int):void

6 Class operations / methods
visibility (name ) (parameters) : return_type visibility: + public # protected - private ~ package (default) underline static methods parameter types listed as (name: type) omit return_type on constructors and when return type is void method example: + distance(p1: Point, p2: Point): double

7 Attribute Example (C++ implementation)
+ author: string = “unknown” + text : string - total : long = 0 Note class Note { private: static long total = 0; public: string author = “unknown”; string text; ... }

8 Operation Example (C++ implementation)
class Figure { private: Size size; Position pos; static long figureCount = 0; public: void move(Position pos) { ... } static long getFigureCount() { return figureCount; } } Figure size: Size - pos : Position figureCount : long = 0 + move(pos : Position) + getFigureCount() : long

9 Comments represented as a folded note, attached to the appropriate class/method/etc by a dashed line

10 Relationships between classes
Class A Superclass Class with parts Class with parts Interface name Class B Subclass Assembly Class Assembly Class Concrete Class Association (relationship) Inheritance (Generalization) (is-a, kind-of) Aggregation (Part-Of) Dependency Realization

11 Associations A semantic relationship between two or more classes that specifies connections among their instances A structural relationship specifying that objects of one class are connected to objects of a second (possibly the same) class Example: “A player plays on a team” You can imagine an association that you could read in another direction. e.g, a team employs a player

12 Association Names describes nature of relationship:
can also show direction to read name: Company Person works for Company Person works for employs

13 Association Roles describe “faces” that objects of a class present to each other within association class can play same or different roles within different associations Company Person employee employer

14 Multiplicity The number of objects from one class that relate with a single object in an associated class Example: A basketball team has five players (not counting substitutes). In the other direction, a player can play for just one team 1..* Person Company 1

15 Multiplicities 1 Class exactly one 0..* many (zero or more) Class 0..1
optional (zero or one) Class m..n numerically specified Class Example: 0..* Course CourseOffering 1

16

17 Bidirectional Association

18 Unidirectional Association
class, but the BankAccount class does not know about the association                                                                               Unidirectional Association

19 Aggregation A special form of association that models a whole-part relationship between an aggregate (the whole) and its parts. Models a “is a part of” relationship. 4 Car Wheel wheels Whole Part

20 Aggregation (C++ implementation)
class Car { private: Wheel wheels[4]; public: void Car( Wheel w1, Wheel w2, … ) { // we can check w1, w2, etc. for null // to make sure wheels exist wheels = new Wheel[4]; wheels[0] = w1; wheels[1] = w2; … }

21 Composition A strong form of aggregation
The whole is the sole owner of its part The part object may belong to only one whole Multiplicity on the whole side must be one The life time of the part is dependent upon the whole When Circle is destroyed, Point is also destroyed The composite must manage the creation and destruction of its parts Circle Point 3..* 1 Polygon

22 Composition (C++ implementations)
class Circle { public: void SetCenter(const Point&); void SetRadius(double); double Area() const; double Circumference() const; private: double itsRadius; Point itsCenter; };

23 Aggregation and Composition

24 Aggregation and Composition

25 Generalization relationship
Indicates that objects of the specialized class (subclass) are substitutable for objects of the generalized class (super-class) “is kind of” relationship Super Class An abstract class Generalization relationship Sub Class Sub Class

26 Generalization A sub-class inherits from its super-class
Attributes Operations Relationships A sub-class may Add attributes and operations Add relationships Refine (override) inherited operations

27 Generalization (C++ implementation)
class shape { public: virtual void Draw() = 0; virtual void Erase() = 0; …. }; class Circle:public Shape { }; class Square:public Shape { };

28 Generalization Example (1)

29 Generalization Example (2)

30 Abstract classes and operations
The observant reader will notice that the diagrams in the previous slides use italicized text for the BankAccount class name and withdrawal operation This indicates that the BankAccount class is an abstract class and the withdrawal method is an abstract operation The BankAccount class provides the abstract operation signature of withdrawal and the two child classes of CheckingAccount and SavingsAccount each implement their own version of that operation. However, super classes (parent classes) do not have to be abstract classes It is normal for a standard class to be a super class.

31 processTransactions ()
Dependency A dependency indicates a semantic relation between two or more classes in which a change in one may force changes in the other although there is no explicit association between them A stereotype may be used to denote the type of the dependency Dependencies between classes may exist because: One class sends a message to another One class has another as part of its data One class mentions another as a parameter to an operation Parser getTransaction() uses Bank processTransactions ()

32 Dependency (C++ implementation)
class Bank { … public: void processTransactions() // Parser p is a local variable … Parser p = new Parser(…); p.getTransaction(); } };

33 <<interface>> List
Realization A realization relationship indicates that one class implements a behavior specified by another class (an interface or protocol). There are classes that have nothing but pure virtual functions In Java such entities are not classes at all; they are a special language element called an interface. An interface can be realized by many classes A class may realize many interfaces LinkedList <<interface>> List LinkedList List

34 Reflexive Association
However, a class can also be associated with itself, using a reflexive association

35 Basic Class Diagram (Example)
Person Head Arm Class Student takes

36 Class Diagrams (Advanced)
StaffMember Student 1..* * instructs instructor Association name Role name Multiplicity Navigable (uni-directional) association Courses pre - requisites 0..3 Reflexive association Role

37 Class diagram example 1..* 1 0..1 DVD Movie VHS Movie Video Game
Rental Item Rental Invoice 1..* 1 Customer Checkout Screen 0..1 Simple Association Class Abstract Aggregation Generalization Composition Multiplicity

38 Class Diagram Summary Provide abstraction of problem domain
Embodies small set of well-defined responsibilities Clear separation between specification and implementation Understandable and simple Show only important properties

39 Class Diagram Summary Organize similar classes into packages
Beware of cyclical generalization Use associations where there are structural relationships Start with Analysis, then refine details

40 Object Diagrams

41 Object Diagram An object diagram is a snapshot of the objects in a system at a point in time. Since it shows instances rather than classes, an object diagram is often called an instance diagram. A graphic representation of the relationships between these instantiated classes at any point of time (called objects) is called an "Object diagram." It very similar to a class diagram, and uses the similar notations to denote relationships.

42 Object Diagram Vs. Class Diagram
A class diagram, would not give the picture of how these classes interact with each other at runtime, and in the actual system, how the objects created at runtime are related to the classes. An object diagram shows this relation between the instantiated classes and the defined class, and the relation between these objects, in the logical view of the system.

43 Elements of an Object Diagram
Object Diagram consists of the same elements as a class diagram (it contains classes and links showing the relationships). The minor difference is that the class diagram shows a class with attributes and methods declared. An object diagram, these attributes and method parameters are allocated values.

44 Example(1) College-Student class diagram
The class diagram with attributes

45 Example(1) Cont. The object diagram for the College-Student class diagram

46 Example(1) - Explained The object diagram shows how objects are instantiated in the running system represented by the College-Student class diagram The class diagram shows that a single college has many students, and defines the variables. The object diagram for the same system shows instantiated classes of Student (Student #1 and Student #2) enrolled in College The object diagram shows the name of the instantiated object, separated from the class name by a ":", and underlined, to show an instantiation. Eg. Graduate School of Business: College In the diagram, values are assigned to variables and represented using the notation variable name=variable value.

47 Example (2) c: Company d1: Department name = “R&D”
: Contact Info phone = “411” p1: Person ID = “13”

48 Example (3)


Download ppt "Unified Modelling Language"

Similar presentations


Ads by Google