Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Analysis: Associations. 2 Object Oriented Modeling BUAD/American University Class Relationships u Classes have relationships between each.

Similar presentations


Presentation on theme: "Object Oriented Analysis: Associations. 2 Object Oriented Modeling BUAD/American University Class Relationships u Classes have relationships between each."— Presentation transcript:

1 Object Oriented Analysis: Associations

2 2 Object Oriented Modeling BUAD/American University Class Relationships u Classes have relationships between each other. u These relationships include: –Associations –Aggregation (Whole - Part) –Generalization (Inheritance)

3 3 Object Oriented Modeling BUAD/American University Associations u An association is a connection between classes. u Associations allow objects to be “Aware” of each other u Associations very important when modeling complex and large systems u Associations have long been used in data and database modeling u UML defines association as a relationship that describes a set of links u A Link is a semantic connection (relationship) between objects of the class involved in the association.

4 4 Object Oriented Modeling BUAD/American University Example of associations u An instructor teaches students. –Therefore there is an association between the instructor and students u Two persons are married u Employees works for a manager u A bank has customers u Others that you can think of???

5 5 Object Oriented Modeling BUAD/American University Why Model Associations? Two perspectives: u At Conceptual level: –Associations represent conceptual relationships between the classes. –They help us to structure and understand the problem. u At a Specification or Implementation level –Associations describe how objects will know about each other –How objects will gain access to one another. –Describe the needed behavior of objects. –For example, an association between a person and the cars they own may imply that the person class should have an operation: getCars

6 6 Object Oriented Modeling BUAD/American University An association may represent multiple links Class Association Object Links etc Advises

7 7 Object Oriented Modeling BUAD/American University Normal Associations u A connection between classes u Drawn as solid line u Association has a name(s), normally a static verb or noun, that expresses the nature of the relationship – eg Teaches, owns, works for etc. u Names should reflect the problem domain as much as possible. u Can have navigable associations by adding arrow at the end of the association, indicating that the association can be used only in one direction

8 8 Object Oriented Modeling BUAD/American University Multiplicity u Describes how many objects may participate in a given relationship. u It specifies the number of object instances of one class that may relate to a single instance of an associated class. –How many professors can teach how many students in a course? –How many cars can an owner have?. –How many owners can a car have? u Used primarily to indicate lower and upper bounds for participating objects.

9 9 Object Oriented Modeling BUAD/American University Multiplicity Notation u Appears at each end of the association: –Zero to One (0..1) –Zero to many (0..* or just *) –One to many (1..*) –Specific numbers such as 2, or specific range 0..5 –If no multiplicity is specified, one (1) is the default

10 10 Object Oriented Modeling BUAD/American University Multiplicity Notation An A is always associated with one B An A is always associated with zero or one Bs An A is always associated with zero, one, or more Bs An A is always associated with one or more Bs

11 11 Object Oriented Modeling BUAD/American University Association Examples Advises

12 12 Object Oriented Modeling BUAD/American University One to Many vs. Many to Many Associations u Should this association be a: one to many or many to many relationship?

13 13 Object Oriented Modeling BUAD/American University Professor to student: Many to many or one to many? u It depends on the context of the problem u Are you trying to model a teaching or advising relationship or both? u Are you trying to model the relationship in one course section? u Or you trying to keep track of the relationship across multiple courses, semesters etc. u Where would the students grade be recorded?

14 14 Object Oriented Modeling BUAD/American University Example of one to many and many to many

15 15 Object Oriented Modeling BUAD/American University Eliminating Many to Many Relationships u Many to many relationships may represent situations where information needs to be represented and stored about the association. But where is it stored? u For example, if many car owners are associated with many cars, where would the date of ownership be placed? In owner? In Car? u If there is a many to many relationship between a professor and students, where is the specific information concerning the course and date indicating that a relationship exists?

16 16 Object Oriented Modeling BUAD/American University Eliminating Many to Many Relationships: Example 1 Car +getNumberOfCars() : int +getOwner(Owner : String, Address : String) +calculateRegFee() : Float +getMake() : maketype -NumberOfCars : int -registrationNumber : String -weight : int -vinNumber : String -year : int -make : maketype Person +getAge() : int +getFullName() : String +getFullAddress() : String -lastName : String -firstName : String -streetAddress : String -city : String -state : string -zipcode : string -phoneNumber : String -faxNumber : String -email : String -dateOfBirth : Date * * Owns Person +getAge() : int +getFullName() : String +getFullAddress() : String -lastName : String -firstName : String -streetAddress : String -city : String -state : string -zipcode : string -phoneNumber : String -faxNumber : String -email : String -dateOfBirth : Date Car +getNumberOfCars() : int +getOwner(Owner : String, Address : String) +calculateRegFee() : Float +getMake() : maketype -NumberOfCars : int -registrationNumber : String -weight : int -vinNumber : String -year : int -make : maketype Purchase +GetDate() +GetAmount() -Date : String -AmountPaid : Float 1 * * 1

17 Object Oriented Analysis: Aggregation

18 18 Object Oriented Modeling BUAD/American University Aggregation u A special form of association u Indicates the relationship between classes is Whole-Part (e.g. A Car has wheels, and engine, a School has Departments etc.) u Used to help describe different levels of abstraction u By representing aggregation relationships can partition a very complex class and assign operations and attributes to the individual classes within it. u Words like “Consists of”, “Contains”, “Is part of” describe an aggregation relationship

19 19 Object Oriented Modeling BUAD/American University Aggregation Notation u Uses a hollow diamond at whole side of relationship to represent aggregation u Uses same notation as associations for multiplicity

20 20 Object Oriented Modeling BUAD/American University Examples of Aggregation

21 21 Object Oriented Modeling BUAD/American University Aggregation Example 2

22 22 Object Oriented Modeling BUAD/American University Example of Aggregation in Loan Processing system What are the implications of the relationships and multiplicity in this aggregation example?

23 23 Object Oriented Modeling BUAD/American University Department and Employee Example

24 24 Object Oriented Modeling BUAD/American University When to use Aggregation vs. Attributes to model an Entity u When should an “entity” be modeled as simple attribute of a class and when should it be modeled as a Class that is part of the Whole? –When the Entity is simple, e.g. Name, use an attribute –When the Entity is complex, Address, use Aggregation –If you want to provide reuse or enforce standard formats use aggregation u Early in analysis, you may not be able to determine whether an entity should be an attribute or part. Model as attribute, as analysis progresses and more details are discovered, if appropriate, make the attribute a separate part class.

25 Object Oriented Analysis: Inheritance (Generalization)

26 26 Object Oriented Modeling BUAD/American University Inheritance (Generalization) Definition u Inheritance is a mechanism whereby one class of objects can be defined as a special case of a more general class (it automatically includes the operations, associations and attribute definitions of the general class) u Subclasses “inherit” operations, associations and attributes definitions from superclasses u Referred to as an “Is-A” relationship

27 27 Object Oriented Modeling BUAD/American University Inheritance (Generalization) Inheritance allows a class to incorporate all or part of the definition of another class as part of its own definition.

28 28 Object Oriented Modeling BUAD/American University Reasons for Inheritance u Helps to organize analysis u Design by difference u Special cases share all characteristics of more general cases (share the code and data structures) u Therefore more reuse

29 29 Object Oriented Modeling BUAD/American University Inheritance Hierarchy Motor Vehicle 4-door 2-door

30 30 Object Oriented Modeling BUAD/American University Approaches for using Inheritance include u Generalization: –Acting as a mechanism for expressing commonality between two Classes –More common in first version of the design u Specialization: –Used to specify that one class is a special type of another class –More common in successive versions of the system

31 31 Object Oriented Modeling BUAD/American University Inheriting attributes and operations u Attributes and operations with: –Public visibility in the superclass will be public in the subclass. –Private visibility in the superclass will be inherited, but not accessible within the subclass. –Protected visibility in the superclass will be accessible in both the superclass and subclass and from other classes in the same file.

32 32 Object Oriented Modeling BUAD/American University Class B inherits from Class A Superclass Base class Subclass Derived class

33 33 Object Oriented Modeling BUAD/American University Inheritance Workshop u Can we build inheritance between the Classes below u Where should attributes and operation be placed?

34 34 Object Oriented Modeling BUAD/American University Multiple level inheritance u A class can be both a superclass and a subclass u i.e., a class can both inherit from one class and be inherited by other classes

35 35 Object Oriented Modeling BUAD/American University Another example with multiple level inheritance


Download ppt "Object Oriented Analysis: Associations. 2 Object Oriented Modeling BUAD/American University Class Relationships u Classes have relationships between each."

Similar presentations


Ads by Google