Você está na página 1de 26

Problem Solving

Class Collaboration and Relationship

Object Collaboration
Sends message

Complete this assignment

(Responsibility of the student)

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-

OOP Orientation INTERNAL

Lets examine an example - Heavy Bricks


BobworksparttimeforHeavybricksCorporation.InHBCspayrollthereare004 parttimeworkers.TomisBobsmanager.Thewageforparttimeworkersispaid every week based on the hours they worked. The rate of pay is different for different workers. The date and time of the shifts are punched in a time card by the workers. Each shift is defined by date ,start time and finish time .From the start & finish times the duration, of the shift can be worked out. By adding up the durations of all the shifts in a timecard, the total time worked by a worker, and hence the wages due, can be calculated.

Heavy Bricks wants to automate the payroll system Can you help?

Think in Objects >>>

-2-

OOP Orientation INTERNAL

Bobs Time Card

EMPLOYEE
EMPLOYEE NAME :
DATE START TIME

TIME

CARD

Bob Smith
FINISH TIME DURATION

06/04/09 07/04/09 08/04/09

6:30 6:00 14:00

14:00 14:00 22:00

7:30 8:00 8:00

09/04/09
10/04/09

22:00
22:30

6:30
7:30
Weekly Total

8:30
9:00

41

-3-

OOP Orientation INTERNAL

Identify the objects

Start Time, End Time

Dates worked

HeavyBricks Payroll

Bobs Timecard

Bob

Shifts Bob worked

Tom

-4-

OOP Orientation INTERNAL

Classes

Time

Date

Payroll

Timecard

Part Time Worker Shift

Manager

Both are employees

-5-

OOP Orientation INTERNAL

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

Manager Knows name , Employees who reports to.

Part Time Worker Can calculate the wage. Adds shift to the time card Knows name, pay rate , manager

-6-

OOP Orientation INTERNAL

Collaborates with

Payroll Time Date >> Part Time Worker

Timecard

Shift >> Date >> Time

Part Time Worker >> Time Card >> Manager Manager >> Part Time Worker

>> Shift

-7-

OOP Orientation INTERNAL

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-

OOP Orientation INTERNAL

How to find Associations


Bob needs to maintain his timecard (at least for a week) to get his Salary If a relationship between two collaborating objects needs to be preserved for some duration (it could be milliseconds or years), you can consider them for association. If a candidate collaboration satisfies the above condition and /or is derived from the common associations given below you can name the relationship an association.
Cancellation Reservation Flight Reservation Square Board Customer Payment Item Shelf Product Description Item Sale Register Cashier Store Department Store Player-Football City City [A is a transaction related to another transaction B] [A is a product / service for a transaction B] [A is a physical or logical part of B] [A is a role related to a transaction B] [A is physically or logically contained in B] [A is a description of B] [A is known/recorded in B] [A is a member of B] [A is a subunit of B] [A uses or manages or owns B] [A is next to B]

-9-

OOP Orientation INTERNAL

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

Note : Multiplicity is context dependent

0..1 1

Navigability : From where to where you read the association ?

0..* * 1..*

Store

Stocks 1

Item *

0..4 5..15

- 10 -

OOP Orientation INTERNAL

Visibility of Associated Classes.


Classes that cooperate with each other, or are associated with each other, need to know each others to message back and forth ! For. E.g. Timecard must be visible to Part Time Worker so that he can find the total hours he worked in a week.

- 11 -

OOP Orientation INTERNAL

Association Demo Simple Association

Part Time Worker

add Shifts 1

Timecard 1

- 12 -

OOP Orientation INTERNAL

Association Demo - Multiplicity


Part Time Worker
Add Shifts 1

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 -

OOP Orientation INTERNAL

Unary & Binary Association


Binary Association implies symmetry; both classes know each other and can message each other.

1 Manager

Reports to

*
Worker

Manager knows who all reports to him Worker knows his manager

- 14 -

OOP Orientation INTERNAL

Multiple Associations - Demo


Two classes may have multiple associations between them. A shift starts at a time and finishes at another time

1 Shift 1

Starts at

1 1 Time

Finishes at

- 15 -

OOP Orientation INTERNAL

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 *

Employment Salary Joining Date

- 16 -

OOP Orientation INTERNAL

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 -

OOP Orientation INTERNAL

Aggregation Definition & Representation


Aggregation is a type of association which loosely suggests a whole part relationship.

Time Card has Shifts

To test for aggregation is to see if there is a whole/part relationship between two classes(hasa).Asynonymforthisispart-of.

- 18 -

OOP Orientation INTERNAL

Composition Composite Aggregation


Department is a part of university, it suggests that university is an aggregation of departments.Butdepartmentscannotexistunlessitbelongstoanuniversity.Itsa stronger aggregation composite aggregation/composition.

Consider Professor and Department .Even though Professor is a part of a department, Professorcanexistwithoutbelongingtoadepartment.Itsanaggregationrelationship.

Composition, also known as composite aggregation, is a strong kind of whole-part aggregation.

- 19 -

OOP Orientation INTERNAL

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 -

OOP Orientation INTERNAL

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 -

OOP Orientation INTERNAL

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

Demo: University Professor Relationship

- 22 -

OOP Orientation INTERNAL

Dependency Another example


In an e-commerce application, a Wishlist class depends on a Product class because the Wishlist class uses the Product class as a parameter for an add operation. In a class diagram, a dependency relationship points from the Wishlist class to the Product class. As the following figure illustrates, the Wishlist class is, therefore, the client, and the Product class is the supplier.

A change to the Product class might require a change to the Wishlist class.

- 23 -

OOP Orientation INTERNAL

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

OOP Orientation INTERNAL

Happy Learning !

Você também pode gostar