1 object oriented programming lecture xi an abstract function plotter, using the template and the...

28
1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

Upload: donna-francis

Post on 05-Jan-2016

219 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

1

Object Oriented ProgrammingLecture XI

An abstract function plotter, using the Template and the Strategy design patterns

Page 2: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

2

Last Lecture

• Generic class definitions– Actual types are bound at compile time

• typing safety (checked at compile time) • no type casts needed• over-loading can sometimes be avoided

• Enhanced for loops: for-each– Useful especially for iterations over collections– Increased safety

• three Iterator operations reduced to one “for” line

Page 3: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

3

Today’s Talk

• Design by abstraction– more design patterns

• the Template (method) pattern– abstract classes and inheritance

• the Strategy pattern– interfaces

– A concrete case study• a single variable function plotter• applying both Template and Strategy

Page 4: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

4

Design by Abstraction

• A generic component is designed to be– reusable– extensible

• For abstract design, we can make use of– design patterns– abstract classes– interfaces

Without having to modify the code

Page 5: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

5

A generic function plotter

• Design goals– a generic animation

applet for plotting single variable functions.

– easy to reuse and adapt for different functions

Page 6: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

6

First design attempt

• Common plotting properties– of course, a drawing area

• should be scaleable (function interval)

– graded x- and y axes • should be scaleable (zoom function)

• What are the functions?– any single variable function ƒ :doubledouble

• The only difference is by what function we compute the values to be plotted

Page 7: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

7

The Template pattern

Page 8: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

8

A generic function plotter

Page 9: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

9

Designing the Plotter

Factor the common code into a generic template class:

public class Plotter extends Japplet

public init: read parameters from html tags size parametersscale parameters

public paint: draw the coordinate axes draw the function graph in the interval given by the parameters

Page 10: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

10

The class PlotterWhat 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); Method func has to be defined by any extending instances:

public abstract class Plotter extends Japplet

Page 11: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

11

How do we define a function?

By overriding the abstract func method:

public class CosPlotter extends Plotter{ protected double func(double x){

return Math.cos(x); }}

public class SinPlotter extends Plotter{ protected double func(double x){

return Math.sin(x); }}

Page 12: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

12

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 = ...

}

Page 13: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

13

Factoring by inheritance

Applied design pattern: Template

The template class(Plotter)provides an abstract method, called “hook method”(func),which is overridden by the extending class

Page 14: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

14

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){}}

}

Page 15: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

15

Improving the function plotter

• Can the plotter be made more generic?– only one function can be plotted

• We would like to• plot multiple functions• plot functions by different color

– this implies that we should try to separate the function from the plotter template

Page 16: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

16

The Strategy pattern

Page 17: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

17

Refactoring by delegation

Page 18: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

18

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 any object that can do apply(double)! private Function f;

Page 19: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

19

The interface Function

We need a type Function for objects that can do

double apply(double x)

Now, we want this method to compute cos, sin, or any other function. We leave theimplementation unspecified and just define

public interface Function{ public double apply(double x);}

Page 20: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

20

MultiPlotter

• We are now able to plot multiple functions in the applet– an array of Functions and an array of Colors

• But we also have to– define a method for adding functions– decide when/how to add the functions

Page 21: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

21

MultiPlotter

• Let the class that extends MultiPlotter define init() where

• parameters are read

• functions are added

• A bad thing, what happens if the programmer do not get the parameters? – Let init be a final method, add an abstract hook

method for user specific inits (such as adding functions)

Page 22: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

22

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();

Page 23: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

23

MultiPlotter.plotFunctions

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){}}

}}

Page 24: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

24

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); }}

Page 25: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

25

Plotting Sin and Cos

Page 26: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

26

Design Guidelines

• Maximize adaptability

– the more flexible a component is, the better chances it will be reused

• Minimize risk for misuse

– make necessary (critical) initialization in a final method and offer a hook method for user specific inits

Page 27: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

27

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)

Page 28: 1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns

28

Exercises

• You will be working with the– Plotter and Multiplotter classes