Here is a summary of the ** State pattern ** in the GoF design pattern.
--The English word State means ** state **. --The State pattern is a method that expresses a state as a class and expresses a "change of state" by switching classes **. --The GoF design patterns are classified as ** behavioral design patterns **.
It is a safe management program that changes the operation of buttons depending on the day and night conditions.
An interface that manages changes in the state of the safe and keeps in touch with the security center.
Context.java
public interface Context {
//Time setting
public abstract void setClock(int hour);
//Change of state
public abstract void changeState(State state);
//Security Center Security Guard Call
public abstract void callSecurityCenter(String msg);
//Security center record
public abstract void recordLog(String msg);
}
A class that implements the Context interface. It has a UI such as buttons and screen display.
SafeFrame.java
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SafeFrame extends Frame implements ActionListener, Context {
private TextField textClock = new TextField(60);
private TextArea textScreen = new TextArea(10, 60);
private Button buttonUse = new Button("Safe use");
private Button buttonAlarm = new Button("emergency bell");
private Button buttonPhone = new Button("Normal call");
private Button buttonExit = new Button("End");
private State state = DayState.getInstance();
public SafeFrame(String title) {
super(title);
setBackground(Color.lightGray);
setLayout(new BorderLayout());
add(textClock, BorderLayout.NORTH);
textClock.setEditable(false);
add(textScreen, BorderLayout.CENTER);
textScreen.setEditable(false);
Panel panel = new Panel();
panel.add(buttonUse);
panel.add(buttonAlarm);
panel.add(buttonPhone);
panel.add(buttonExit);
add(panel, BorderLayout.SOUTH);
pack();
show();
buttonUse.addActionListener(this);
buttonAlarm.addActionListener(this);
buttonPhone.addActionListener(this);
buttonExit.addActionListener(this);
}
//Come here when the button is pressed
public void actionPerformed(ActionEvent e) {
System.out.println(e.toString());
if (e.getSource() == buttonUse) {
//Safe use button
state.doUse(this);
} else if (e.getSource() == buttonAlarm) {
//Emergency bell button
state.doAlarm(this);
} else if (e.getSource() == buttonPhone) {
//Normal call button
state.doPhone(this);
} else if (e.getSource() == buttonExit) {
//Finish button
System.exit(0);
} else {
System.out.println("?");
}
}
//Time setting
public void setClock(int hour) {
String clockstring = "The current time is";
if (hour < 10) {
clockstring += "0" + hour + ":00";
} else {
clockstring += hour + ":00";
}
System.out.println(clockstring);
textClock.setText(clockstring);
state.doClock(this, hour);
}
//Change of state
public void changeState(State state) {
System.out.println(this.state + "From" + state + "The state has changed.");
this.state = state;
}
//Security Center Security Guard Call
public void callSecurityCenter(String msg) {
textScreen.append("call! " + msg + "\n");
}
//Security center record
public void recordLog(String msg) {
textScreen.append("record ... " + msg + "\n");
}
}
An interface that shows the status of the safe.
State.java
public interface State {
//Time setting
public abstract void doClock(Context context, int hour);
//Safe use
public abstract void doUse(Context context);
//emergency bell
public abstract void doAlarm(Context context);
//Normal call
public abstract void doPhone(Context context);
}
A class that implements the State interface. Represents the daytime condition.
DayState.java
public class DayState implements State {
private static DayState singleton = new DayState();
private DayState() {
}
public static State getInstance() {
return singleton;
}
public void doClock(Context context, int hour) {
if (hour < 9 || 17 <= hour) {
context.changeState(NightState.getInstance());
}
}
public void doUse(Context context) {
context.recordLog("Safe use(Daytime)");
}
public void doAlarm(Context context) {
context.callSecurityCenter("emergency bell(Daytime)");
}
public void doPhone(Context context) {
context.callSecurityCenter("Normal call(Daytime)");
}
public String toString() {
return "[Daytime]";
}
}
A class that implements the State interface. Represents the nighttime condition
NightState.java
public class NightState implements State {
private static NightState singleton = new NightState();
private NightState() {
}
public static State getInstance() {
return singleton;
}
public void doClock(Context context, int hour) {
if (9 <= hour && hour < 17) {
context.changeState(DayState.getInstance());
}
}
public void doUse(Context context) {
context.callSecurityCenter("Emergency: Use the safe at night!");
}
public void doAlarm(Context context) {
context.callSecurityCenter("emergency bell(Night)");
}
public void doPhone(Context context) {
context.recordLog("Night call recording");
}
public String toString() {
return "[Night]";
}
}
This class performs the main processing.
Main.java
public class Main {
public static void main(String[] args) {
SafeFrame frame = new SafeFrame("State Sample");
while (true) {
for (int hour = 0; hour < 24; hour++) {
frame.setClock(hour);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}
}
The policy of ** splitting and integrating ** is common in programming. The State pattern represents a state as a class. Each concrete state is expressed as a separate class and divided. While implementing DayState, programmers no longer need to be aware of other classes (NightState). In the sample program, there were only two states, but the State pattern will be effective when there are more states.
-** GoF design pattern summary **
This article and sample program were created based on the following books.
-** Introduction to design patterns learned in Java language **
It was very easy to understand and I learned a lot. Thank you. The detailed explanations of the design patterns and sample programs are written, so please take a look at the books as well.
Recommended Posts