480 likes | 491 Views
Explore UML class diagrams: elements like attributes and operations, class interactions, and relationship types such as aggregation and inheritance. Learn how to represent object-oriented systems effectively.
E N D
Unified Modelling Language Class & Object Diagrams
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
Diagram of one class Window size: Sizevisibility: boolean display()hide() • A class is the description of a set of objects having similar attributes, operations, relationships and behavior. Class Name Attributes Operations
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
Class operations / methods • 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 andwhen return type is void • method example:+ distance(p1: Point, p2: Point): double
Attribute Example (C++ implementation) Note + author: string = “unknown”+ text : string- total : long = 0 class Note{ private: static long total = 0; public: string author = “unknown”; string text; ...}
Figure • size: Size- pos : Position • figureCount : long = 0 + move(pos : Position)+ getFigureCount() : long 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; } ...}
Comments • represented as a folded note, attached to the appropriate class/method/etc by a dashed line
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
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
works for Person Company Association Names describes nature of relationship: can also show direction to read name: works for Person Company employs
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 employee Person Company employer
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
Multiplicities 1 Class exactly one many (zero or more) 0..* Class 0..1 optional (zero or one) Class m..n numerically specified Class Example: 0..* Course CourseOffering 1
class, but the BankAccount class does not know about the association Unidirectional Association
Car 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 Wheel wheels Whole Part
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; … } }
1 Circle Point 3..* Polygon 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
Composition(C++ implementations) class Circle { public: void SetCenter(const Point&); void SetRadius(double); double Area() const; double Circumference() const; private: double itsRadius; Point itsCenter; };
Generalization • 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
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
Generalization(C++ implementation) class shape { public: virtual void Draw() = 0; virtual void Erase() = 0; …. }; class Circle:public Shape { }; class Square:public Shape { };
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.
Bank processTransactions () Parser getTransaction() uses 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
Dependency(C++ implementation) class Bank { … public: void processTransactions() { // Parser p is a local variable … Parser p = new Parser(…); … p.getTransaction(); … } … };
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
Reflexive Association • However, a class can also be associated with itself, using a reflexive association
Head Arm Person Class Student Basic Class Diagram (Example) takes
Role name Association name instructor StaffMember Student 1..* instructs * Role Navigable (uni-directional) association Multiplicity * pre - requisites Courses 0..3 Reflexive association Class Diagrams (Advanced)
Multiplicity Customer Simple Aggregation 1 Class Abstract Class Rental Invoice 1..* Rental Item 1 0..1 Composition Simple Association Generalization Checkout Screen DVD Movie VHS Movie Video Game Class diagram example
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
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
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.
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.
Elements of an Object Diagram • Object Diagramconsists 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.
Example(1) College-Student class diagram The class diagram with attributes
Example(1) Cont. The object diagram for the College-Student class diagram
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.
d1: Department p1: Person : Contact Info name = “R&D” ID = “13” phone = “411” Example (2) c: Company