File tree Expand file tree Collapse file tree 5 files changed +121
-0
lines changed
patterns/design-patterns-2/src
main/java/com/baeldung/mediator
test/java/com/baeldung/mediator Expand file tree Collapse file tree 5 files changed +121
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 MediatorIntegrationTest {
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+ }
You can’t perform that action at this time.
0 commit comments