1
Lect 3
27-08-2024
Qualified Association
2
Qualified Association
 Allows us to express uniqueness, eg no duplicate
objects allowed in an association…
Implemented by hash
tables, maps, or dictionaries.
 How to read?
 There exists atmost one file for each instance of
filename in the directory .
Directory File
file
name
Class Student
Roll
1 0..1
1
0..1
Read
Class Student
*
belongs
3
Qualified Association

Qualifier hints at setting up efficient
access to linked objects:
 For example, access accounts based only on
the account number;
 Implementation should avoid a linear search
through all accounts.
Account
accno
0..1
1
Bank
4
Setting up Qualified Association --
An Example

An order has of many Order lines.

How do you represent: There is at most one Order
Line in the Order for each instance of Product.
Order Order Line
Product
1
0..1
Line item
Order Order Line
*
1
contains
5
Player
nickName
0..1
1
League
nickName
Qualified Association…
Player
*
1
League
•The second conveys more information…
•Hints on implementation…
6
Qualified Association: Implementation
public class League {
private Map players=new HashMap();
public void addPlayer
(String nickName, Player p) {
if(!players.containsKey(nickName))
players.put(nickName, p);
}
}
public class Player {
private League
league;
}
1
Player
nickName
0..1
League
7
Converting to Qualified Association
works for
Person Company
employee employer
Association Name
Role
* 1
employee
works for
Person Company
employer
empId
1
0..1
Assume company assigns each employee a unique empId.
Give qualified association representation…
8
9
Types of Class Relationships
Relation
Association
Generalization Dependency
Aggregation
Binary Association N-ary Association
Composition
10
Overdoing Associations

Avoid unnecessary Associations
1
Person
Name
PersonInfo
Address
E-Mail
Birthday
PersonInfo
Name
Address
E-Mail
Birthday
1
Avoid This… Do This
11
Summary of Implementation of Association

1-to-1 association:
 Role names become attributes

1-to-many association:
 Translate into a Vector or ArrayList

Qualified association:
 Translate into a Map or Hash table
12
Which sentences are true?
a) CheckingAccount implements BankAccount
b) CheckingAccount and SavingAccount are BankAccount
c) CheckingAccount and SavingAccount are associated
d) BankAccount is associated to CheckingAccount
e) SavingAccount can processCheck
f) CheckingAccount has a balance
Quiz
13
Aggregation, Composition, and
Dependence Relationships
Among Classes
14
Aggregation Relationship

Represents whole-part relationship

Represented by a diamond symbol at the aggregator end.

Often indistinguishable from plain association. However:
 Aggregate usually creates the components:
 Aggregate usually invokes the same operations on all its
components.

Usually aggregate is owner of the components:
 But may share with other objects
employs memberOf
Company Club
*
Person 1
*
15
Aggregation: Two Examples
Document Line
1
* Paragraph 1
*
1employs memberOf
Company Club
* Person
1
*
16

An aggregate object contains other
objects.

Aggregation limited to tree hierarchy:
 No circular inclusion relation.
Aggregation cont…
Line
Paragraph
1
*
17
Composition

A stronger form of aggregation
 The whole is sole owner of its part.

A component can belong to only one whole
 The life time of the part is dependent upon the whole.
Circle Point
3..*
1
Polygon
Point
Circle
18
Composition Relationship
Order 1
* Item

Life of item is same as that of order
19
Composition: Alternate Notation
Car
Wheel
4
Engine
1
Door
2
Chassis
1
Axle
1
Steering
1
20
Composition
• An object (component) may be a part of
ONLY one composite.
Whole is responsible for the creation and
disposition of its parts.
Window
Frame
*
1
whole
part
21
Aggregation vs. Composition

Composition:
 Composite and its components
have the same life line.

Aggregation:
 Lifelines are different.

Consider an order object:
 Aggregation: If order items can be changed or
deleted after placing an order.
 Composition: Otherwise.
22
Composition versus Aggregation
1
Order Item
*
Order Item
1
*
Composition
Aggregation
23
Implementing Composition…
public class Car{
private Wheel wheels[4];
public Car (){
wheels[0] = new Wheel();
wheels[1] = new Wheel();
wheels[2] = new Wheel();
wheels[3] = new Wheel();
}
}
Car Wheel
1 4
24
Aggregation: Code Example

An aggregation relationship is usually represented as a data field in the
aggregated class.
public class Name {
/* Data fields */
/*Constructors */
/* Methods */
}
public class Person {
/** Data fields */
private
ArrayList<Name>
name =new
ArrayList<Name>() ;
}
public class
Address {
/* Data fields */
/*Constructors*/
/** Methods */
}
Person
Address
1
*
Name
*
25
Often Inner Classes are Used

If House is used
only in the Person
class:
 Declare it as an
inner class in
Person.
public class Person {
private Name name;
private House house;
...
class House {
...
}
}
26
Implementing Aggregation: Ex 1
import java.util.ArrayList;
public class CarShop{
CarCompany company; Manager manager;
private ArrayList<SalesPerson> people = new
ArrayList<SalesPerson>();
private ArrayList<Car> cars = new
ArrayList<Car>();
} Car Shop
Salesman
1
*
Car
*
27
Implementing Aggregation: Ex 2
Public class Sentence{
String text;
Sentence(String ){text=s;}
}
Public class Paragraph{
private ArrayList <Sentence>
sentList=new
ArrayList<Sentence>();
public addSentence(Sentence s){
sentList.add(s);
}
}
Class Aggregator{
public static void main() {
BufferedReader br=new
BufferedReader(new
InputStreamReader(System.in));
Paragraph p= new Paragraph();
do{
p.addSentence(new
Sentence(br.readLine());
}
}
Pragraph Sentence
1
*
28
Deciding Whether to Use Composition or
Aggregation…

Use composition if:
 Lifetime of part is bound within lifetime of
composite
 There is an obvious physical or logical assembly
 Some properties of composite propagate to parts
(e.g., location)
 Operations applied to composite propagate to parts
(e.g., destruction, movement, etc)
29
Class Dependency
Dependent Class Independent Class
A class may be dependent on another
class due to a variety of reasons.
Any change to the independent class
would necessitate a change to the
dependent class.
30
Dependency

Dependency relationship between two classes X and Y can arise due to a variety of reasons:
 X has an attribute of class Y
 X has a template attribute with a parameter of class Y
 X has a method with an input argument of class Y
 X has a method with an output argument of class Y
 X has of a method containing a local variable of class Y
 Etc.
31
Dependency

Common Dependences are caused by:
 Local variable
 Parameter
 Return value

Example:
Class A { Class B {
B Foo(B x) { …
B y = new B(); …
return y; …
}
} }
32
Dependence – Examples
class MyDependentClass{
void
myFunction1( MyReference
dClass r ) { .. }
MyreferencedClass
myFunction2( .. ) { .. }
void myFunction3( .. )
{ MyReferencedClass m .. }
}
MyDependentClass
att: int
myFunction()
MyReferencedClass
dependence
arrow
33
Association Vs. Aggregation

Is aggregation an association?

Is composition an aggregation?
34
Summary: Association Types

aggregation: "is part of"
 Symbolized by empty diamond

composition: “is made of”
 Stronger version of aggregation
 The parts live and die with the whole
 Symbolized by a filled diamond

dependency: “Depends on”
 Represented by dotted arrow.
1
1
Car
aggregation
Engine
Lottery
Ticket
Random
dependency
Page
Book
composition
*
1
35
Object Association
n n
Class
Generalization
Relationship
Object
Aggregation
Association
0..*
1..*
Object
Composition
Association
0..*
1
UML Class Relation Notation Summary
Will always be “1”
dependency
36
Aggregation Composition
0..*
1..5
Faculty
CourseTeaching
1..*
1
SalesOrder
SalesOrderLineItem
(team-
teaching is
possible)
37
38
Multiplicity Quiz #1
•One Whole is associated with 5 Part A
•One Part A is associated with 1 Whole
•One Whole is associated with 2 PartB
•One PartB is associated with 3 Whole
W
W
PA
PA
PA
PA
PA
PA
PA
PA
PA
PA
W
W
PA
PA
W
W
PB
PB
PB
PB W
W
PB
PB
W
W
W
W
Whole
5
1
2
3
Part A Part B
Class
diagram
Object diagram
Read
39
Class
Relation
Hints

Composition
 B is a permanent part of A
 A contains B
 A is a permanent collection of Bs

Subclass / Superclass
 A is a kind of B
 A is a specialization of B
 A behaves like B

Association (Collaboration)
 A delegates to B
 A needs help from B
 A and B are peers.
40
Class Diagram Inference Based on Text Analysis
(based on Dennis, 2002)

A common or improper noun implies a class e.g. Book

A proper noun implies an object (instance of a class):
CSE Dept, OOSD, etc.

An adjective implies an attribute e.g. price of book

A “doing” verb implies an operation (method)
 Can also imply a relationship e.g. student issues Book

A “having” verb implies an aggregation relationship

An adverb implies an attribute of an operation e.g.
fast loading of image…
41
Faculty & student
Hospital & doctor
Door & Car
Member & Organization
People & student
Department & Faculty
Employee & Faculty
Computer Peripheral & Printer
Account & Savings account
Identify Class Relations
42
Identify Classes & Relations

A square is a polygon

Shyam is a student

Every student has a name

100 paisa is one rupee

Students live in hostels

Every student is a member of the library

A student can renew his borrowed books

The Department has many students
43
Identify Classes & Relations

A country has a capital city

A dining philosopher uses a fork

A file is an ordinary file or a directory file

Files contain records

A class can have several attributes

A relation can be association or generalization

A polygon is composed of an ordered set of points

A programmer uses a computer language on a
project
44
45
Exercise

The B.Tech program of SNU Computer Science
Department:
 comprises of many B.Tech batches.

Each B.Tech batch consists of many B.Tech students.

CSE Department has many listed courses.
 A course may be offered in either spring or Autumn
semesters
 A course is either listed as an elective course or a core
course.
 Each B.Tech students need to credit between 30 to 32
course offerings.
 A student might repeat a course if he/she desires
46
46
Model Solution
*
1
BTech Batch
Student
BTech Program
1 *
Course Credit
Course
30..32
Course
List
1
*
credits
Elective
Core
*
Semester
offered in
Autumn
Spring

Software Designing Qualified Association

  • 1.
  • 2.
    2 Qualified Association  Allowsus to express uniqueness, eg no duplicate objects allowed in an association… Implemented by hash tables, maps, or dictionaries.  How to read?  There exists atmost one file for each instance of filename in the directory . Directory File file name Class Student Roll 1 0..1 1 0..1 Read Class Student * belongs
  • 3.
    3 Qualified Association  Qualifier hintsat setting up efficient access to linked objects:  For example, access accounts based only on the account number;  Implementation should avoid a linear search through all accounts. Account accno 0..1 1 Bank
  • 4.
    4 Setting up QualifiedAssociation -- An Example  An order has of many Order lines.  How do you represent: There is at most one Order Line in the Order for each instance of Product. Order Order Line Product 1 0..1 Line item Order Order Line * 1 contains
  • 5.
  • 6.
    6 Qualified Association: Implementation publicclass League { private Map players=new HashMap(); public void addPlayer (String nickName, Player p) { if(!players.containsKey(nickName)) players.put(nickName, p); } } public class Player { private League league; } 1 Player nickName 0..1 League
  • 7.
    7 Converting to QualifiedAssociation works for Person Company employee employer Association Name Role * 1 employee works for Person Company employer empId 1 0..1 Assume company assigns each employee a unique empId. Give qualified association representation…
  • 8.
  • 9.
    9 Types of ClassRelationships Relation Association Generalization Dependency Aggregation Binary Association N-ary Association Composition
  • 10.
    10 Overdoing Associations  Avoid unnecessaryAssociations 1 Person Name PersonInfo Address E-Mail Birthday PersonInfo Name Address E-Mail Birthday 1 Avoid This… Do This
  • 11.
    11 Summary of Implementationof Association  1-to-1 association:  Role names become attributes  1-to-many association:  Translate into a Vector or ArrayList  Qualified association:  Translate into a Map or Hash table
  • 12.
    12 Which sentences aretrue? a) CheckingAccount implements BankAccount b) CheckingAccount and SavingAccount are BankAccount c) CheckingAccount and SavingAccount are associated d) BankAccount is associated to CheckingAccount e) SavingAccount can processCheck f) CheckingAccount has a balance Quiz
  • 13.
  • 14.
    14 Aggregation Relationship  Represents whole-partrelationship  Represented by a diamond symbol at the aggregator end.  Often indistinguishable from plain association. However:  Aggregate usually creates the components:  Aggregate usually invokes the same operations on all its components.  Usually aggregate is owner of the components:  But may share with other objects employs memberOf Company Club * Person 1 *
  • 15.
    15 Aggregation: Two Examples DocumentLine 1 * Paragraph 1 * 1employs memberOf Company Club * Person 1 *
  • 16.
    16  An aggregate objectcontains other objects.  Aggregation limited to tree hierarchy:  No circular inclusion relation. Aggregation cont… Line Paragraph 1 *
  • 17.
    17 Composition  A stronger formof aggregation  The whole is sole owner of its part.  A component can belong to only one whole  The life time of the part is dependent upon the whole. Circle Point 3..* 1 Polygon Point Circle
  • 18.
    18 Composition Relationship Order 1 *Item  Life of item is same as that of order
  • 19.
  • 20.
    20 Composition • An object(component) may be a part of ONLY one composite. Whole is responsible for the creation and disposition of its parts. Window Frame * 1 whole part
  • 21.
    21 Aggregation vs. Composition  Composition: Composite and its components have the same life line.  Aggregation:  Lifelines are different.  Consider an order object:  Aggregation: If order items can be changed or deleted after placing an order.  Composition: Otherwise.
  • 22.
    22 Composition versus Aggregation 1 OrderItem * Order Item 1 * Composition Aggregation
  • 23.
    23 Implementing Composition… public classCar{ private Wheel wheels[4]; public Car (){ wheels[0] = new Wheel(); wheels[1] = new Wheel(); wheels[2] = new Wheel(); wheels[3] = new Wheel(); } } Car Wheel 1 4
  • 24.
    24 Aggregation: Code Example  Anaggregation relationship is usually represented as a data field in the aggregated class. public class Name { /* Data fields */ /*Constructors */ /* Methods */ } public class Person { /** Data fields */ private ArrayList<Name> name =new ArrayList<Name>() ; } public class Address { /* Data fields */ /*Constructors*/ /** Methods */ } Person Address 1 * Name *
  • 25.
    25 Often Inner Classesare Used  If House is used only in the Person class:  Declare it as an inner class in Person. public class Person { private Name name; private House house; ... class House { ... } }
  • 26.
    26 Implementing Aggregation: Ex1 import java.util.ArrayList; public class CarShop{ CarCompany company; Manager manager; private ArrayList<SalesPerson> people = new ArrayList<SalesPerson>(); private ArrayList<Car> cars = new ArrayList<Car>(); } Car Shop Salesman 1 * Car *
  • 27.
    27 Implementing Aggregation: Ex2 Public class Sentence{ String text; Sentence(String ){text=s;} } Public class Paragraph{ private ArrayList <Sentence> sentList=new ArrayList<Sentence>(); public addSentence(Sentence s){ sentList.add(s); } } Class Aggregator{ public static void main() { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); Paragraph p= new Paragraph(); do{ p.addSentence(new Sentence(br.readLine()); } } Pragraph Sentence 1 *
  • 28.
    28 Deciding Whether toUse Composition or Aggregation…  Use composition if:  Lifetime of part is bound within lifetime of composite  There is an obvious physical or logical assembly  Some properties of composite propagate to parts (e.g., location)  Operations applied to composite propagate to parts (e.g., destruction, movement, etc)
  • 29.
    29 Class Dependency Dependent ClassIndependent Class A class may be dependent on another class due to a variety of reasons. Any change to the independent class would necessitate a change to the dependent class.
  • 30.
    30 Dependency  Dependency relationship betweentwo classes X and Y can arise due to a variety of reasons:  X has an attribute of class Y  X has a template attribute with a parameter of class Y  X has a method with an input argument of class Y  X has a method with an output argument of class Y  X has of a method containing a local variable of class Y  Etc.
  • 31.
    31 Dependency  Common Dependences arecaused by:  Local variable  Parameter  Return value  Example: Class A { Class B { B Foo(B x) { … B y = new B(); … return y; … } } }
  • 32.
    32 Dependence – Examples classMyDependentClass{ void myFunction1( MyReference dClass r ) { .. } MyreferencedClass myFunction2( .. ) { .. } void myFunction3( .. ) { MyReferencedClass m .. } } MyDependentClass att: int myFunction() MyReferencedClass dependence arrow
  • 33.
    33 Association Vs. Aggregation  Isaggregation an association?  Is composition an aggregation?
  • 34.
    34 Summary: Association Types  aggregation:"is part of"  Symbolized by empty diamond  composition: “is made of”  Stronger version of aggregation  The parts live and die with the whole  Symbolized by a filled diamond  dependency: “Depends on”  Represented by dotted arrow. 1 1 Car aggregation Engine Lottery Ticket Random dependency Page Book composition * 1
  • 35.
  • 36.
  • 37.
  • 38.
    38 Multiplicity Quiz #1 •OneWhole is associated with 5 Part A •One Part A is associated with 1 Whole •One Whole is associated with 2 PartB •One PartB is associated with 3 Whole W W PA PA PA PA PA PA PA PA PA PA W W PA PA W W PB PB PB PB W W PB PB W W W W Whole 5 1 2 3 Part A Part B Class diagram Object diagram Read
  • 39.
    39 Class Relation Hints  Composition  B isa permanent part of A  A contains B  A is a permanent collection of Bs  Subclass / Superclass  A is a kind of B  A is a specialization of B  A behaves like B  Association (Collaboration)  A delegates to B  A needs help from B  A and B are peers.
  • 40.
    40 Class Diagram InferenceBased on Text Analysis (based on Dennis, 2002)  A common or improper noun implies a class e.g. Book  A proper noun implies an object (instance of a class): CSE Dept, OOSD, etc.  An adjective implies an attribute e.g. price of book  A “doing” verb implies an operation (method)  Can also imply a relationship e.g. student issues Book  A “having” verb implies an aggregation relationship  An adverb implies an attribute of an operation e.g. fast loading of image…
  • 41.
    41 Faculty & student Hospital& doctor Door & Car Member & Organization People & student Department & Faculty Employee & Faculty Computer Peripheral & Printer Account & Savings account Identify Class Relations
  • 42.
    42 Identify Classes &Relations  A square is a polygon  Shyam is a student  Every student has a name  100 paisa is one rupee  Students live in hostels  Every student is a member of the library  A student can renew his borrowed books  The Department has many students
  • 43.
    43 Identify Classes &Relations  A country has a capital city  A dining philosopher uses a fork  A file is an ordinary file or a directory file  Files contain records  A class can have several attributes  A relation can be association or generalization  A polygon is composed of an ordered set of points  A programmer uses a computer language on a project
  • 44.
  • 45.
    45 Exercise  The B.Tech programof SNU Computer Science Department:  comprises of many B.Tech batches.  Each B.Tech batch consists of many B.Tech students.  CSE Department has many listed courses.  A course may be offered in either spring or Autumn semesters  A course is either listed as an elective course or a core course.  Each B.Tech students need to credit between 30 to 32 course offerings.  A student might repeat a course if he/she desires
  • 46.
    46 46 Model Solution * 1 BTech Batch Student BTechProgram 1 * Course Credit Course 30..32 Course List 1 * credits Elective Core * Semester offered in Autumn Spring

Editor's Notes

  • #11 Not covered in Fall 91 class