Professional Documents
Culture Documents
1
A Question Factorization
• What is factorization?
• What will be printed when the main method in
singleSpooler is executed?
2
import java.awt.*;
An Animation Applet public class AnimationApplet extends java.applet.Applet
implements java.lang.Runnable {
• A Simplistic Approach
3
import java.awt.*; protected void initAnimator() {}
A Better
public abstract class DBAnimationApplet
extends AnimationApplet { Version - 1 abstract protected void paintFrame(Graphics g);
}
// super.update();
protected DBAnimationApplet() {
A Better
}
final public void paint(Graphics g) {
this.doubleBuffered = true; Version - 2
}
paintFrame(g);
} protected boolean doubleBuffered;
final public void init() {
protected Dimension d;
d = getSize();
protected Image im;
im = createImage(d.width, d.height);
protected Graphics offscreen;
offscreen = im.getGraphics();
initAnimator();
} }
public BouncingBall2() {
super(true); // double buffering
}
protected int x, y;
protected int dx = -2, dy = -4;
protected int radius = 20;
protected Color color = Color.green;
}
4
The Template Design Pattern - 2 Drawing Triangles
• Motivation
– Where a user is drawing on a sketch pad they may
want to draw a triangle; in some cases the user may
want the triangle to be isosceles or a right triangle
– It is often difficult for the user to be sure the triangle
meets the desired criteria from the visual image
– This drawing routine may adjust the “third point” to
• Can you think of any other applications that could make sure the triangle meets any special criteria
benefit from the Template design pattern?
p3 Adjusted p3
third line
second line
p1 p2
first line
The Abstract
import java.awt.*;
public abstract class Triangle { The Standard Triangle
private Point p1, p2, p3; Triangle
public Triangle(Point a, Point b, Point c) {
p1 = a; p2 = b; p3 = c; Class No adjustments are made to the second line to
} force it to be a “special” type of triangle
public void draw(Graphics g) {
//This routine draws a general triangle
drawLine(g, p1, p2); import java.awt.*;
Point current = draw2ndLine(g, p2, p3); public class stdTriangle extends Triangle {
closeTriangle(g, current);
public stdTriangle(Point a, Point b, Point c) {
}
//--------------------------------------- super(a, b, c);
public void drawLine(Graphics g, Point a, Point b) { }
g.drawLine(a.x, a.y, b.x, b.y); public Point draw2ndLine(Graphics g, Point a, Point b) {
}
g.drawLine(a.x, a.y, b.x, b.y);
//---------------------------------------
abstract public Point draw2ndLine(Graphics g, Point a, Point b); return b;
//--------------------------------------- }
public void closeTriangle(Graphics g, Point c) { }
//draw back to first point
g.drawLine(c.x, c.y, p1.x, p1.y);
}
}
5
A Right Triangle Why Triangle is a Template pattern
• Using the isosceles triangle as a guide, write the • The generic class is Triangle
right triangle class • The concrete classes are stdTriangle, isoscelesTriangle,
and rightTriangle
• Assume the right angle is between the second and • The template methods are drawLine and closeTriangle
third legs, so the first leg is the hypotenuse • The hook method is draw2ndLine