Skip to content

Commit 5258744

Browse files
committed
BAEL-2836 Mediator Pattern in Java
1 parent fda8a29 commit 5258744

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.mediator;
2+
3+
public class Button {
4+
private Mediator mediator;
5+
6+
public void setMediator(Mediator mediator) {
7+
this.mediator = mediator;
8+
}
9+
10+
public void press() {
11+
this.mediator.press();
12+
}
13+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.mediator;
2+
3+
public class Fan {
4+
private Mediator mediator;
5+
private boolean isOn = false;
6+
7+
public void setMediator(Mediator mediator) {
8+
this.mediator = mediator;
9+
}
10+
11+
public boolean isOn() {
12+
return isOn;
13+
}
14+
15+
public void turnOn() {
16+
this.mediator.start();
17+
isOn = true;
18+
}
19+
20+
public void turnOff() {
21+
isOn = false;
22+
this.mediator.stop();
23+
}
24+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.mediator;
2+
3+
public class Mediator {
4+
private Button button;
5+
private Fan fan;
6+
private PowerSupplier powerSupplier;
7+
8+
public void setButton(Button button) {
9+
this.button = button;
10+
this.button.setMediator(this);
11+
}
12+
13+
public void setFan(Fan fan) {
14+
this.fan = fan;
15+
this.fan.setMediator(this);
16+
}
17+
18+
public void setPowerSupplier(PowerSupplier powerSupplier) {
19+
this.powerSupplier = powerSupplier;
20+
}
21+
22+
public void press() {
23+
if (fan.isOn()) {
24+
fan.turnOff();
25+
} else {
26+
fan.turnOn();
27+
}
28+
}
29+
30+
public void start() {
31+
powerSupplier.turnOn();
32+
}
33+
34+
public void stop() {
35+
powerSupplier.turnOff();
36+
}
37+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.mediator;
2+
3+
public class PowerSupplier {
4+
public void turnOn() {
5+
// implementation
6+
}
7+
8+
public void turnOff() {
9+
// implementation
10+
}
11+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.mediator;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertFalse;
7+
import static org.junit.Assert.assertTrue;
8+
9+
public class ButtonTest {
10+
11+
private Button button;
12+
private Fan fan;
13+
14+
@Before
15+
public void setUp() {
16+
this.button = new Button();
17+
this.fan = new Fan();
18+
PowerSupplier powerSupplier = new PowerSupplier();
19+
Mediator mediator = new Mediator();
20+
21+
mediator.setButton(this.button);
22+
mediator.setFan(fan);
23+
mediator.setPowerSupplier(powerSupplier);
24+
}
25+
26+
@Test
27+
public void givenTurnedOffFan_whenPressingButtonTwice_fanShouldTurnOnAndOff() {
28+
assertFalse(fan.isOn());
29+
30+
button.press();
31+
assertTrue(fan.isOn());
32+
33+
button.press();
34+
assertFalse(fan.isOn());
35+
}
36+
}

0 commit comments

Comments
 (0)