Skip to content

Commit

Permalink
[Refactoring] Command pattern of actionPerformed() in LaneView.java
Browse files Browse the repository at this point in the history
  • Loading branch information
inrir committed May 30, 2023
1 parent 9212bfe commit 0016ee7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
14 changes: 13 additions & 1 deletion LaneView.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class LaneView implements LaneObserver, ActionListener {

JButton maintenance;
Lane lane;
private ButtonCommand command; // the Command instance for Command pattern

public LaneView(Lane lane, int laneNum) {

Expand Down Expand Up @@ -216,8 +217,19 @@ private void updateBallScores(LaneEvent le, int k){ // 점수에 대해서 전

public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(maintenance)) {
lane.pauseGame();
setCommand(new LaneViewMaintenanceCommand(this));
}
buttonPressed();
}

// The Invoker holds a command and can use it to call a method
public void setCommand(ButtonCommand command) {
this.command = command;
}
// Call the execute() method of the command
public void buttonPressed() {
command.execute();
}


}
11 changes: 11 additions & 0 deletions LaneViewMaintenanceCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class LaneViewMaintenanceCommand implements ButtonCommand {
private LaneView theView;

public LaneViewMaintenanceCommand(LaneView theView) {
this.theView = theView;
}

public void execute() {
theView.lane.pauseGame();
}
}

0 comments on commit 0016ee7

Please sign in to comment.