|
Design Pattern |
|
}
void registerDisplay(Display disp) {
display = disp;
}
void throwit() {
display.setForeground(Color.black);
int num = (int)(Math.random()*6 +1);
int i = counter++;
list.add(i, new Integer(num));
undo.add(i, new Memento(num));
display.setText(“”+num);
}
void previous() {
display.setForeground(Color.red);
btnDc.setEnabled(false);
if (undo.size() > 0) {
ct = undo.size()-1;
Memento num = (Memento)undo.get(ct);
display.setText(“”+num.getNumber());
undo.remove(ct);
}
if (undo.size() == 0)
display.setText(“0”);
}
void clear() {
list = new ArrayList();
undo = new ArrayList();
counter = 0;
display.setText(“0”);
btnDc.setEnabled(true);
}
}
|
|
MementoDemoDp.java
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.awt.FontMetrics;
class MementoDemoDp extends JFrame
implements ActionListener {
Mediator mediator = new Mediator();
MementoDemoDp() {
JPanel panel = new JPanel();
panel.add(new ButtonDice(this,mediator));
panel.add(new
ButtonPrevious(this,mediator));
panel.add(new ButtonClear(this,mediator));
JPanel dice = new JPanel();
Display disp = new Display(mediator);
dice.add(disp);
getContentPane().add(dice, “Center”);
getContentPane().add(panel, “South”);
setTitle(“Memento pattern example”);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400,200);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
Command cmd = (Command)ae.getSource();
cmd.execute();
}
public static void main(String[] args) {
new MementoDemoDp();
}
} |
|
Jan 2007 | Java Jazz Up | 95 |
|
|
View All Topics |
All Pages of this Issue |
Pages:
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31,
32,
33,
34,
35,
36,
37,
38,
39,
40,
41,
42,
43,
44,
45,
46,
47,
48,
49,
50,
51,
52,
53 ,
54,
55,
56,
57,
58,
59,
60,
61,
62,
63 ,
64,
65 ,
66 ,
67 ,
68 ,
69 ,
70 ,
71 ,
72 ,
73 ,
74 ,
75 ,
76 ,
77 ,
78 ,
79 ,
80 ,
81 ,
82 ,
83,
84 ,
85 ,
86,
87 ,
88,
89 ,
90 ,
91 ,
92 ,
93 ,
94 ,
95 ,
96 ,
97 ,
98 ,
99 ,
100 ,
101 ,
102 ,
103,
104 ,
105 ,
106,
107,
Download PDF |
|
|
|
|
|
|
|
|
|