Transcript Slide 1

Object Oriented Programming
Lecture XI
An abstract function plotter, using the
Template and the Strategy design patterns
1
Last Lecture
• Java 1.5 enhancements
– Generics
2
Today’s Talk
• Enhancements in Java 1.5
– generics
– enhanced for loops
• Other enhancements (to read on your own)
–
–
–
–
auto boxing/unboxing
type-safe enums
Varargs
Annotations
3
Design by Abstraction
• A generic component is designed to be
– reusable
Without having to modify the code
– extensible
• For abstract design, we make use of
–
–
–
abstract classes
interfaces
design patterns
4
A generic function plotter
• Design goals
– a generic animation
applet for plotting
functions.
– easy to reuse and
adapt for different
functions
5
The Template pattern
6
A generic function plotter
7
Designing the Plotter
Factor common code into a generic template class:
public class Plotter extends Japplet
public init: read parameters from html tags
size parameters
scale parameters
public paint: draw the coordinate axes
draw the function graph
in the interval given by the parameters
8
The class Plotter
What function are we drawing?
A function can be implemented by a method:
public double func(double x){???}
or even better:
protected abstract double func(double x);
which also forces the class to be abstract.
Method func has to be defined by any extending instances:
public abstract class Plotter extends Japplet
9
How do we define a
function?
public class CosPlotter extends Plotter{
protected double func(double x){
return Math.cos(x);
}
}
or…
public class SinPlotter extends Plotter{
protected double func(double x){
return Math.sin(x);
}
}
10
Inside the Plotter
public abstract class Plotter extends Japplet{
private int w,h,xorigin,yorigin,xratio,yratio;
private Color color = Color.black;
protected abstract double func(double x);
public void init(){
w = Integer.parseInt(getParameter(“width”));
h = Integer.parseInt(getParameter(“height”));
xorigin = ...
yorigin = ...
xratio = ...
yratio = ...
}
11
Factoring by inheritance
Applied design pattern: Template
In the template class an abstract method
(plotFunction)
calls a hook method
(func)
that is made abstract
12
Looking inside Plotter
public void paint(Graphics g){
drawCoordinates(g);
plotFunction(g);
}
private void plotFunction(Graphics g){
for(int px = 0; px < dim.width; px ++){
try{
double x =(double)(px - xorigin)/(double)xratio;
double y =func(x);
int py = yorigin - (int)(y * yratio);
g.fillOval(px-1,py-1,3,3);
}catch(Exception e){}
}
}
13
Improving the function plotter
14
Refactoring by delegation
15
The class MultiPlotter
public class MultiPlotter extends Japplet
public init as before: read parameters.
public paint as before: draw coordinates and the function
in the interval given by the params.
What function? A function can be implemented by an object that
can do apply(double)!
private Function f;
16
The interface Function
We need a type Function for objects that can do
double apply(double x)
Now, we want this method to behave sometimes as
cos, sin, or other function. We leave the implementation
thus unspecified and just define
public interface Function{
public double apply(double x);
}
17
MultiPlotter
• We can now plot a number of functions in
the same applet (By having an array with
Functions and an array with Colors)
• We have to offer a method to add
functions
• We have to decide when/how to add the
functions.
18
MultiPlotter
• Let the class that adapts (extends)
MultiPlotter define init() where
• Parameters are read
• Functions are added (using the method for
doing so)
• A bad thing:
what happens if the programmer forgets to read
the parameters?
• Let init be a template method and use a hook to
allow the new class to add functions!
19
MultiPlotter
public abstract class MultiPlotter extends Japplet
private int w, h, xorigin, yorigin, xratio, yratio;
private Function [] functions;
private Color [] colors;
public final void init(){
/* read parameters; */
functions = new Function[max];
colors
= new Color[max];
initMultiPlotter();
}
protected abstract void initMultiPlotter();
20
MultiPlotter.plotFuncti
ons
private void plotFunctions(Graphics g){
for(int i = 0; i < numOfFunctions; i++){
g.setColor(colors[i]);
for(int px = 0; px < dim.width; px ++){
try{
double x = (double)(px ...
double y = functions[i].apply(x);
int
py = yorigin - ...
g.fillOval(px-1,py-1,3,3);
}catch(Exception e){}
}
}
}
21
Plotting sin and cos
public class SinCos extends MultiPlotter{
protected void initMultiPlotter(){
addFunction(new Sin(),Color.red);
addFunction(new Cos(),Color.blue);
}
}
public class Cos implements Function{
public double apply(double x){
return Math.cos(x);
}
}
22
Plotting Sin and Cos
23
Design Guidelines
• Maximize adaptability (extensibility)
The more extensible a component is, the
better chances it will be reused
• Minimize risk for missuse
(make init a final method and force the
definition of initMultiPlotter instead
of allowing for redefinition of init!)
24
Factoring by delegation
Applied design pattern: Strategy
In the context (the general class, MultiPlotter)
one or more instances of strategy objects
(Function[] functions)
define concrete strategies (classes implementing
the strategy: Cos, Sin)
25