class relationships lecture oo10 dependencies. references n booch, et al, the unified modeling...

20
Class Relationships Lecture Oo10 Dependencies

Upload: logan-hudson

Post on 31-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10

Class Relationships

Lecture Oo10

Dependencies

Page 2: Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10

References

Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10 p.137.

Gamma, Helm, Johnson, Vlissides, Design Patterns, AWL, 1995

Page 3: Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10

Teaching Points

Dependency Parameterized Classes Abstract Factory

Page 4: Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10

Review

What is the difference between aggregation and composition?

Factory Method

Page 5: Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10

Dependencies

A dependency exists between to classes (or other elements) if changes to the definition of one element may cause changes to the other

Ideally we are considering changes to interface

not as strong as an association relationship

Page 6: Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10

Dependencies An object of one class creates an object

of another class

PrinterDriver

printDoc(argname : Document)

Document

ApplicationcurrentPrinter : PrinterDriver

print()

0..*

1

0..*

1

<<instantiates>>

Page 7: Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10

Dependencies An object of one class sends a

message to another• but the sending object is not responsible for

keeping track of the receiving object (which would be association)

PrinterDriver

printDoc(argname : Document)

Document

ApplicationcurrentPrinter : PrinterDriver

print()

0..*

1

0..*

1

<<uses>>

...currentPrinter.printDoc(someDoc)...

Page 8: Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10

Dependencies one class mentions another as a

parameter to an operation

PrinterDriver

printDoc(argname : Document)

Application

currentPrinter : PrinterDriver

print()

Document

0..*

1

0..*

1

Document is an argument to printDoc()

Page 9: Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10

Parameterized Class

Classes hide and manipulate data/objects Often we want to specify the way in which

data/objects are stored and manipulated without actually specifying the actual type of the data/objects

Parameterized classes allow types to be a parameters in the class or function definitions

Page 10: Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10

Parameterized Class

Used mainly for containers– sometimes for other novel implementations

Also known as– “Templates” in C++– “Generics” in Ada

Page 11: Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10

Example

Stacks are a general concept We might want to have stacks of int,

char, complex, or some other user defined objects

We would like to describe stacks in terms of some unknown type (say T), a type parameter, and provide an actual real type when we need a stack.

Page 12: Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10

Example

Here T is a type parameter

Stack

push()pop()bottom()full()

T

Page 13: Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10

Binding Relationship

Sometimes called “instantiation”– Note: this is not the same as object

instantiation A new class created by specifying an

actual type is called a “bound element” “Binding” is a kind of dependency

relationship between a class which is a bound element and a parameterized class

Page 14: Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10

Example

Here complex is an actual type which is substituted for the type parameter T

T

Stack

push()pop()bottom()full()

ComplexStack

<<bind>> (complex)

Page 15: Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10

Java Exampleinterface List<E>{

void add(E x);Iterator<E> iterator();

}

class LinkedList<E> implements List<E> {// implementation

}

class AnotherClass {LinkedList<string> strList = new LinkedList<string>();

someMethod() {strList.add(“Hello, World!”);string returnStr = strList.getHead();

}}

Page 16: Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10

Parameterized Classes vs. Generalization A bound element (instantiated class) is a

“kind-of” the parameterized class But this is not Generalization Binding merely restricts the type(s) used

but the interface remains the same Instantiation provides no mechanism for

adding new functionality (state or interface)

Page 17: Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10

ModComplexStack

clearStack()numOfElements()

Stack<complex>

T

Stack

push()pop()bottom()full()

Parameterized Classes and Generalization

– Note: Alternate notation for binding

Page 18: Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10

Abstract Factory Pattern

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

Page 19: Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10

AbstractFactory

createProductA()createProductB()

ConcreteFactory2

createProductA()createProductB()

ConcreteFactory1

createProductA()createProductB()

AbstractProductA

ConcreteProductA1 ConcreteProductA2

AbstractProductB

ConcreteProductB1 ConcreteProductB2

Client<<instantiates>>

<<instantiates>>

<<instantiates>>

<<instantiates>>

Page 20: Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10

Teaching Points

Dependency Parameterized Classes Abstract Factory