Docstoc

Class Relationships

Document Sample
Class Relationships Powered By Docstoc
					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
                                                 Application
                                         currentPrinter : PrinterDriver

                                         print()

                                                   1

                      <<instantiates>>




                                                       PrinterDriver
    Document   0..*
                                         printDoc(argname : Document)
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)           Application
                                           currentPrinter : PrinterDriver
    ...
    currentPrinter.printDoc(someDoc)       print()
    ...
                                       1


                                                           <<uses>>



                  0..*
                                                     PrinterDriver
       Document
                                           printDoc(argname : Document)
Dependencies
   one class mentions another as a
    parameter to an operation
                                      Application
      Document is an
      argument to print       currentPrinter : PrinterDriver
      Doc()
                              print()
                          1




               0..*
                                        PrinterDriver
    Document
                              printDoc(argname : Document)
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

                        T
                   Stack

                 push()
                 pop()
                 bottom()
                 full()
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()                     ComplexStack
       pop()
       bottom()   <<bind>> (complex)
       full()
Java Example
 interface 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)
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.
                                                           AbstractProductA




           AbstractFactory
                                                ConcreteProductA1    ConcreteProductA2
          createProductA()
          createProductB()
                               <<instantiates>>
                                                                                          Client



ConcreteFactory2        ConcreteFactory1
                                                           AbstractProductB
createProductA()       createProductA()
createProductB()       createProductB()
                                <<instantiates>>



                                                ConcreteProductB1     ConcreteProductB2



                             <<instantiates>>

                                                               <<instantiates>>
Teaching Points

   Dependency
   Parameterized Classes
   Abstract Factory

				
DOCUMENT INFO