-
Notifications
You must be signed in to change notification settings - Fork 1
/
3-9-state.vala
55 lines (41 loc) · 1.08 KB
/
3-9-state.vala
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
interface WritingState : Object {
public abstract void write (string words);
}
class UpperCase : Object, WritingState {
public void write (string words) {
print ("%s\n", words.up ());
}
}
class LowerCase : Object, WritingState {
public void write (string words) {
print ("%s\n", words.down ());
}
}
class Default : Object, WritingState {
public void write (string words) {
print ("%s\n", words);
}
}
class TextEditor {
protected WritingState state;
public TextEditor (WritingState state) {
this.state = state;
}
public void set_state (WritingState state) {
this.state = state;
}
public void type (string words) {
state.write (words);
}
}
public int main (string[] args) {
var editor = new TextEditor (new Default ());
editor.type ("First line");
editor.set_state (new UpperCase ());
editor.type ("Second line");
editor.type ("Third line");
editor.set_state (new LowerCase ());
editor.type ("Fourth line");
editor.type ("Fifth line");
return 0;
}