Class Relationships - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Class Relationships

Description:

Fowler & Scott, UML Distilled Applying the Standard Object Modeling Language, ... that the classes are substitutable for each other, and a late binding mechanism ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 31
Provided by: elect77
Category:

less

Transcript and Presenter's Notes

Title: Class Relationships


1
Class Relationships
  • Lecture Oo08
  • Polymorphism

2
References
  • Booch, et al, The Unified Modeling Language User
    Guide, Chapt 10 p.125
  • Fowler Scott, UML Distilled Applying the
    Standard Object Modeling Language, AWL, 1997,
    Chapt 4
  • Booch, Object Oriented Analysis and Design, Chapt
    3

3
Teaching Points
  • Abstract Classes
  • Polymorphism
  • Late-Binding Mechanisms
  • Interfaces
  • When/Why to use Polymorhism?
  • Patterns
  • Case/Enum
  • Factory Method

4
Review
  • What is the difference between type and class?
  • What is an operation signature?

5
Abstract Classes
  • Are objects normally instantiated from every
    class in a system?

6
Polymorphism
  • Overloading (ad hoc polymorphism)
  • Binding based on number and type of arguments
  • I.e. operation signature
  • A static binding mechanism (compile/link -time
    binding)

7
Polymorphism
  • polymorphism (parametric polymorphism)
  • depends on type of actual parameter of function
    call at run time
  • A dynamic binding mechanism (late-binding)(run-tim
    e binding)

8
Polymorphism
  • Allows us to treat the same kinds of objects in
    the same way
  • Objects with the same interface can be treated in
    the same way
  • Objects of different types can be treated in the
    same way provided they have a common base-type
  • A VERY POWERFUL MECHANISM!

9
Things Needed for a Polymorphic Function Call
  • Common methods in different classes such that the
    classes are substitutable for each other, and a
    late binding mechanism
  • In statically typed langagues (e.g. Java, C)
  • Base-class Object Reference
  • Over ridden methods
  • REMEMBER THESE!

10
Operation Overriding
class shape private point centre private
int id public int getId() public
point getCentre() public void draw()
// overridden operation class circle
extends shape private double radius
public void draw() //overriding method (same
signature)
11
Example
  • List of shapes
  • Drawing the list

12
Abstract Classes
  • Provide a common interface
  • Abstract operations
  • A class with all abstract operations and no
    state, like a Java interface
  • Interface-inheritance

13
Abstract Operations
abstract class shape // Now an abstract class
private point centre private int id
public int getId() public point
getCentre() public abstract void draw()
// Now an abstract operation class circle
extends shape private double radius
public void draw() //overriding method (same
signature)
14
Sub-typing without Sub-classing
  • Java Interfaces
  • Abstract classes with no field and no operation
    implementations
  • These ideas only come into play when working with
    the implementation perspective

15
Modeling an Interface
16
Alternate Interface Notation
  • Lollipop
  • Equivalent to last slide

17
Implementing an Interface
interface shape // Now an interface public
int getId() // All operations now abstract
public point getCentre() public void
draw() class circle implements shape
private point centre //State variables have to
move to private int id //the
implementing class private double radius
public void draw() //overriding method (same
signature)
18
Sub-classing without Sub-typing
  • Private and Protected Inheritance
  • Allows us to reuse a class without having to
    present its interface to the rest of the world
  • The base class is concrete
  • Can be similar to aggregation

19
When to Use Polymorphism
  • When we want to treat a set of things in the same
    way, even though they may not be objects of the
    same concrete class
  • The concept of an abstract interface is critical
  • Probably the prime reason for using
    generalization/inheritance

20
What is a Pattern?
  • Each pattern describes a problem which occurs
    over and over again in our environment, and then
    describes the core of the solution to that
    problem, in such a way that you can use this
    solution a million times over, without ever doing
    it the same way twice
  • Christopher Alexander

21
What is a Pattern?
  • A design pattern describes a commonly-recurring
    structure of communicating components that solve
    a general design problem in a particular
    context.
  • Gamma, et al.

22
Why Use Patterns?
  • Advantages of design patterns are reusability and
    modularity, which are precisely the goals of
    object-oriented software design.
  • Because design patterns capture expert knowledge,
    the solutions it provides are tried and true.
    Therefore, they not only make it less
    time-consuming to design a large system, it also
    improves design quality.

23
Why Use Patterns?
  • Design patterns provide a common vocabulary for
    designers to communicate by expressing software
    design at higher level of abstraction than that
    of a design notation or programming language.
  • Being able to recognise design patterns helps one
    to understand existing systems.

24
Pattern Examples
  • Meta-pattern for case/enum
  • Factory

25
Case/Enum
//Fertilization schedule switch(fruitType)
case APPLES //apples fertilization
code break case ORANGES //oranges
fertilization code break case PEARS //pears
fertilization code break
26
Case/Enum
27
Factory Method
  • Intent
  • Define an interface for creating an object, but
    let subclasses decide which class to instantiate.
    Factory Method lets a class defer instantiation
    to subclasses.
  • Also Known As
  • Virtual Constructor

28
Factory Method
  • Use the Factory Method pattern when
  • a class can't anticipate the class of objects it
    must create.
  • a class wants its subclasses to specify the
    objects it creates.
  • classes delegate responsibility to one of several
    helper subclasses, and you want to localize the
    knowledge of which helper subclass is the
    delegate.

29
Factory Method
30
Teaching Points
  • Abstract Classes
  • Polymorphism
  • Late-Binding Mechanisms
  • Interfaces
  • When/Why to use Polymorhism?
  • Patterns
  • Case/Enum
  • Factory Method
Write a Comment
User Comments (0)
About PowerShow.com