Module 4 Relationships Lawrence Chung CS 6359 0

  • Slides: 23
Download presentation
Module 4: Relationships Lawrence Chung CS 6359. 0 T 1: Module 4 1

Module 4: Relationships Lawrence Chung CS 6359. 0 T 1: Module 4 1

Overview p Relationships n n p Dependency Generalization Association Realization (See Module 2 for

Overview p Relationships n n p Dependency Generalization Association Realization (See Module 2 for this) Modeling Techniques Lawrence Chung CS 6359. 0 T 1: Module 4 2

Relationships and Associations different words, but more or less same concept p UML n

Relationships and Associations different words, but more or less same concept p UML n Associations p Fusion p Relationships OMT n Associations n Lawrence Chung CS 6359. 0 T 1: Module 4 3

Relationships: 3 Kinds Window Event open() close() dependency generalization Console. Window Dialog. Box Control

Relationships: 3 Kinds Window Event open() close() dependency generalization Console. Window Dialog. Box Control association Lawrence Chung CS 6359. 0 T 1: Module 4 4

Dependency p p p A change in one thing may affect another. “Uses” relationship.

Dependency p p p A change in one thing may affect another. “Uses” relationship. May have a name, but not common. Audio. Clip name record(m: Microphone) start() stop() Microphone dependency One important use of dependency Lawrence Chung CS 6359. 0 T 1: Module 4 5

Generalization p p p Relationship between general thing (parent) and more specific thing (child)

Generalization p p p Relationship between general thing (parent) and more specific thing (child) Child “is-a-kind-of” parent. Child inherits attributes and operations of parent. generalization Shape Rectangle Circle Polygon leaf class Square Lawrence Chung base class CS 6359. 0 T 1: Module 4 6

Associations (UML) p Represent conceptual relationships between classes direction indicator: relationship name Professor *

Associations (UML) p Represent conceptual relationships between classes direction indicator: relationship name Professor * how to read relation name teaches teacher 1. . * class role names Lawrence Chung Course CS 6359. 0 T 1: Module 4 Multiplicity defines the number of objects associated with an instance of the association. Default of 1; Zero or more (*); n. . m; range from n to m inclusive 7

Associations - In Other OOAD Associations may be binary, ternary, or higher order binary

Associations - In Other OOAD Associations may be binary, ternary, or higher order binary association Professor teaches Course Fusion Style Ternary association Language Project Person How are these represented in UML? Lawrence Chung CS 6359. 0 T 1: Module 4 8

Associations – A Question p How would you model the following situation? “You have

Associations – A Question p How would you model the following situation? “You have two files, say Homework 1 and My. Pet, where Homework 1 is accessible only by you, but My. Pet is accessible by anybody. ” You could create two classes, File and User. Homework 1 and My. Pet are files, and you are a user. Approach 1: Now, would you associate the file access right with File? Approach 2: Or, would you associate the file access right with User? Lawrence Chung CS 6359. 0 T 1: Module 4 9

Associations – Links UML has links and associations whose ideas originate largely from OMT

Associations – Links UML has links and associations whose ideas originate largely from OMT and also from Fusion to a certain extent. p OMT has links n p OMT has association n p p link is a physical or conceptual connection between object instances. E. g. , My. Pet “is-accessible-to” by any user. describes a group of links with common structure and common semantics Inherently bi-directional Often implemented as pointers in programming languages In other advanced OOAD notations, such as RML (Requirements Modeling Language), relationships including associations are treated fully and uniformly as classes. And as such they can have links as instances. Lawrence Chung CS 6359. 0 T 1: Module 4 10

Associations – UML Links n n link is a semantic connection among objects. A

Associations – UML Links n n link is a semantic connection among objects. A link is an instance of an association class association name Worker 1. . * works for employee +set. Salary( s : Salary) +set. Dept( d : Dept) w : Worker Named object Lawrence Chung assign(development) link CS 6359. 0 T 1: Module 4 * employer class Company : Company Anonymous object 11

Associations – Link Attributes p Link Attributes The most compelling reason for having links

Associations – Link Attributes p Link Attributes The most compelling reason for having links and attributes is for-many-tomany relationships File User access permission p link attribute UML Association Class File 1. . * * User class Access. Right access permission Lawrence Chung CS 6359. 0 T 1: Module 4 association class 12

Associations - Aggregation - structural association representing “whole/part” relationship. - “has-a” relationship. part Department

Associations - Aggregation - structural association representing “whole/part” relationship. - “has-a” relationship. part Department multiplicity 1. . * Company association Lawrence Chung whole CS 6359. 0 T 1: Module 4 aggregation 13

Modeling Techniques Simple dependencies p Single inheritance p Structural relationships p Lawrence Chung CS

Modeling Techniques Simple dependencies p Single inheritance p Structural relationships p Lawrence Chung CS 6359. 0 T 1: Module 4 14

Modeling Simple Dependencies p The most common dependency between two classes is one where

Modeling Simple Dependencies p The most common dependency between two classes is one where one class uses another as a parameter to an operation. Create dependency pointing from class with operation to parameter. Using relationship Course. Schedule add. Course(c : Course) remove. Course(c : Course Usually initial class diagrams will not have any significant number of dependencies in the beginning of analysis but will as more details are identified. Lawrence Chung CS 6359. 0 T 1: Module 4 15

Modeling Single Inheritance p p p Look for common responsibilities, attributes, and operations that

Modeling Single Inheritance p p p Look for common responsibilities, attributes, and operations that are common to two (2) or more classes. If necessary, create a new class to assign commonalities. Specify that the more-specific classes inherit from the more-general. Security is-a-kind-of relationship Cash. Account interest. Rate present. Value() history() Stock Lawrence Chung abstract Bond present. Value() Preferred. Stock In UML, abstract classes and their operations would be italicized. But not in Rational Rose present. Value() Common. Stock CS 6359. 0 T 1: Module 4 16

Modeling Single Inheritance (cont’d) p Abstract n n p Abstraction—the essential characteristics of a

Modeling Single Inheritance (cont’d) p Abstract n n p Abstraction—the essential characteristics of a thing. Abstract class—cannot be instantiated. Abstract method—has no implementation defined (i. e. , no method body). Depicted in italics or with stereotypes. Concrete n n Not abstract. Can have instances. Lawrence Chung CS 6359. 0 T 1: Module 4 17

Modeling Structural Relationships p Considering a bunch of classes and their association relationships School

Modeling Structural Relationships p Considering a bunch of classes and their association relationships School 1 has 1. . * Department 0. . 1 1. . * 5 assigned to 5 member * 1. . * attends 4 Student * * 1. . * 3 teaches 1. . * Course 1 chairperson Instructor * The model above is from Rational Rose. How did the composite symbol ( )get loaded versus the aggregation? Use the Role Detail and select aggregation and then the “by value” radio button. Lawrence Chung CS 6359. 0 T 1: Module 4 18

Modeling Structural Relationships Composite Aggregation Body Car Liver Heart Wheel Engine Composite is a

Modeling Structural Relationships Composite Aggregation Body Car Liver Heart Wheel Engine Composite is a stronger form of aggregation. Composite parts live and die with the whole. Lawrence Chung CS 6359. 0 T 1: Module 4 19

Modeling Structural Relationships p p Specify an association to create a navigation path between

Modeling Structural Relationships p p Specify an association to create a navigation path between two objects (in either direction). Specify an association if two objects interact with each other beyond operation arguments. How do you know that objects of one class must interact with another class? • Review the scenarios that were derived from Use Cases. • CRC cards seem much less used in practice. . p p Specify multiplicity; 1 is assumed. (Error in text on implicit default. ) Specify aggregation when one of the classes represents a whole over the other classes. Lawrence Chung CS 6359. 0 T 1: Module 4 20

Hints & Tips p Modeling relationships n n n p Use dependencies when relationship

Hints & Tips p Modeling relationships n n n p Use dependencies when relationship is not structural. Use generalization with “is-a” relationship. Don’t introduce cyclical generalizations. Balance generalizations - Not too deep, not too wide. Use associations where structural relationships exist. Drawing a UML relationship n n Use rectilinear or oblique lines consistently. Avoid lines that cross. Show only relationships necessary to understand a particular grouping of things. Elide redundant associations. Lawrence Chung CS 6359. 0 T 1: Module 4 21

Summary p Relationships n n n p Dependency Generalization Association -Links and Link Attributes

Summary p Relationships n n n p Dependency Generalization Association -Links and Link Attributes -Aggregation Modeling techniques n n n Simple dependencies Single inheritance Structural relationships Lawrence Chung CS 6359. 0 T 1: Module 4 22

Points To Ponder p p p p Show that cyclical generalizations lead to symmetric

Points To Ponder p p p p Show that cyclical generalizations lead to symmetric relationships between two classes. Use a couple of examples. Why are cyclical generalizations not desirable? Show that associations do not lead to transitive relationships but aggregations do. Use a couple of examples. Express compositions using FOL (first-order logic). Show that two classes C 1 and C 2 can be such that C 2 is a subclass of C 2 but at the same time C 2 is an aggregation of C 1. Use a couple of examples. Show that two association classes can be related to each other, for example, through an association. Can you give a counter-example to the statement “Composite parts live and die with the whole”? Or when would the statement not hold? Lawrence Chung CS 6359. 0 T 1: Module 4 23