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

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Understand and appreciate Object Oriented Programming (OOP) Objects are self-contained modules or subroutines that contain data as well as the functions.
Java Review Interface, Casting, Generics, Iterator.
Data Structures Lecture 2 Fang Yu Department of Management Information Systems National Chengchi University Fall 2011.
Written by: Dr. JJ Shepherd
1 Object Oriented Programming Lecture XII The Adapter,Template, Strategy and Factory design patterns.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Object Oriented Programming Lecture 7: Algorithm animation using strategy and factory patterns, The Adapter design pattern
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Abstract Classes An abstract class is a class with partial implementation. It implements behaviors that are common to all subclasses, but defers to the.
OOP&M - laboratory lectures1 OOP&M – LAB3 LABtres: drawing.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
FrameworksFrameworks chap8. Framework Set of cooperating classes Structures the essential mechanisms of a problem domain Framework != design pattern Typical.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
Generics, Proxy, and The Compile Time Type Checking Debate You are either with us or against us. Please snarf the code for today’s class.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
Inheritance using Java
Introduction to Object-oriented programming and software development Lecture 1.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise www2.hh.se/staff/jebe/oop2005/
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Introduction to Object Oriented Programming CMSC 331.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Object Oriented Programming Lecture 5: Refactoring by Inheritance and Delegation - A simple Design Pattern for animation applets, A Generic Function Plotter.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Design Patterns: Design by Abstraction
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Neal Stublen Tonight’s Agenda  Indexers  Delegates and events  Operator overloading  Class inheritance  Q&A.
STRATEGY PATTERN. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
The Template Method Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Interfaces and Inner Classes
Bridge Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern.
Chapter More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Advanced Programming in Java
Sections Inheritance and Abstract Classes
Interface Java 7 COMP T1.
Object-Oriented Programming
30 Java Applets.
Advanced Programming Behnam Hatami Fall 2017.
More About Inheritance & Interfaces
Java Inheritance.
CIS 199 Final Review.
Final and Abstract Classes
Presentation transcript:

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

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 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 A generic function plotter Design goals –a generic animation applet for plotting single variable functions. –easy to reuse and adapt for different functions

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 The Template pattern

8 A generic function plotter

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 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 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 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 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 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 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 The Strategy pattern

17 Refactoring by delegation

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 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 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 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 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 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 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 Plotting Sin and Cos

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 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 Exercises You will be working with the –Plotter and Multiplotter classes