Created
June 12, 2014 23:17
-
-
Save ar1g/ffb57083e1bac8f43555 to your computer and use it in GitHub Desktop.
Command pattern demo
This file contains hidden or 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
| package CommandClasses; | |
| /** | |
| * @author Ari | |
| * @version 1.0 | |
| * command interface | |
| */ | |
| public interface Action { | |
| public void exec(); | |
| } |
This file contains hidden or 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
| package CommandClasses; | |
| /** | |
| * @author Ari | |
| * @version 1.0 | |
| * receiver class | |
| */ | |
| public class Bicycle { | |
| private int gear = 1; | |
| public int getGear() { | |
| return gear; | |
| } | |
| public void setGear(int gear) { | |
| this.gear = gear; | |
| } | |
| public void goLeft(){ | |
| System.out.println("L"); | |
| } | |
| public void goRight(){ | |
| System.out.println("R"); | |
| } | |
| } |
This file contains hidden or 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
| package CommandClasses; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| /** | |
| * @author Ari | |
| * @version 1.0 | |
| * invoker class | |
| */ | |
| public class Cyclist { | |
| private List<Action> actionList = new ArrayList<Action>(); | |
| public void setAction(Action action){ | |
| actionList.add(action); | |
| } | |
| public void rideBike(){ | |
| for(Action action: actionList){ | |
| action.exec(); | |
| } | |
| actionList.clear(); | |
| } | |
| } |
This file contains hidden or 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
| package CommandClasses; | |
| /** | |
| * @author Ari | |
| * @version 1.0 | |
| * concrete command | |
| */ | |
| public class DecreaseGear implements Action { | |
| private Bicycle bicycle; | |
| public DecreaseGear(Bicycle bicycle) { | |
| this.bicycle = bicycle; | |
| } | |
| @Override | |
| public void exec() { | |
| int gear = bicycle.getGear(); | |
| if(gear > 1) gear--; | |
| bicycle.setGear(gear); | |
| System.out.println("gear down to " + gear); | |
| } | |
| } |
This file contains hidden or 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
| package CommandClasses; | |
| /** | |
| * @author Ari | |
| * @version 1.0 | |
| * concrete command | |
| */ | |
| public class IncreaseGear implements Action { | |
| private Bicycle bicycle; | |
| public IncreaseGear(Bicycle bicycle) { | |
| this.bicycle = bicycle; | |
| } | |
| @Override | |
| public void exec() { | |
| int gear = bicycle.getGear(); | |
| if(gear<9) bicycle.setGear(gear++); | |
| System.out.println("gear up to " + gear); | |
| } | |
| } |
This file contains hidden or 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
| package CommandClasses; | |
| /** | |
| * @author Ari | |
| * @version 1.0 | |
| * concrete command | |
| */ | |
| public class LeftTurn implements Action { | |
| private Bicycle bicycle; | |
| public LeftTurn(Bicycle bicycle) { | |
| this.bicycle = bicycle; | |
| } | |
| @Override | |
| public void exec() { | |
| bicycle.goLeft(); | |
| } | |
| } |
This file contains hidden or 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
| package CommandClasses; | |
| /** | |
| * @author Ari | |
| * @version 1.0 | |
| * concrete command | |
| */ | |
| public class RightTurn implements Action { | |
| private Bicycle bicycle; | |
| public RightTurn(Bicycle bicycle) { | |
| this.bicycle = bicycle; | |
| } | |
| @Override | |
| public void exec() { | |
| bicycle.goRight(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment