-
Notifications
You must be signed in to change notification settings - Fork 1
/
3-4-mediator.vala
41 lines (32 loc) · 1004 Bytes
/
3-4-mediator.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
interface ChatRoomMediator : Object {
public abstract void show_message (User user, string message);
}
class ChatRoom : Object, ChatRoomMediator {
public void show_message (User user, string message) {
var time = new DateTime.now_local ();
var sender = user.get_name ();
print ("%s [%s]:%s\n", time.to_string (), sender, message);
}
}
class User {
protected string name;
protected ChatRoomMediator chat_mediator;
public User (string name, ChatRoomMediator chat_mediator) {
this.name = name;
this.chat_mediator = chat_mediator;
}
public string get_name () {
return name;
}
public void send (string message) {
chat_mediator.show_message (this, message);
}
}
public int main (string[] args) {
var mediator = new ChatRoom ();
var john = new User ("John Doe", mediator);
var jane = new User ("Jane Dow", mediator);
john.send ("Hi there!");
jane.send ("Hey!");
return 0;
}