Title: Class Relationships
1Class Relationships
- Lecture Oo08
- Polymorphism
2References
- 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
3Teaching Points
- Abstract Classes
- Polymorphism
- Late-Binding Mechanisms
- Interfaces
- When/Why to use Polymorhism?
- Patterns
- Case/Enum
- Factory Method
4Review
- What is the difference between type and class?
- What is an operation signature?
5Abstract Classes
- Are objects normally instantiated from every
class in a system?
6Polymorphism
- Overloading (ad hoc polymorphism)
- Binding based on number and type of arguments
- I.e. operation signature
- A static binding mechanism (compile/link -time
binding)
7Polymorphism
- 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)
8Polymorphism
- 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!
9Things 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!
10Operation 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)
11Example
- List of shapes
- Drawing the list
12Abstract Classes
- Provide a common interface
- Abstract operations
- A class with all abstract operations and no
state, like a Java interface - Interface-inheritance
13Abstract 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)
14Sub-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
15Modeling an Interface
16Alternate Interface Notation
- Lollipop
- Equivalent to last slide
17Implementing 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)
18Sub-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
19When 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
20What 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
21What 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.
22Why 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.
23Why 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.
24Pattern Examples
- Meta-pattern for case/enum
- Factory
25Case/Enum
//Fertilization schedule switch(fruitType)
case APPLES //apples fertilization
code break case ORANGES //oranges
fertilization code break case PEARS //pears
fertilization code break
26Case/Enum
27Factory 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
28Factory 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.
29Factory Method
30Teaching Points
- Abstract Classes
- Polymorphism
- Late-Binding Mechanisms
- Interfaces
- When/Why to use Polymorhism?
- Patterns
- Case/Enum
- Factory Method