-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
451f6a6
commit bc8ae5e
Showing
12 changed files
with
290 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package patterns.command; | ||
|
||
import patterns.command.commands.Command; | ||
import patterns.command.commands.NoOpCommand; | ||
|
||
public class AlexaDevice { | ||
Command[] onCommands; | ||
Command[] offCommands; | ||
int slots = 3; | ||
|
||
public AlexaDevice() { | ||
onCommands = new Command[slots]; | ||
offCommands = new Command[slots]; | ||
|
||
for (int i = 0; i < slots; i++) { | ||
onCommands[i] = new NoOpCommand(); | ||
offCommands[i] = new NoOpCommand(); | ||
} | ||
} | ||
|
||
public void setCommand(int slot, Command onCommand, Command offCommand) { | ||
//slot between 0..2, neither commands can be nll | ||
if (onCommand == null || offCommand == null) { | ||
throw new IllegalArgumentException("Commands cannot be null!"); | ||
} else { | ||
onCommands[slot] = onCommand; | ||
offCommands[slot] = offCommand; | ||
} | ||
} | ||
|
||
public void activateSlot(int slot) { | ||
onCommands[slot].execute(); | ||
} | ||
|
||
public void deactivateSlot(int slot) { | ||
offCommands[slot].execute(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("\n-------- Alexa Device Controller --------\n"); | ||
for (int i = 0; i < slots; i++) { | ||
sb.append("Slot #" + i + " - " + onCommands[i].getClass().getSimpleName() + "(" + onCommands.getClass().getSimpleName() + ")" + | ||
" - " + offCommands[i].getClass().getSimpleName() + "(" + offCommands.getClass().getSimpleName() + ")" + | ||
"\n"); | ||
} | ||
return sb.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package patterns.command; | ||
|
||
import patterns.command.commands.EverythingOffCommand; | ||
import patterns.command.commands.EverythingOnCommand; | ||
import patterns.command.commands.KitchenLightsOffCommand; | ||
import patterns.command.commands.KitchenLightsOnCommand; | ||
import patterns.command.hardware.KitchenLights; | ||
import patterns.command.hardware.Music; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
//Example 1 - Empty slots | ||
AlexaDevice alexa = new AlexaDevice(); | ||
System.out.println(alexa); | ||
|
||
// --------------------------------------------------------------- | ||
|
||
//Example 2 - Kitchen Lights on | ||
// AlexaDevice alexa = new AlexaDevice(); | ||
// | ||
// //Create an instance | ||
// KitchenLights kitchenLights = new KitchenLights(); | ||
// | ||
// //Set the slots with new commands | ||
// alexa.setCommand( | ||
// 0, | ||
// new KitchenLightsOnCommand(kitchenLights), | ||
// new KitchenLightsOffCommand(kitchenLights) | ||
// ); | ||
// | ||
// System.out.println(alexa); | ||
// | ||
// alexa.activateSlot(0); | ||
// alexa.deactivateSlot(0); | ||
|
||
// --------------------------------------------------------------- | ||
|
||
//Example 3 - Everything on! | ||
// AlexaDevice alexa = new AlexaDevice(); | ||
// | ||
// //Create an instance | ||
// KitchenLights kitchenLights = new KitchenLights(); | ||
// Music music = new Music(); | ||
// | ||
// //Set the slots with new commands | ||
// alexa.setCommand( | ||
// 1, | ||
// new EverythingOnCommand(music, kitchenLights), | ||
// new EverythingOffCommand(music, kitchenLights) | ||
// ); | ||
// | ||
// System.out.println(alexa); | ||
// | ||
// alexa.activateSlot(1); | ||
// alexa.deactivateSlot(1); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package patterns.command.commands; | ||
|
||
public interface Command { | ||
public void execute(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package patterns.command.commands; | ||
|
||
import patterns.command.hardware.KitchenLights; | ||
import patterns.command.hardware.Music; | ||
|
||
public class EverythingOffCommand implements Command { | ||
Music music; | ||
KitchenLights kitchenLights; | ||
|
||
public EverythingOffCommand(Music music, KitchenLights kitchenLights) { | ||
this.music = music; | ||
this.kitchenLights = kitchenLights; | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
System.out.println("--- PARTY TIME OVER! ---"); | ||
kitchenLights.off(); | ||
music.off(); | ||
music.setVolume(0); | ||
music.setStation(0); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package patterns.command.commands; | ||
|
||
import patterns.command.hardware.KitchenLights; | ||
import patterns.command.hardware.Music; | ||
|
||
public class EverythingOnCommand implements Command { | ||
Music music; | ||
KitchenLights kitchenLights; | ||
|
||
public EverythingOnCommand(Music music, KitchenLights kitchenLights) { | ||
this.music = music; | ||
this.kitchenLights = kitchenLights; | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
System.out.println("--- ITS PARTY TIME! ---"); | ||
kitchenLights.on(); | ||
music.on(); | ||
music.setVolume(10); | ||
music.setStation(102.1F); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package patterns.command.commands; | ||
|
||
import patterns.command.hardware.KitchenLights; | ||
|
||
public class KitchenLightsOffCommand implements Command { | ||
KitchenLights kitchenLights; | ||
|
||
public KitchenLightsOffCommand(KitchenLights kitchenLights) { | ||
this.kitchenLights= kitchenLights; | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
kitchenLights.off(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package patterns.command.commands; | ||
|
||
import patterns.command.hardware.KitchenLights; | ||
|
||
public class KitchenLightsOnCommand implements Command { | ||
KitchenLights kitchenLights; | ||
|
||
public KitchenLightsOnCommand(KitchenLights kitchenLights) { | ||
this.kitchenLights= kitchenLights; | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
kitchenLights.on(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package patterns.command.commands; | ||
|
||
import patterns.command.hardware.Music; | ||
|
||
public class MusicOffCommand implements Command { | ||
Music music; | ||
|
||
public MusicOffCommand(Music music) { | ||
this.music = music; | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
music.setVolume(0); | ||
music.setStation(0); | ||
music.off(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package patterns.command.commands; | ||
|
||
import patterns.command.hardware.Music; | ||
|
||
public class MusicOnCommand implements Command { | ||
Music music; | ||
|
||
public MusicOnCommand(Music music) { | ||
this.music = music; | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
music.on(); | ||
music.setVolume(5); | ||
music.setStation(99.9F); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package patterns.command.commands; | ||
|
||
public class NoOpCommand implements Command { | ||
public void execute() { | ||
|
||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package patterns.command.hardware; | ||
|
||
public class KitchenLights { | ||
|
||
public void on() { | ||
System.out.println("Turning on Kitchen Lights"); | ||
//activate hardware... | ||
} | ||
|
||
public void off() { | ||
System.out.println("Turning off Kitchen Lights"); | ||
//deactivate hardware | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package patterns.command.hardware; | ||
|
||
public class Music { | ||
private int volume; | ||
private float station; | ||
|
||
public int getVolume() { | ||
return volume; | ||
} | ||
|
||
public void setVolume(int volume) { | ||
System.out.println("Setting volume to: " + volume); | ||
this.volume = volume; | ||
} | ||
|
||
public float getStation() { | ||
return station; | ||
} | ||
|
||
public void setStation(float station) { | ||
System.out.println("Setting station to: " + station ); | ||
this.station = station; | ||
} | ||
|
||
public void on() { | ||
System.out.println("Turning on Music"); | ||
//activate hardware... | ||
} | ||
|
||
public void off() { | ||
System.out.println("Turning off Music"); | ||
//deactivate hardware | ||
|
||
} | ||
} |