Skip to content

Commit

Permalink
Command Pattern Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
awssimplified committed Dec 12, 2020
1 parent 451f6a6 commit bc8ae5e
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 0 deletions.
50 changes: 50 additions & 0 deletions patterns/command/AlexaDevice.java
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();
}
}
59 changes: 59 additions & 0 deletions patterns/command/Main.java
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);

}
}
5 changes: 5 additions & 0 deletions patterns/command/commands/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package patterns.command.commands;

public interface Command {
public void execute();
}
24 changes: 24 additions & 0 deletions patterns/command/commands/EverythingOffCommand.java
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);
}

}
24 changes: 24 additions & 0 deletions patterns/command/commands/EverythingOnCommand.java
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);
}

}
17 changes: 17 additions & 0 deletions patterns/command/commands/KitchenLightsOffCommand.java
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();
}

}
17 changes: 17 additions & 0 deletions patterns/command/commands/KitchenLightsOnCommand.java
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();
}

}
19 changes: 19 additions & 0 deletions patterns/command/commands/MusicOffCommand.java
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();
}

}
19 changes: 19 additions & 0 deletions patterns/command/commands/MusicOnCommand.java
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);
}

}
7 changes: 7 additions & 0 deletions patterns/command/commands/NoOpCommand.java
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() {

};
}
14 changes: 14 additions & 0 deletions patterns/command/hardware/KitchenLights.java
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
}
}
35 changes: 35 additions & 0 deletions patterns/command/hardware/Music.java
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

}
}

0 comments on commit bc8ae5e

Please sign in to comment.