added lab4 ex1
This commit is contained in:
parent
3a2484a123
commit
2598af8d3a
16
src/lab4_command/ex1/Car.java
Normal file
16
src/lab4_command/ex1/Car.java
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package lab4_command.ex1;
|
||||||
|
|
||||||
|
public class Car {
|
||||||
|
public void forward() {
|
||||||
|
System.out.println("Car goes forward");
|
||||||
|
}
|
||||||
|
public void backward() {
|
||||||
|
System.out.println("Car goes backward");
|
||||||
|
}
|
||||||
|
public void left() {
|
||||||
|
System.out.println("Car goes left");
|
||||||
|
}
|
||||||
|
public void right() {
|
||||||
|
System.out.println("Car goes right");
|
||||||
|
}
|
||||||
|
}
|
10
src/lab4_command/ex1/CarCommand.java
Normal file
10
src/lab4_command/ex1/CarCommand.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package lab4_command.ex1;
|
||||||
|
|
||||||
|
public abstract class CarCommand {
|
||||||
|
protected Car car;
|
||||||
|
public CarCommand(Car car) {
|
||||||
|
this.car = car;
|
||||||
|
}
|
||||||
|
public abstract void execute();
|
||||||
|
public abstract void undo();
|
||||||
|
}
|
38
src/lab4_command/ex1/Game.java
Normal file
38
src/lab4_command/ex1/Game.java
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package lab4_command.ex1;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
|
public class Game {
|
||||||
|
private final Map<Integer, CarCommand> actions = new HashMap<>();
|
||||||
|
private final Stack<CarCommand> history = new Stack<>();
|
||||||
|
public Car car;
|
||||||
|
|
||||||
|
public Game() {
|
||||||
|
car = new Car();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void bindKey(int key, CarCommand command) {
|
||||||
|
actions.put(key, command);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void unbindKey(int key) {
|
||||||
|
actions.remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void pushKey(int key) {
|
||||||
|
if (actions.containsKey(key)) {
|
||||||
|
CarCommand action = actions.get(key);
|
||||||
|
action.execute();
|
||||||
|
history.push(action);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void undo() {
|
||||||
|
if (history.empty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
history.pop().undo();
|
||||||
|
}
|
||||||
|
}
|
25
src/lab4_command/ex1/MobilePhone.java
Normal file
25
src/lab4_command/ex1/MobilePhone.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package lab4_command.ex1;
|
||||||
|
|
||||||
|
import lab4_command.ex1.commands.BackwardCommand;
|
||||||
|
import lab4_command.ex1.commands.ForwardCommand;
|
||||||
|
import lab4_command.ex1.commands.LeftCommand;
|
||||||
|
import lab4_command.ex1.commands.RightCommand;
|
||||||
|
|
||||||
|
public class MobilePhone {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Game game = new Game();
|
||||||
|
|
||||||
|
game.bindKey(0, new ForwardCommand(game.car));
|
||||||
|
game.bindKey(1, new BackwardCommand(game.car));
|
||||||
|
game.bindKey(2, new LeftCommand(game.car));
|
||||||
|
game.bindKey(3, new RightCommand(game.car));
|
||||||
|
|
||||||
|
game.pushKey(2);
|
||||||
|
game.pushKey(2);
|
||||||
|
game.pushKey(3);
|
||||||
|
game.pushKey(2);
|
||||||
|
game.pushKey(0);
|
||||||
|
game.pushKey(1);
|
||||||
|
game.undo();
|
||||||
|
}
|
||||||
|
}
|
20
src/lab4_command/ex1/commands/BackwardCommand.java
Normal file
20
src/lab4_command/ex1/commands/BackwardCommand.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package lab4_command.ex1.commands;
|
||||||
|
|
||||||
|
import lab4_command.ex1.Car;
|
||||||
|
import lab4_command.ex1.CarCommand;
|
||||||
|
|
||||||
|
public class BackwardCommand extends CarCommand {
|
||||||
|
public BackwardCommand(Car car) {
|
||||||
|
super(car);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
car.backward();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void undo() {
|
||||||
|
car.forward();
|
||||||
|
}
|
||||||
|
}
|
20
src/lab4_command/ex1/commands/ForwardCommand.java
Normal file
20
src/lab4_command/ex1/commands/ForwardCommand.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package lab4_command.ex1.commands;
|
||||||
|
|
||||||
|
import lab4_command.ex1.Car;
|
||||||
|
import lab4_command.ex1.CarCommand;
|
||||||
|
|
||||||
|
public class ForwardCommand extends CarCommand {
|
||||||
|
public ForwardCommand(Car car) {
|
||||||
|
super(car);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
car.forward();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void undo() {
|
||||||
|
car.backward();
|
||||||
|
}
|
||||||
|
}
|
20
src/lab4_command/ex1/commands/LeftCommand.java
Normal file
20
src/lab4_command/ex1/commands/LeftCommand.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package lab4_command.ex1.commands;
|
||||||
|
|
||||||
|
import lab4_command.ex1.Car;
|
||||||
|
import lab4_command.ex1.CarCommand;
|
||||||
|
|
||||||
|
public class LeftCommand extends CarCommand {
|
||||||
|
public LeftCommand(Car car) {
|
||||||
|
super(car);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
car.left();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void undo() {
|
||||||
|
car.right();
|
||||||
|
}
|
||||||
|
}
|
20
src/lab4_command/ex1/commands/RightCommand.java
Normal file
20
src/lab4_command/ex1/commands/RightCommand.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package lab4_command.ex1.commands;
|
||||||
|
|
||||||
|
import lab4_command.ex1.Car;
|
||||||
|
import lab4_command.ex1.CarCommand;
|
||||||
|
|
||||||
|
public class RightCommand extends CarCommand {
|
||||||
|
public RightCommand(Car car) {
|
||||||
|
super(car);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
car.right();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void undo() {
|
||||||
|
car.left();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user