The Department of Computer Science & Engineering
cse@buffalo
STUART C. SHAPIRO: CSE 115 C

CSE 115
Introduction To Computer Science for Majors I
Lecture C
Lecture Notes
Stuart C. Shapiro
Spring, 2001


Design Patterns

Reading
Brown U. Slides

See /projects/CSE115/Classlibs/Demos/PatternsLecture/

Previous programs have been
Programs with no input
For example: Move-speak-identify-beg animals; Weighable objects; Filled ellipse; Alien programs; SnowmanFace (Lab2); BankTester (Lab5).
The only way to change behavior is to reprogram.

Programs with GUIs using mouse input
For example: Snowman (Lab3); TV warehouse (Lab4); House scene (Lab6).
Behavior direct result of which button is pressed.

Can the same input result in different behavior at different times?
     MEMORY = STATE
Same input can have different behavior if the state of the program is different.

How to store, and use state information?
We will look at three patterns for the design of programs that change their state as a result of input.

See /projects/CSE115/Classlibs/Demos/PatternsLecture/Holder/ and /projects/CSE115/Classlibs/Demos/PatternsLecture/Proxy/

Note on translate (from MovingEllipse).
Destructive change, rather than creation of new point.

bsh % p1 = new java.awt.Point(10, 20);
bsh % p2 = new java.awt.Point(15, 30);
bsh % p3 = p1;

bsh % print(p1 + "  " + p2 + "  " + p3);
java.awt.Point[x=10,y=20]  java.awt.Point[x=15,y=30]  java.awt.Point[x=10,y=20]

bsh % print(p1==p2);
false
bsh % print(p1==p3);
true

bsh % p1.translate(5, 10);

bsh % print(p1 + "  " + p2 + "  " + p3);
java.awt.Point[x=15,y=30]  java.awt.Point[x=15,y=30]  java.awt.Point[x=15,y=30]

bsh % print(p1==p2);
false
bsh % print(p1==p3);
true

First Previous Next

Copyright © 2001 by Stuart C. Shapiro. All rights reserved.

Stuart C. Shapiro <shapiro@cse.buffalo.edu>