Young Jun Park (박영준)
Java back-end developer
Github : https://github.com/june0313
Y3S Study group : https://github.com/y3s-study
2018.08.07
Park Young Jun
class Light {
private static int ON = 0;
private static int OFF = 1;
private int state;
public Light() {
this.state = OFF;
}
public void onButtonPushed() {
if (state == ON) {
System.out.println("no action");
} else {
System.out.println("Light On");
state = ON;
}
}
public void offButtonPushed() {
if (state == OFF) {
System.out.println("no action");
} else {
System.out.println("Light Off");
state = OFF;
}
}
}
class Light {
private static int ON = 0;
private static int OFF = 1;
private static int SLEEPING = 2; // 취침등 상태 추가
private int state;
public Light() {
this.state = OFF;
}
public void onButtonPushed() {
if (state == ON) {
System.out.println("취침등 상태");
state = SLEEPING;
} else if (state == SLEEPING) { // 취침등 조건 추가
System.out.println("Light On");
state = ON;
} else {
System.out.println("Light On");
state = ON;
}
}
public void offButtonPushed() {
if (state == OFF) {
System.out.println("no action");
} else if (state == SLEEPING) { // 취침등 조건 추가
System.out.println("Light Off");
state = OFF;
} else {
System.out.println("Light Off");
state = OFF;
}
}
}
class Light {
private static int ON = 0;
private static int OFF = 1;
private static int SLEEPING = 2; // 취침등 상태 추가
private int state;
public Light() {
this.state = OFF;
}
public void onButtonPushed() {
if (state == ON) {
System.out.println("취침등 상태");
state = SLEEPING;
} else if (state == SLEEPING) { // 취침등 조건 추가
System.out.println("Light On");
state = ON;
} else {
System.out.println("Light On");
state = ON;
}
}
public void offButtonPushed() {
if (state == OFF) {
System.out.println("no action");
} else if (state == SLEEPING) { // 취침등 조건 추가
System.out.println("Light Off");
state = OFF;
} else {
System.out.println("Light Off");
state = OFF;
}
}
}
State 인터페이스 정의
public interface State {
void onButtonPushed(Light light);
void offButtonPushed(Light light);
}
각 상태별 구현체 구현
public class OFF implements State {
private static OFF off = new OFF();
private OFF () {}
public static OFF getInstance() {
return off;
}
@Override
public void onButtonPushed(Light light) {
System.out.println("Light On");
light.setState(ON.getInstance());
}
@Override
public void offButtonPushed(Light light) {
System.out.println("noAction");
}
}
public class ON implements State {
private static ON on = new ON();
private ON() {}
public static ON getInstance() {
return on;
}
@Override
public void onButtonPushed(Light light) {
System.out.println("취침등 상태");
light.setState(SLEEPING.getInstance());
}
@Override
public void offButtonPushed(Light light) {
System.out.println("Light Off");
light.setState(OFF.getInstance());
}
}
public class SLEEPING implements State {
private static SLEEPING sleeping = new SLEEPING();
private SLEEPING() {}
public static SLEEPING getInstance() {
return sleeping;
}
@Override
public void onButtonPushed(Light light) {
System.out.println("Light On");
light.setState(ON.getInstance());
}
@Override
public void offButtonPushed(Light light) {
System.out.println("Light Off");
light.setState(OFF.getInstance());
}
}
수정된 Light 클래스
public class Light {
private State state;
public Light() {
this.state = OFF.getInstance();
}
public void setState(State state) {
this.state = state;
}
public void onButtonPushed() {
state.onButtonPushed(this);
}
public void offButtonPushed() {
state.offButtonPushed(this);
}
}
Client 클래스
public class Client {
public static void main(String[] args) {
Light light = new Light();
light.onButtonPushed();
light.onButtonPushed();
light.offButtonPushed();
light.offButtonPushed();
}
}
Light On
취침등 상태
Light Off
noAction
실행결과
State를 Enum으로 변경
public enum State {
ON {
public void onButtonPushed(Light light) {//...}
public void offButtonPushed(Light light) {//...}
},
OFF {
public void onButtonPushed(Light light) {//...}
public void offButtonPushed(Light light) {//...}
},
SLEEPING {
public void onButtonPushed(Light light) {//...}
public void offButtonPushed(Light light) {//...}
};
public abstract void onButtonPushed(Light light);
public abstract void offButtonPushed(Light light);
}
By Young Jun Park (박영준)
State Pattern(상태 패턴)에 대해 소개합니다.