1 / 48

Unified Modelling Language

Unified Modelling Language. Class & Object Diagrams. Class 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

ebaldwin
Télécharger la présentation

Unified Modelling Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 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

  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

  6. 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

  7. 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; ...}

  8. 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; } ...}

  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. works for Person Company Association Names describes nature of relationship: can also show direction to read name: works for Person Company 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 employee Person Company 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 many (zero or more) 0..* Class 0..1 optional (zero or one) Class m..n numerically specified Class Example: 0..* Course CourseOffering 1

  16. Bidirectional Association

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

  18. 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

  19. 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; … } }

  20. 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

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

  22. Aggregation and Composition

  23. Aggregation and Composition

  24. 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

  25. 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

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

  27. Generalization Example (1)

  28. Generalization Example (2)

  29. 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.

  30. 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

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

  32. 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

  33. Reflexive Association • However, a class can also be associated with itself, using a reflexive association

  34. Head Arm Person Class Student Basic Class Diagram (Example) takes

  35. 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)

  36. 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

  37. 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

  38. 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

  39. Object Diagrams

  40. 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.

  41. 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.

  42. 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.

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

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

  45. 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.

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

  47. Example (3)

More Related