Abstract Classes An abstract class is a class with partial implementation. It implements behaviors that are common to all subclasses, but defers to the.

Slides:



Advertisements
Similar presentations
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use.
Advertisements

1 Object Oriented Programming Lecture XII The Adapter,Template, Strategy and Factory design patterns.
Design Patterns Section 7.1 (JIA’s) Section (till page 259) (JIA’s) Section 7.2.2(JIA’s) Section (JIA’s)
COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.
Object Oriented Programming Lecture 7: Algorithm animation using strategy and factory patterns, The Adapter design pattern
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
A Simple Applet --- Digital Clock import java.awt.*; import java.util.Calendar; public class DigitalColok extends java.applet.Applet implements Runnable.
1 (More) Pattern Games CS : Software Design Winter /T10.
Enahnced Digital Clock Applet Setting applet parameters in the web page. The applet tag in HTML:
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Recitation 11. Applet Applets. An applet is a Java program that is started by a browser (e.g. netscape or internet explorer) when an html file has a.
Template Method By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
Azusa Pacific University CS 587: Java Lemay & Perkins [1996]2-1 Thinking in Objects u Analogies: Legos, PCs u Components u Assemblies u Defined interfaces.
Chapter 10 Classes Continued
Java Review 3 Rem Collier. Java Wrapper Classes In Java, the term wrapper class commonly refers to a set of Java classes that “objectify” the primitive.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads. Thread = independent flow of control e.g. a server needs to communicate with many customers => each customer is served by a separate thread.
Fonts And Image. Fonts To display text, we choose a font. Recall that we are dealing with pixels in graphics programming. To choose a font, we need to.
1 Features of Java CS 3331 Fall Outline  Abstract class  Interface  Application --- animation applets.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
1 GoF Template Method (pp ) GoF Strategy (pp ) PH Single User Protection (pp ) Presentation by Julie Betlach 6/08/2009.
Applets CS 3331 Sections 3.3 & 4.7 of [Jia03].
Internet Software Development Applets Paul J Krause.
Applets.
PROGARMMING THROUGH JAVA Presentation on IMAGES Group No:3 Presented By: Anthony Narzary (DC2012MCA0002) Chandra Gupta Bora (DC2012MCA0009) Dipankar Saikia.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise www2.hh.se/staff/jebe/oop2005/
Template Design Pattern Kalim Baig. Summary What is Template? What is Template? Definition Definition Problem Problem How might it help the designer How.
Object Oriented Programming Lecture 5: Refactoring by Inheritance and Delegation - A simple Design Pattern for animation applets, A Generic Function Plotter.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns.
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Template Methods Ordering What We Do. Example - Solitaire Initialization of many solitaire games follow this pattern: Shuffle the cards Layout the game.
Design Patterns: Design by Abstraction
Features of Java CS 3331 Sections and 5.5.
Painting (Chapter 12) Java Certification Study Group January 25, 1999 Mark Roth.
BallWorld.java Ball.java A functional walkthrough Part 3: the interaction of objects.
1 Contents Introduction Applet Vs Application Security Restrictions on Applet A simple example “Hello World!” applet Compiling & Running Applet HTML document.
STRATEGY PATTERN. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
The Template Method Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
CSC 480 Software Engineering Design With Patterns.
More OOP. Extending other’s classes extend Java platform classes, e.g. class Applet public class MyApplet extends Applet { public void init() { } public.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
StarBuzz Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
Java Applets Adding Animation. Import Files You still need to include the same files: –import java.applet.*; –import java.awt.*;
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
Chapter 11: Threaded Programs Situations where the program is following multiple execution paths (how to stop one?) Thread: a line of execution Thread.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
TEMPLATE METHOD DESIGN PATTERN -SWAPNIL SHAH. WHAT IS A DESIGN PATTERN… A design pattern is a general reusable solution to a commonly occurring problem.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Topic: Applets Course : JAVA PROGRAMMING Paper Code: ETCS-307
Design Patterns: MORE Examples
Abstract classes and interfaces
Design Patterns: Brief Examples
Inheritance and Polymorphism
Software Design and Architecture
Abstract classes and interfaces
null, true, and false are also reserved.
Java Programming COMP-417 Applet
Abstract classes and interfaces
Chapter 8, Design Patterns Factory
CSC 480 Software Engineering
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Abstract Classes An abstract class is a class with partial implementation. It implements behaviors that are common to all subclasses, but defers to the subclasses to implement others (abstract methods). Abstract methods must be implemented in each non-abstract subclasses. Instance of an abstract class is not allowed.

Interfaces Á Interfaces are classes with no implementation.  Interfaces represent pure design.  Abstract classes represent mixed design and implementation. Á An interface consists of only abstract methods and constants, i.e., static and final. Á All methods and constants are public. Á No static methods. Á No instance allowed. Á Multiple inheritance for interfaces is allowed. Á An interface can only extend interface, not classes. Á There is no single root for interfaces.

A Generic Animator Abstract classes can be used to design generic components.

A Generic Animator (cont'd) public abstract class AnimationApplet extends java.applet.Applet implements java.lang.Runnable { protected Thread animationThread; protected int delay = 100; final public void setDelay(int delay) { this.delay = delay; } final public int getDelay() { return delay; }

A Generic Animator (cont'd) public void start() { animationThread = new Thread(this); animationThread.start(); } public void stop() { animationThread = null; } public void run() { while (Thread.currentThread() == animationThread) { try { Thread.currentThread().sleep(delay); } catch (InterruptedException e){} repaint(); }

Digital Clock Using the Generic Animator import java.awt.*; import java.util.Calendar; public class DigitalClock3 extends AnimationApplet { public DigitalClock3() { setDelay(1000); } public void paint(Graphics g) { // identical to the paint() method in DigitalClock } protected Font font = new Font("Monospaced", Font.BOLD, 48); protected Color color = Color.green; }

A Double-Buffered Generic Animator Key design issues Accommodate various sizes of the view area. Allow the subclasses to decide whether to use double-buffering or not. Factorize common code segments that contain variable parts.

A Double-Buffered Generic Animator (cont'd)

public abstract class DBAnimationApplet extends AnimationApplet { protected DBAnimationApplet(boolean doubleBuffered) { this.doubleBuffered = doubleBuffered; } protected DBAnimationApplet() { this.doubleBuffered = true; } // other methods protected boolean doubleBuffered; protected Image im; protected Graphics offscreen; protected Dimension d; }

A Double-Buffered Generic Animator (cont'd) protected void initAnimator() {} final public void init() { d = getSize(); initAnimator(); } abstract protected void paintFrame(Graphics g); final public void paint(Graphics g) { paintFrame(g); }

A Double-Buffered Generic Animator (cont'd) final public void update(Graphics g) { if (doubleBuffered) { if (im == null) { im = createImage(d.width, d.height); offscreen = im.getGraphics(); } paintFrame(offscreen); g.drawImage(im, 0, 0, this); } else { super.update(g); }

Bouncing Ball Using the Generic Animator public class BouncingBall2 extends DBAnimationApplet { public BouncingBall2() { super(true); // double bufferring } // methods protected int x, y; protected int dx = -2, dy = -4; protected int radius = 20; protected Color color = Color.green; }

Bouncing Ball Using the Generic Animator (cont'd) protected void initAnimator() { String att = getParameter("delay"); if (att != null) setDelay(Integer.parseInt(att)); x = d.width * 2 / 3 ; y = d.height - radius; }

Bouncing Ball Using the Generic Animator (cont'd) protected void paintFrame(Graphics g) { g.setColor(Color.white); g.fillRect(0,0,d.width,d.height); if (x d.width - radius) { dx = -dx; } if (y d.height - radius) { dy = -dy; } x += dx; y += dy; g.setColor(color); g.fillOval(x - radius, y - radius, radius * 2, radius * 2); }

Design Pattern: Template Method Category Behavioral design pattern. Intent Define the skeleton of an algorithm in a method, deferring some steps to subclasses, thus allowing the subclasses to redefine certain steps of the algorithm. Applicability to implement the invariant parts of an algorithm once and leave it up to the subclasses to implement the behavior that can vary. to factorize and localize the common behavior among subclasses to avoid code duplication.

Template Method

A Generic Function Plotter

A Generic Function Plotter: The Design

A Generic Function Plotter (cont'd) public abstract class Plotter extends Applet { // hook method public abstract double func(double x); public void paint(Graphics g) { drawCoordinates(g); plotFunction(g); } public void init() { } protected void drawCoordinates(Graphics g) { }

A Generic Function Plotter (cont'd) (class Plotter continued) protected 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) {} } }

Generic Multiple Function Plotter

Interface Function interface Function { double apply(double x); } public class Sine implements Function { public double apply(double x) { return Math.sin(x); } public class Cosine implements Function { public double apply(double x) { return Math.cos(x); }

Multiple Function Plotter Class public abstract class MultiPlotter extends Plotter { protected static int MAX_FUNCTIONS = 5; protected int numOfFunctions = 0; protected Function functions[] = new Function[MAX_FUNCTIONS]; protected Color colors[] = new Color[MAX_FUNCTIONS]; abstract public void initMultiPlotter(); public void init() { super.init(); initMultiPlotter(); }

Multiple Function Plotter Class (cont'd) final public void addFunction(Function f, Color c) { if (numOfFunctions < MAX_FUNCTIONS && f != null) { functions[numOfFunctions] = f; colors[numOfFunctions++] = c; }

Multiple Function Plotter Class (cont'd) protected void plotFunction(Graphics g) { for (int i = 0; i < numOfFunctions; i++) if (functions[i] != null) { Color c = colors[i]; if (c != null) g.setColor(c); else g.setColor(Color.black); for (int px = 0; px < d.width; px++) try { double x = (double) (px - xorigin) / (double) xratio; double y = functions[i].apply(x); int py = yorigin - (int) (y * yratio); g.fillOval(px - 1, py - 1, 3, 3); } catch (Exception e) {} }

A Concrete Multiple Function Plotter public class PlotSineCosine extends MultiPlotter { public void initMultiPlotter() { addFunction(new Sine(), Color.green); addFunction(new Cosine(), Color.blue); }

Design Pattern: Strategy Category Behavioral design pattern. Intent Define a family of algorithms, encapsulate each one, and make them interchangeable. Applicability many related classes differ only in their behavior. you need different variants of an algorithm. an algorithm uses data that clients shouldn't know about. a class defines many behaviors, and these appear as multiple conditional statements in its methods.

Strategy