diff --git a/patterns/design-patterns-2/src/main/java/com/baeldung/mediator/Button.java b/patterns/design-patterns-2/src/main/java/com/baeldung/mediator/Button.java new file mode 100644 index 0000000000..0507d27872 --- /dev/null +++ b/patterns/design-patterns-2/src/main/java/com/baeldung/mediator/Button.java @@ -0,0 +1,13 @@ +package com.baeldung.mediator; + +public class Button { + private Mediator mediator; + + public void setMediator(Mediator mediator) { + this.mediator = mediator; + } + + public void press() { + this.mediator.press(); + } +} diff --git a/patterns/design-patterns-2/src/main/java/com/baeldung/mediator/Fan.java b/patterns/design-patterns-2/src/main/java/com/baeldung/mediator/Fan.java new file mode 100644 index 0000000000..b7862f42d5 --- /dev/null +++ b/patterns/design-patterns-2/src/main/java/com/baeldung/mediator/Fan.java @@ -0,0 +1,24 @@ +package com.baeldung.mediator; + +public class Fan { + private Mediator mediator; + private boolean isOn = false; + + public void setMediator(Mediator mediator) { + this.mediator = mediator; + } + + public boolean isOn() { + return isOn; + } + + public void turnOn() { + this.mediator.start(); + isOn = true; + } + + public void turnOff() { + isOn = false; + this.mediator.stop(); + } +} diff --git a/patterns/design-patterns-2/src/main/java/com/baeldung/mediator/Mediator.java b/patterns/design-patterns-2/src/main/java/com/baeldung/mediator/Mediator.java new file mode 100644 index 0000000000..08cd023748 --- /dev/null +++ b/patterns/design-patterns-2/src/main/java/com/baeldung/mediator/Mediator.java @@ -0,0 +1,37 @@ +package com.baeldung.mediator; + +public class Mediator { + private Button button; + private Fan fan; + private PowerSupplier powerSupplier; + + public void setButton(Button button) { + this.button = button; + this.button.setMediator(this); + } + + public void setFan(Fan fan) { + this.fan = fan; + this.fan.setMediator(this); + } + + public void setPowerSupplier(PowerSupplier powerSupplier) { + this.powerSupplier = powerSupplier; + } + + public void press() { + if (fan.isOn()) { + fan.turnOff(); + } else { + fan.turnOn(); + } + } + + public void start() { + powerSupplier.turnOn(); + } + + public void stop() { + powerSupplier.turnOff(); + } +} diff --git a/patterns/design-patterns-2/src/main/java/com/baeldung/mediator/PowerSupplier.java b/patterns/design-patterns-2/src/main/java/com/baeldung/mediator/PowerSupplier.java new file mode 100644 index 0000000000..b4a05816c7 --- /dev/null +++ b/patterns/design-patterns-2/src/main/java/com/baeldung/mediator/PowerSupplier.java @@ -0,0 +1,11 @@ +package com.baeldung.mediator; + +public class PowerSupplier { + public void turnOn() { + // implementation + } + + public void turnOff() { + // implementation + } +} diff --git a/patterns/design-patterns-2/src/test/java/com/baeldung/mediator/MediatorIntegrationTest.java b/patterns/design-patterns-2/src/test/java/com/baeldung/mediator/MediatorIntegrationTest.java new file mode 100644 index 0000000000..0bda659f3f --- /dev/null +++ b/patterns/design-patterns-2/src/test/java/com/baeldung/mediator/MediatorIntegrationTest.java @@ -0,0 +1,36 @@ +package com.baeldung.mediator; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class MediatorIntegrationTest { + + private Button button; + private Fan fan; + + @Before + public void setUp() { + this.button = new Button(); + this.fan = new Fan(); + PowerSupplier powerSupplier = new PowerSupplier(); + Mediator mediator = new Mediator(); + + mediator.setButton(this.button); + mediator.setFan(fan); + mediator.setPowerSupplier(powerSupplier); + } + + @Test + public void givenTurnedOffFan_whenPressingButtonTwice_fanShouldTurnOnAndOff() { + assertFalse(fan.isOn()); + + button.press(); + assertTrue(fan.isOn()); + + button.press(); + assertFalse(fan.isOn()); + } +} \ No newline at end of file