forked from luisburgos/design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Command] Added tv example and pattern notes
- Loading branch information
1 parent
6250b4b
commit 4200062
Showing
12 changed files
with
329 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
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,57 @@ | ||
package command.examples.tv; | ||
|
||
import command.examples.tv.commands.TurnOffAllDevices; | ||
import command.examples.tv.commands.TurnOffTelevision; | ||
import command.examples.tv.commands.TurnOnTelevision; | ||
import command.examples.tv.commands.VolumeUpTelevision; | ||
import command.examples.tv.devices.ElectronicDevice; | ||
import command.examples.tv.devices.Radio; | ||
import command.examples.tv.devices.Television; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by luisburgos on 13/08/15. | ||
*/ | ||
public class Client { | ||
|
||
public static void main(String[] args){ | ||
|
||
ElectronicDevice televisionOne = new Television("SAMSUMG"); | ||
|
||
TurnOnTelevision onCommand = new TurnOnTelevision(televisionOne); | ||
|
||
DeviceButton onPressed; | ||
onPressed = new DeviceButton(onCommand); | ||
onPressed.press(); | ||
|
||
|
||
TurnOffTelevision offCommand = new TurnOffTelevision(televisionOne); | ||
onPressed = new DeviceButton(offCommand); | ||
onPressed.press(); | ||
|
||
VolumeUpTelevision volUpCommand = new VolumeUpTelevision(televisionOne); | ||
onPressed = new DeviceButton(volUpCommand); | ||
onPressed.press(); | ||
onPressed.press(); | ||
onPressed.press(); | ||
|
||
Television televisionTwo = new Television("SONY"); | ||
Radio radioOne = new Radio("PIONEER"); | ||
|
||
List<ElectronicDevice> allDevices = new ArrayList<>(); | ||
|
||
allDevices.add(televisionTwo); | ||
allDevices.add(radioOne); | ||
|
||
TurnOffAllDevices turnOffDevices = new TurnOffAllDevices(allDevices); | ||
|
||
DeviceButton turnThemOff = new DeviceButton(turnOffDevices); | ||
|
||
turnThemOff.press(); | ||
turnThemOff.pressUndo(); | ||
|
||
} | ||
|
||
} |
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 command.examples.tv; | ||
|
||
import command.examples.tv.commands.Command; | ||
|
||
/** | ||
* The INVOKER | ||
* Created by luisburgos on 13/08/15. | ||
*/ | ||
public class DeviceButton { | ||
|
||
private Command command; | ||
|
||
public DeviceButton(Command command){ | ||
this.command = command; | ||
} | ||
|
||
public void press(){ | ||
command.execute(); | ||
} | ||
|
||
public void pressUndo(){ | ||
command.undo(); | ||
} | ||
} |
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,9 @@ | ||
package command.examples.tv.commands; | ||
|
||
/** | ||
* Created by luisburgos on 13/08/15. | ||
*/ | ||
public interface Command { | ||
public void execute(); | ||
public void undo(); | ||
} |
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,29 @@ | ||
package command.examples.tv.commands; | ||
|
||
import command.examples.tv.devices.ElectronicDevice; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Created by luisburgos on 13/08/15. | ||
*/ | ||
public class TurnOffAllDevices implements Command { | ||
|
||
List<ElectronicDevice> allDevices; | ||
|
||
public TurnOffAllDevices(List<ElectronicDevice> newDevices) { | ||
allDevices = newDevices; | ||
} | ||
|
||
public void execute() { | ||
for (ElectronicDevice device : allDevices) { | ||
device.off(); | ||
} | ||
} | ||
|
||
public void undo() { | ||
for (ElectronicDevice device : allDevices) { | ||
device.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,25 @@ | ||
package command.examples.tv.commands; | ||
|
||
import command.examples.tv.devices.ElectronicDevice; | ||
|
||
/** | ||
* Created by luisburgos on 13/08/15. | ||
*/ | ||
public class TurnOffTelevision implements Command { | ||
|
||
private ElectronicDevice device; | ||
|
||
public TurnOffTelevision(ElectronicDevice device){ | ||
this.device = device; | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
device.off(); | ||
} | ||
|
||
@Override | ||
public void undo() { | ||
device.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,25 @@ | ||
package command.examples.tv.commands; | ||
|
||
import command.examples.tv.devices.ElectronicDevice; | ||
|
||
/** | ||
* Created by luisburgos on 13/08/15. | ||
*/ | ||
public class TurnOnTelevision implements Command { | ||
|
||
private ElectronicDevice device; | ||
|
||
public TurnOnTelevision(ElectronicDevice device){ | ||
this.device = device; | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
device.on(); | ||
} | ||
|
||
@Override | ||
public void undo() { | ||
device.off(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/command/examples/tv/commands/VolumeDownTelevision.java
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,27 @@ | ||
package command.examples.tv.commands; | ||
|
||
import command.examples.tv.devices.ElectronicDevice; | ||
|
||
/** | ||
* Created by luisburgos on 13/08/15. | ||
*/ | ||
public class VolumeDownTelevision implements Command { | ||
|
||
private ElectronicDevice device; | ||
|
||
public VolumeDownTelevision(ElectronicDevice device){ | ||
this.device = device; | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
device.volumenDown(); | ||
} | ||
|
||
@Override | ||
public void undo() { | ||
device.volumeUp(); | ||
} | ||
|
||
|
||
} |
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,27 @@ | ||
package command.examples.tv.commands; | ||
|
||
import command.examples.tv.devices.ElectronicDevice; | ||
|
||
/** | ||
* Created by luisburgos on 13/08/15. | ||
*/ | ||
public class VolumeUpTelevision implements Command { | ||
|
||
private ElectronicDevice device; | ||
|
||
public VolumeUpTelevision(ElectronicDevice device){ | ||
this.device = device; | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
device.volumeUp(); | ||
} | ||
|
||
@Override | ||
public void undo() { | ||
device.volumenDown(); | ||
} | ||
|
||
|
||
} |
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,11 @@ | ||
package command.examples.tv.devices; | ||
|
||
/** | ||
* Created by luisburgos on 13/08/15. | ||
*/ | ||
public interface ElectronicDevice { | ||
public void on(); | ||
public void off(); | ||
public void volumeUp(); | ||
public void volumenDown(); | ||
} |
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,36 @@ | ||
package command.examples.tv.devices; | ||
|
||
/** | ||
* Created by luisburgos on 13/08/15. | ||
*/ | ||
public class Radio implements ElectronicDevice { | ||
|
||
private int volume = 0; | ||
private String name; | ||
|
||
public Radio(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public void on() { | ||
System.out.println(name + "RADIO is on"); | ||
} | ||
|
||
@Override | ||
public void off() { | ||
System.out.println(name + "RADIO is off"); | ||
} | ||
|
||
@Override | ||
public void volumeUp() { | ||
volume++; | ||
System.out.println(name + "RADIO Volume at: " + volume); | ||
} | ||
|
||
@Override | ||
public void volumenDown() { | ||
volume--; | ||
System.out.println(name + "RADIO Volume at: " + volume); | ||
} | ||
} |
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,36 @@ | ||
package command.examples.tv.devices; | ||
|
||
/** | ||
* Created by luisburgos on 13/08/15. | ||
*/ | ||
public class Television implements ElectronicDevice { | ||
|
||
private int volume = 0; | ||
private String name; | ||
|
||
public Television(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public void on() { | ||
System.out.println(name + "TV is on"); | ||
} | ||
|
||
@Override | ||
public void off() { | ||
System.out.println(name + "TV is off"); | ||
} | ||
|
||
@Override | ||
public void volumeUp() { | ||
volume++; | ||
System.out.println(name + "TV Volume at: " + volume); | ||
} | ||
|
||
@Override | ||
public void volumenDown() { | ||
volume--; | ||
System.out.println(name + "TV Volume at: " + volume); | ||
} | ||
} |