Escolar Documentos
Profissional Documentos
Cultura Documentos
Object Collaboration
Sends message
For an object to send messages to another object, it must have access to that object or there should be some relationship between their classes.
-1-
Heavy Bricks wants to automate the payroll system Can you help?
-2-
EMPLOYEE
EMPLOYEE NAME :
DATE START TIME
TIME
CARD
Bob Smith
FINISH TIME DURATION
09/04/09
10/04/09
22:00
22:30
6:30
7:30
Weekly Total
8:30
9:00
41
-3-
Dates worked
HeavyBricks Payroll
Bobs Timecard
Bob
Tom
-4-
Classes
Time
Date
Payroll
Timecard
Manager
-5-
Responsibilities
Time Date Can compute the difference between two times Knows the hours and minute of a time. Timecard Knows the day, month and year of a date
Payroll Knows the employees and their wages Interpret the user input and controls
Can add shifts Can find the total time worked Knows the shifts worked
Shift Can find the length of a shift Knows the date , in time and out time of the shift
Part Time Worker Can calculate the wage. Adds shift to the time card Knows name, pay rate , manager
-6-
Collaborates with
Timecard
Part Time Worker >> Time Card >> Manager Manager >> Part Time Worker
>> Shift
-7-
Close Collaboration
In the Heavy bricks exercise you identified that a Part Time Worker closely collaborates with Timecard to calculate his wage. Such, close object collaboration means their classes are related through an association. Associations are the most generic of the relationships between classes that indicates some meaningful and interesting connection.
-8-
-9-
Representing an association
Naming an Association : Name an association using a ClassName-VerbPhrase-ClassName format Player-Square [Player - Has - Square] x [ Player Is On - Square] Register Sale [Register RecordsCurrent -Sale ] Multiplicity Expression : Defines how many instances of class A can be associated with one instance of a class B. Possible Multiplicity Values
Notation Meaning Zero or one One only Zero or more Zero or more One or more Six only Zero to Four Five to Fifteen
0..1 1
0..* * 1..*
Store
Stocks 1
Item *
0..4 5..15
- 10 -
- 11 -
add Shifts 1
Timecard 1
- 12 -
Timecard 1 1
Shift
Punched in
Multiplicity is context dependent : For. E.g. A Person can work only for one company or a person can be employed by many companies
- 13 -
1 Manager
Reports to
*
Worker
Manager knows who all reports to him Worker knows his manager
- 14 -
1 Shift 1
Starts at
1 1 Time
Finishes at
- 15 -
Association Class
In the Heavy Bricks example, consider that a Person can be employed by more than one company at a time. The presence of many to many relationship indicates we can to treat the association itself as a class.
An association class might be useful if some attributes are related to an association and its lifetime is dependant on association.
Company
employs
Person *
- 16 -
Aggregate Objects
An aggregate object is one which contains other objects For E.g. Time Card contains Shifts Hand has fingers Board has Squares. Car Engine It can be 1. A physical containment (like the car & engine). 2. more abstract (e.g. Club and Members).
Being an aggregated object of some class means that objects of the containing class can message objects of contained class.
- 17 -
To test for aggregation is to see if there is a whole/part relationship between two classes(hasa).Asynonymforthisispart-of.
- 18 -
Consider Professor and Department .Even though Professor is a part of a department, Professorcanexistwithoutbelongingtoadepartment.Itsanaggregationrelationship.
- 19 -
Composition Implies
1.An instance of the part (such as a square) belongs to only one composite instance (such as one board) 2.The part must always belong to a whole (no free floating fingers) 3.The whole is responsible for the creation and deletion of its parts (either by itself or collaborating with other objects). In the relationship between University and Department , University class instance will always have at least one Department class instance. Departmentinstancedoesnthaveexistencewithoutbelongingtoauniversityinstance. Because the relationship is a composition relationship, when the University instance is removed/destroyed, the Department instance is automatically removed/destroyed as well. Another important feature of composition aggregation is that the part class can only be related to one instance of the parent class (e.g. Same department instance cannot belong to two universities).
- 20 -
What if
A Class (Client) has knowledge of another Class (Supplier) and that a change in the supplier could affect the client.
Client Class depends on Supplier Class, this relationship is called Dependency.
Itsabroadrelationship.Manytypesofdependency 1.Having an attribute of supplier type (Partime Worker & Timecard) 2.Sending a message to a supplier; the visibility to the supplier could be an attribute, a parameter variable, a local variable, a global variable. 3.Receiving a parameter of the supplier type 4.The supplier is a super class or interface Some of these are covered by association, aggregation, composition, inheritance
- 21 -
Dependency IF
A Class is a parameter variable of a method of another Class A Class is a local variable of a method of another Class
Notation
- 22 -
A change to the Product class might require a change to the Wishlist class.
- 23 -
References
http://download.oracle.com/javase/tutorial/java/concepts/ http://www.ipipan.gda.pl/~marek/objects/TOA/oobasics/oobasics.html http://articles.sitepoint.com/article/oriented-concepts-java-1 http://www.codeproject.com/KB/architecture/OOP_Concepts_and_manymore.aspx#Genera lization
Happy Learning !