1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces...

23
1 Object Oriented Design and UML • Class Relationships – Dependency – Aggregation – Interfaces – Inheritance (not today) • Interfaces • Reading for this Lecture: L&L 7.4 – 7.5

Transcript of 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces...

Page 1: 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance (not today) Interfaces Reading for this Lecture:

1

Object Oriented Design and UML

• Class Relationships– Dependency– Aggregation– Interfaces– Inheritance (not today)

• Interfaces

• Reading for this Lecture: L&L 7.4 – 7.5

Page 2: 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance (not today) Interfaces Reading for this Lecture:

2

Class Relationships• Classes in a software system can have various

types of relationships to each other

• Three of the most common relationships:– Dependency: A uses B

– Aggregation: A has-a B (as in B is an integral part of A)

– Interface: A is B (adjective) or A is-a B (noun)

– Inheritance: A is-a B

• We cover the first three now

• We cover inheritance later

Page 3: 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance (not today) Interfaces Reading for this Lecture:

3

Dependency

• A dependency exists when one class relies on another in some way, usually by invoking the methods of the other

• We've seen dependencies in previous examples and in Projects 1 and 2

• We don't want numerous or complex dependencies among classes

• Nor do we want complex classes that don't depend on others

• A good design strikes the right balance

Page 4: 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance (not today) Interfaces Reading for this Lecture:

Project 2 UML Diagram

Histogram

DiceStats Die

- MIN_ROLL : int- MAX_ROLL : int- MAX_LENGTH : int

+ main(args : String [ ]) : void

- MAX : int- faceValue : int+ Die() + roll() : int+ setFaceValue(value : int) : void+ getFaceValue( ) : int+ toString() : String

- values : int [ ]- minIndex : int- maxIndex : int- maxLength : int

+ Histogram (values : int [ ], minIndex : int, maxIndex : int, maxLength : int)+ drawHor(void) : void+ drawVer(void) : void

Page 5: 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance (not today) Interfaces Reading for this Lecture:

5

Aggregation• An aggregate is an object that is made up of other

objects• Therefore aggregation is a has-a relationship

– A Student has (a) Address object(s) (pg. 321)

• In code, an aggregate object contains references to its component objects as instance data

• - Student has 2 Address refs:private Address homeAddress, schoolAddress;

• The aggregate object itself is defined in part by the objects that make it up: these Addresses are important state for Student

Page 6: 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance (not today) Interfaces Reading for this Lecture:

6

Aggregation

• In the text’s example, a Student object has two Address objects, so Student is an “aggregate”

• See Student.java, Address.java (page 318-319)• An aggregation association is shown in a UML

class diagram using an open diamond at the aggregate end, here the Student end

vs.

• Note there is no arrow point on this connector, but many UML authors do use one

Page 7: 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance (not today) Interfaces Reading for this Lecture:

7

Dependency/Aggregation in UMLStudentBody

+ main (args : String[]) : void

+ toString() : String

Student- firstName : String- lastName : String- homeAddress : Address- schoolAddress : Address

+ toString() : String

- streetAddress : String- city : String- state : String- zipCode : long

AddressDependency shown with this symbol

Aggregation shown with this symbol

Page 8: 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance (not today) Interfaces Reading for this Lecture:

Aggregation

• There are two ways to include the component objects in an object that is an aggregation– For a small constant number of components, can use

parameters in the constructor

public Car(CarPart chassis, CarPart engine)

{ ... }– For a large or indefinite number of components, define an

add method to add them one at a time: adding students to a Section:

public void add(Student aStudent)

{ ... }8

Page 9: 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance (not today) Interfaces Reading for this Lecture:

9

Interfaces

• A Java interface is a collection of constants and method headers with a name that looks like a class name, i.e. the first letter is capitalized

• An interface is used to identify a set of methods that a class will implement

• The class can have other methods too• Example later: • class Lion implements interface Predator• Methods Lion needs to implement: chasePrey,

eatPrey, the predator-related actions• Lion can also have a purr() method

Page 10: 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance (not today) Interfaces Reading for this Lecture:

10

Interfaces

public interface Doable{ // Doable constants public static final boolean DONE = true; public static final boolean NOT_DONE = false;

// Doable required methods (signatures only) public void doThis(); public int doThat();}

interface is a reserved wordNone of the methods inan interface are given

a definition {body}

A semicolon immediatelyfollows each method header

Page 11: 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance (not today) Interfaces Reading for this Lecture:

11

Interfaces, without “public”, “static” or “final” except on whole thing

public interface Doable{ // Doable constants: // automatically public static final boolean DONE = true; boolean NOT_DONE = false;

// Doable required methods // automatically public void doThis(); int doThat();}

Page 12: 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance (not today) Interfaces Reading for this Lecture:

12

Interfaces• An interface name can be either an adjective

(like …able) or a noun (like a class name)

• An interface cannot be instantiated by itself

• A class implements an interface by:– using the Java reserved word implements

– providing an implementation for each abstract method that is defined in the interface

• Classes that implement an interface can also implement their own methods and they usually do

Page 13: 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance (not today) Interfaces Reading for this Lecture:

13

Interfacespublic class Worker implements Doable{

public void doThis () { // whatever }

public int doThat () { // whatever }

// etc.}

implements is areserved word

Each method listedin Doable must be

given a definition, and here “public” IS NEEDED!!

Doable is an adjective

Page 14: 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance (not today) Interfaces Reading for this Lecture:

14

Interfaces In UML

Worker

<<interface>> Doable

+ DONE : boolean+ NOT_DONE : boolean+ doThis( ) : void+ doThat( ) : int

+ doThis( ) : void+ doThat( ) : int

+ doNothing ( ) : void+ doSomething ( ) : void

Each method listedin Doable becomes a method of Worker

Worker can haveother methods

of its own

A “Generalization” arrowis used for “implements”

(and also for “extends” later)

Interface box looks likea class box with

stereotype <<interface>>

Page 15: 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance (not today) Interfaces Reading for this Lecture:

15

Interfaces• A class can implement multiple interfaces• All interface names are listed in the implements clause

• The class must implement all methods in all interfaces listed in the header

class ManyThings implements Interface1, Interface2, ...{ // all methods of all interfaces}

Page 16: 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance (not today) Interfaces Reading for this Lecture:

Example from WikipediaIdea: a Predator must be able to chase and eat preypublic interface Predator {

boolean chasePrey(Prey p);

void eatPrey(Prey p);

}

public class Lion implements Predator {

public boolean chasePrey(Prey p) {

// programming to chase prey p (spec. for a lion) }

public void eatPrey (Prey p) { // programming to eat prey p (spec. for a lion) }

// other Lion methods public void purr();

}16

Page 17: 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance (not today) Interfaces Reading for this Lecture:

17

Predator Interface In UML

Lion

<<interface>> Predator

+ chasePrey(Prey p) : void+ eatPrey(Prey p) : void

+ chasePrey(Prey p) : void+ eatPrey(Prey p) : void

+ purr( ) : void

Each method listedin Predator becomes

a method of Lion

Lion can haveother methods

of its own

A “Generalization” arrowis used for “implements”

(and also for “extends” later)

Interface box looks likea class box with

stereotype <<interface>>

Page 18: 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance (not today) Interfaces Reading for this Lecture:

Example from Wikipedia

Here Prey can itself be an interface, expressing Prey actions, say flee(Predator p)

And a class can implement multiple interfaces,

public class Frog implements Predator, Prey { ... }

This class will need to have methods chasePrey, eatPrey, and flee, and can have more methods of its own

18

Page 19: 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance (not today) Interfaces Reading for this Lecture:

19

Interface Types• You can go from Lion to Predator type:

• Now have useful Predator object ref• You can pass an object of Lion class to a method as

an object of Predator type.

• We say a Lion IS-A Predator, a Frog IS-A Prey, and also a Frog IS-A Predator

Lion leo = new Lion(…); // Lion objectPredator p = leo; // Predator ref to Lion

Frog freddie = new Frog(…);freddie.flee(leo);leo.chasePrey(freddie); //also works

Page 20: 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance (not today) Interfaces Reading for this Lecture:

20

InterfaceTypes

• When you are using an object via an interface ref, you can only call the interface methods with it

• Lion-specific methods, such as purr(), are not accessible when a Lion object is being used as a Predator object because they are not defined in the interface

Lion leo = new Lion(…); // Lion object

Predator p = leo; // Predator ref to Lion

leo.purr(); // works fine: Lions can purr()

p.purr(); // doesn’t work!! Predators can’t purr()

((Lion)p).purr(); // cast to Lion—works (the object is really a Lion, after all)

Page 21: 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance (not today) Interfaces Reading for this Lecture:

21

Library Interfaces

• The Java standard class library contains many interface definitions that allow other classes to treat your new class as if it were that interface

• Note: Comparable is an adjective in this case• The Comparable interface contains one abstract

method called compareTo, which can compare an object with another object of the same type

• We discussed the compareTo method of the String class previously

• The String class implements Comparable, giving us the ability to put strings in lexicographic order

Page 22: 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance (not today) Interfaces Reading for this Lecture:

22

The Comparable Interface• Any class can implement Comparable to provide

a mechanism for comparing objects of that type by providing a compareTo method

• The value returned from compareTo should be negative if obj1 is less than obj2, 0 if they are equal, and positive if obj1 is greater than obj2

• When you design a class that implements the Comparable interface, it should follow this intent

if (obj1.compareTo(obj2) < 0)

System.out.println ("obj1 is ” + “less than obj2");

Page 23: 1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance (not today) Interfaces Reading for this Lecture:

23

The Comparable Interface

• It's up to you as the programmer to determine what makes one object less than another

• For example, you may define the compareTo method of an Lion class to order by weight or tooth-length or any other useful way

• The implementation of the method can be as straightforward or as complex as needed for the situation

• Then we can sort an array of Lions, for example