explain SilyCanvas design
svn: r7094
This commit is contained in:
parent
a811f769d7
commit
91eb15f72a
|
@ -1,7 +1,7 @@
|
||||||
package draw;
|
package draw;
|
||||||
|
|
||||||
public abstract class World {
|
public abstract class World {
|
||||||
protected Canvas theCanvas = new SillyCanvas(600,600); // can I do better here?
|
protected Canvas theCanvas = new SillyCanvas(600,600); // can I do better than null here?
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*@author Matthias Felleisen, Kathy Gray
|
*@author Matthias Felleisen, Kathy Gray
|
||||||
|
|
44
collects/htdch/draw/design-note.txt
Normal file
44
collects/htdch/draw/design-note.txt
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
[Mon Aug 13 20:03:56 EDT 2007]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
1. SillyClass exists to get around the NULL problem in World.java.
|
||||||
|
|
||||||
|
I create a SillyCanvas, which is a 600 x 600 canvas so that
|
||||||
|
theCanvas doesn't have to be initialized to NULL.
|
||||||
|
|
||||||
|
A SillyCanvas is a Canvas that displays a warning for every
|
||||||
|
draw method that is invoked. So students can show the canvas
|
||||||
|
before they call bigBang and test the draw method in World,
|
||||||
|
but it is pretty silly.
|
||||||
|
|
||||||
|
Rationale:
|
||||||
|
The reason I don't want NULL in this draw.* library is that
|
||||||
|
it is used with ProfessorJ's Beginner and Intermediate
|
||||||
|
language levels and they don't include NULL. It turns out,
|
||||||
|
however, that this particular NULL for theCanvas could "leak
|
||||||
|
out" if a student did this:
|
||||||
|
|
||||||
|
new SampleWorld().theCanvas.show()
|
||||||
|
|
||||||
|
or something less direct than that.
|
||||||
|
|
||||||
|
2. Of course, the rationale also suggests that I may have made
|
||||||
|
a mistake in the design of World. Instead of creating World
|
||||||
|
via a no-argument constructor, I add a constructor of two
|
||||||
|
arguments, width and height, and force students to perform
|
||||||
|
a super call:
|
||||||
|
|
||||||
|
World(int width, int height) {
|
||||||
|
this.theCanvas = new Canvas(width,height);
|
||||||
|
}
|
||||||
|
|
||||||
|
World() {
|
||||||
|
throw RuntimeException("can't create a world without size arguments");
|
||||||
|
}
|
||||||
|
|
||||||
|
PRO: And voli`a, the problem would go away.
|
||||||
|
CONS: Students must fix the size of the canvas at World creation time.
|
||||||
|
With my existing design, they can choose different sizes when
|
||||||
|
they run bigBang. Is it worth it? Probably not.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user