Top Banner
Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014
30

Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Dec 14, 2015

Download

Documents

Tyler Kinyon
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Class Relationships Part

1: Composition and Association

CS 21a: Introduction to Computing I

First Semester, 2013-2014

Page 2: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Class Relationships

►More complex programs require multiple classes

► It is typical for objects to have fields that refer to other objects

► In class A, there may be a field whose type is class B► There is a class relationship between A and

B►Examples of class relationships

► Composition or Aggregation► Association

Page 3: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Object Composition

►Objects can be composed of other objects

►Have references to "parts" of the class as fields of the class

►Objects can create instances of other objects

►Also called aggregation

Page 4: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Encapsulation

►The idea of "hiding" implementation details

►What’s more important is the interface

►Users don’t need to know how a method works, just that it’s there and it works

►Objects know how to handle themselves …►users don’t need to know

Page 5: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Encapsulation

►Data should be hidden with the object that it belongs to

►Changes to data should be done via methods of object that contains the data

►Again … objects should know how to handle the data

►Allows the object’s programmer to change data representation

►This is why we make fields private

Page 6: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Bank Example► A Bank encapsulates a set of BankAccount

objects►What’s important is the external interface►Users don’t need to know what goes on

inside the Bank, and Bank doesn’t need to know what goes on inside BankAccount

getBalance( "marsha")

withdraw( "john", 200 )

Page 7: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Bank and BankAccount

BankAccount balance

1000

BankAccount balance

2000

BankBankAccount john

BankAccount marsha

Page 8: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Object Composition in Javapublic class Bank {

private BankAccount john;

private BankAccount marsha;

public Bank() {

john = new BankAccount( 1000 );

marsha = new BankAccount( 2000 );

}

public void deposit(String name, double amt) {

if ( name.equals( "john" ) )

john.deposit( amt );

...

} ...

}

There are BankAccount fields in Bank

The fields are instantiated in Bank’s constructor

Bank has its own deposit method that calls BankAccount’s deposit method on the appropriate object

Page 9: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Using a Bank Object

Bank b = new Bank();

b.deposit( "john", 200 );

b.withdraw( "marsha", 100 );

System.out.println( b.getBalance( "john" ) );

System.out.println( b.getBalance( "marsha" ) ); Prints:

12001900

Page 10: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Object Interaction

BankAccount balance

1000

BankAccount balance

2000

BankBankAccount john

BankAccount marsha

deposit( "john", 200 )

deposit( 200 )

Calling deposit on the Bank object causesdeposit to be called on a BankAccount object

Page 11: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

The whole manages its parts

►In effect, Bank is a manager of BankAccounts

►Transactions are carried out through the Bank object but ultimately uses/affects a BankAccount object

►The one calling Bank’s methods does not even need to know about the BankAccount class this is exactly what encapsulation is about!

Page 12: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Composition with Array(List)s

►An object can be composed of a fixed number of other objects

►A fixed number of fields can implement that composition

►But in general, an object may be composed of an arbitrary number of instances of another object

Page 13: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Composition with Array(List)s

public class Bank

{

private ArrayList<BankAccount> accounts;

public Bank()

{

accounts = new ArrayList<BankAccount>();

}

public void openAccount( String name,

double init )

{

accounts.add( new BankAccount(name, init) );

}

...

There is an ArrayList of BankAccount objects in Bank

The ArrayList is instantiated in Bank’s constructor

The actual BankAccounts are instantiated when they are needed.

Page 14: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Composition with Array(List)s

...

public void deposit( String name, double amt )

{

BankAccount acct = find( name );

if( acct != null )

acct.deposit( amt );

}

...

}

Deposit now requires a search for the correct BankAccount instance (done with a loop instead of hard-coded if-else)

Page 15: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Object Association

►Association: a weaker kind of relationship

►Examples:► Borrower and Book in a library system► Student, Class, Teacher in a university

system► WaterTank and Faucet

Page 16: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

WaterTank-Faucet Example

►A WaterTank object has methods that cause it to be filled up with water or to dispense water

►A Faucet object is connected to a WaterTank and has methods to dispense or drain water

►Faucet needs a way to connect/associate to a WaterTank object► Note: we can connect several faucets to a

single water tank

Page 17: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

WaterTank-Faucet Association

►Option 1: create WaterTank object, create Faucet object(s), and call a method on Faucet:

w = new WaterTank();f1 = new Faucet();f2 = new Faucet();f1.connect( w ); f2.connect( w );

►Option 2: Faucet’s constructor has a WaterTank parameter

w = new WaterTank();f1 = new Faucet( w ); f2 = new Faucet( w );

Page 18: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

WaterTank and Faucet

f1: Faucet

WaterTank tank

f2: Faucet

WaterTank tank

WaterTank

double waterLeft100.0

Page 19: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Object Association in Javapublic class Faucet

{

private WaterTank tank;

public Faucet( WaterTank w )

{

tank = w;

}

...

public void connect( WaterTank w )

{

tank = w;

} ...

}

The association is represented by a WaterTank field

The field can be set in the constructor…

…or in a method

Page 20: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Object Interaction

f1: Faucet

WaterTank tank

f2: Faucet

WaterTank tank

WaterTank

double waterLeft100.0

dispense( 20.0 )

flush()

dispense( 20.0 )

dispense( 80.0 )

Page 21: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Object Interactionpublic class Faucet{ private WaterTank tank; public Faucet( WaterTank w ) { tank = w; } … public void dispense( double

amt ) { tank.dispense( amt ); } public void flush() { tank.dispense(

tank.getWaterLeft() ); }}

public class WaterTank

{

private double waterLeft = 0;

...

public void fillTank() ...

public void dispense( double amt )

{

waterLeft = waterLeft - amt;

}

public double getWaterLeft()

{

return waterLeft;

}

}

Page 22: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Using Faucet andWaterTank Objects

WaterTank w = new WaterTank();WaterTank x = new WaterTank();w.fillTank(); // fills tank to capacity, say 100 gallonsx.fillTank(); // fills tank to capacity, say 100 gallonsFaucet f1 = new Faucet( w );Faucet f2 = new Faucet( w );f1.dispense( 20 );f2.flush();f1.connect( x );f1.dispense( 50 );System.out.println( w.getWaterLeft() );System.out.println( x.getWaterLeft() );

Prints:050

Page 23: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Composition versus Association

►In both cases, there appears to be a one-to-many relationship►One Bank, many BankAccounts►One WaterTank, many Faucets

►Why is it not correct to say that the WaterTank is composed of Faucets?

Page 24: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Composition versus Association

►Composition: The one manages the many► Implies whole-part relationship► The body is composed of cells, but the

cells do not need to be aware of the body

►Association: The many use the one► Does not imply a whole-part relationship► Many parasites can feed off a host, but

the host does not need to be aware of the parasites

Page 25: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Composition versus Association

►Composition: the whole is dependent on the parts

►Association: there is no such dependency

Page 26: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Composition

► It doesn’t make sense to have Banks if there is no concept of BankAccount (the class)► Although it’s perfectly okay to start a

Bank without any BankAccounts (the instances) yet

► In other words, if BankAccount is removed from the system, it no longer makes sense to retain Bank.

Page 27: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Association

►It still makes sense to have a WaterTank even if Faucets were never invented

►Faucets don’t need to be connected to WaterTanks. They can be connected to pipes, for example.

►In other words, if Faucet is removed from the system, it’s perfectly fine to retain WaterTank, and vice versa.

Page 28: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

An Integrated Example

►Grocery environment►Products are stocked and sold in the

grocery►Cashiers are front-end objects that carry

out a sale through a back-end price-and-stock Manager object► Multiple cashiers are associated to the

Manager object►The Manager object aggregates Product

objects (where prices and stock levels are stored)

Page 29: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Grocery Example

c1: Cashier

c2: Cashier

Manager

product apples

product oranges

product pomelos

apples:Product

oranges:Product

pomelos:Product

Transactions are carried out through the Cashier objects

Product objects may be updated as a result

Prices are checked and purchase requests are made thru the Manager object

Page 30: Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester, 2013-2014.

Exercise

►Can you identify the class relationships present in the Project 1 solution?