Skip to content

Commit 684c820

Browse files
committed
Add "State"
1 parent 7564abb commit 684c820

File tree

7 files changed

+164
-0
lines changed

7 files changed

+164
-0
lines changed

State/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Stateパターン

State/src/Context.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
public interface Context {
3+
public abstract void setClock(int hour);
4+
public abstract void changeState(State state);
5+
public abstract void callSecurityCenter(String msg);
6+
public abstract void recordLog(String msg);
7+
}

State/src/DayState.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
public class DayState implements State {
3+
private static DayState instance = new DayState();
4+
private DayState() {}
5+
public static DayState getInstance() {
6+
return DayState.instance;
7+
}
8+
public void doClock(Context context, int hour) {
9+
if (hour < 9 || 17 <= hour) {
10+
context.changeState(NightState.getInstance());
11+
}
12+
}
13+
public void doUse(Context context) {
14+
context.recordLog("Use (daytime)");
15+
}
16+
public void doAlarm(Context context) {
17+
context.callSecurityCenter("Alerm (daytime)");
18+
}
19+
public void doPhone(Context context) {
20+
context.callSecurityCenter("Phone (daytime)");
21+
}
22+
public String toString() {
23+
return "DAYTIME";
24+
}
25+
}

State/src/Main.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
public class Main extends Thread {
3+
public static void main(String[] args) {
4+
SafeFrame frame = new SafeFrame("State Sample");
5+
while (true) {
6+
for (int hour = 0; hour < 24; hour++) {
7+
frame.setClock(hour);
8+
try {
9+
Thread.sleep(1000);
10+
} catch (InterruptedException e) {}
11+
}
12+
}
13+
}
14+
}

State/src/NightState.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
public class NightState implements State {
3+
private static NightState instance = new NightState();
4+
private NightState() {}
5+
public static NightState getInstance() {
6+
return NightState.instance;
7+
}
8+
public void doClock(Context context, int hour) {
9+
if (9 <= hour && hour < 17) {
10+
context.changeState(DayState.getInstance());
11+
}
12+
}
13+
public void doUse(Context context) {
14+
context.callSecurityCenter("Use in night (emergency)");
15+
}
16+
public void doAlarm(Context context) {
17+
context.callSecurityCenter("Ringing alarm (nighttime)");
18+
}
19+
public void doPhone(Context context) {
20+
context.recordLog("Phone (nighttime)");
21+
}
22+
public String toString() {
23+
return "NIGHTTIME";
24+
}
25+
}

State/src/SafeFrame.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import java.awt.Frame;
2+
import java.awt.Label;
3+
import java.awt.Color;
4+
import java.awt.Button;
5+
import java.awt.TextField;
6+
import java.awt.TextArea;
7+
import java.awt.Panel;
8+
import java.awt.BorderLayout;
9+
import java.awt.event.ActionListener;
10+
import java.awt.event.ActionEvent;
11+
12+
public class SafeFrame extends Frame implements ActionListener, Context {
13+
private TextField clock = new TextField(60);
14+
private TextArea screen = new TextArea(10, 60);
15+
private Button btnUse = new Button("金庫使用");
16+
private Button btnAlarm = new Button("非常ベル");
17+
private Button btnPhone = new Button("通常電話");
18+
private Button btnExit = new Button("終了");
19+
20+
// private State currentState = DayState.getInstance();
21+
private State currentState = (State)NightState.getInstance();
22+
23+
public SafeFrame(String title) {
24+
super(title);
25+
this.setBackground(Color.lightGray);
26+
this.setLayout(new BorderLayout());
27+
28+
this.add(this.clock, BorderLayout.NORTH);
29+
this.clock.setEditable(false);
30+
31+
this.add(this.screen, BorderLayout.CENTER);
32+
this.screen.setEditable(false);
33+
34+
Panel panel = new Panel();
35+
panel.add(this.btnUse);
36+
panel.add(this.btnAlarm);
37+
panel.add(this.btnPhone);
38+
panel.add(this.btnExit);
39+
this.add(panel, BorderLayout.SOUTH);
40+
41+
this.pack();
42+
// this.show();
43+
this.setVisible(true);
44+
45+
this.btnUse.addActionListener(this);
46+
this.btnAlarm.addActionListener(this);
47+
this.btnPhone.addActionListener(this);
48+
this.btnExit.addActionListener(this);
49+
}
50+
51+
public void actionPerformed(ActionEvent e) {
52+
if (e.getSource() == this.btnUse) {
53+
this.currentState.doUse(this);
54+
} else if (e.getSource() == this.btnAlarm) {
55+
this.currentState.doAlarm(this);
56+
} else if (e.getSource() == this.btnPhone) {
57+
this.currentState.doPhone(this);
58+
} else if (e.getSource() == this.btnExit) {
59+
System.exit(0);
60+
} else {
61+
System.out.println("????");
62+
}
63+
}
64+
65+
public void setClock(int hour) {
66+
String clockstring = "現在時刻は";
67+
if (hour < 10) {
68+
clockstring += "0" + hour + ":00";
69+
} else {
70+
clockstring += hour + ":00";
71+
}
72+
this.clock.setText(clockstring);
73+
this.currentState.doClock(this, hour);
74+
}
75+
public void changeState(State state) {
76+
System.out.println("state has changed to " + state);
77+
this.currentState = state;
78+
}
79+
public void callSecurityCenter(String msg) {
80+
this.screen.append("Call! " + msg + "\n");
81+
}
82+
public void recordLog(String msg) {
83+
this.screen.append("Log.. " + msg + "\n");
84+
}
85+
}

State/src/State.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
public interface State {
3+
public abstract void doClock(Context context, int hour);
4+
public abstract void doUse(Context context);
5+
public abstract void doAlarm(Context context);
6+
public abstract void doPhone(Context context);
7+
}

0 commit comments

Comments
 (0)