Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns."— Presentation transcript:

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

2 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

3 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

4 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

5 5 A generic function plotter Design goals –a generic animation applet for plotting single variable functions. –easy to reuse and adapt for different functions

6 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 ƒ :double  double The only difference is by what function we compute the values to be plotted

7 7 The Template pattern

8 8 A generic function plotter

9 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 parameters scale parameters public paint : draw the coordinate axes draw the function graph in the interval given by the parameters

10 10 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); Method func has to be defined by any extending instances: public abstract class Plotter extends Japplet

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

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

13 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

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

15 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

16 16 The Strategy pattern

17 17 Refactoring by delegation

18 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;

19 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 the implementation unspecified and just define public interface Function{ public double apply(double x); }

20 20 MultiPlotter We are now able to plot multiple functions in the applet –an array of Function s and an array of Color s But we also have to –define a method for adding functions –decide when/how to add the functions

21 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)

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

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

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

25 25 Plotting Sin and Cos

26 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

27 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)

28 28 Exercises You will be working with the –Plotter and Multiplotter classes


Download ppt "1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns."

Similar presentations


Ads by Google