-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediator.js
71 lines (60 loc) · 1.51 KB
/
mediator.js
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
// The mediator pattern.
// If you have more then 5 object, for interaction between them will be useful to use a mediator object.
// The objects.
function Daddy() { }
Daddy.prototype =
{
constructor: Daddy,
getBeer: function ()
{
if (!kitchen.tryToGetBeer())
{
console.log("Daddy: Who the hell drank all my beer?");
return false;
}
console.log("Daddy: Yeeah! My beer!");
kitchen.oneBeerHasGone();
return true;
},
argue_back: function () { console.log("Daddy: it's my last beer, for shure!"); }
}
function Mammy() { }
Mammy.prototype =
{
constructor: Mammy,
argue: function ()
{
console.log("Mammy: You are f*king alconaut!");
kitchen.disputeStarted();
}
}
function BeerStorage(beer_bottle_count)
{
this._beer_bottle_count = beer_bottle_count;
}
BeerStorage.prototype =
{
constructor: BeerStorage,
takeOneBeerAway: function ()
{
if (this._beer_bottle_count == 0) return false;
this._beer_bottle_count--;
return true;
}
}
// The mediator.
var kitchen =
{
daddy: new Daddy(),
mammy: new Mammy(),
refrigerator: new BeerStorage(3),
stash: new BeerStorage(2),
tryToGetBeer: function ()
{
if (this.refrigerator.takeOneBeerAway()) return true;
if (this.stash.takeOneBeerAway()) return true;
return false
},
oneBeerHasGone: function (){ this.mammy.argue(); },
disputeStarted: function (){ this.daddy.argue_back(); }
}