Upload
logan-hudson
View
212
Download
0
Embed Size (px)
Citation preview
Class Relationships
Lecture Oo10
Dependencies
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
Teaching Points
Dependency Parameterized Classes Abstract Factory
Review
What is the difference between aggregation and composition?
Factory Method
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
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>>
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)...
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()
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
Parameterized Class
Used mainly for containers– sometimes for other novel implementations
Also known as– “Templates” in C++– “Generics” in Ada
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.
Example
Here T is a type parameter
Stack
push()pop()bottom()full()
T
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
Example
Here complex is an actual type which is substituted for the type parameter T
T
Stack
push()pop()bottom()full()
ComplexStack
<<bind>> (complex)
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();
}}
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)
ModComplexStack
clearStack()numOfElements()
Stack<complex>
T
Stack
push()pop()bottom()full()
Parameterized Classes and Generalization
– Note: Alternate notation for binding
Abstract Factory Pattern
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
AbstractFactory
createProductA()createProductB()
ConcreteFactory2
createProductA()createProductB()
ConcreteFactory1
createProductA()createProductB()
AbstractProductA
ConcreteProductA1 ConcreteProductA2
AbstractProductB
ConcreteProductB1 ConcreteProductB2
Client<<instantiates>>
<<instantiates>>
<<instantiates>>
<<instantiates>>
Teaching Points
Dependency Parameterized Classes Abstract Factory