State Pattern

This commit is contained in:
kim
2021-01-18 16:23:29 +09:00
parent 4af6067c6b
commit da5d5671e3
3 changed files with 72 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
package State;
public class Application {
public static void main(String[] args) {
Light light = new Light();
light.off();
light.off();
light.off();
light.on();
light.on();
light.on();
light.off();
light.on();
light.off();
light.on();
light.off();
light.on();
}
}

View File

@@ -0,0 +1,48 @@
package State;
public class Light {
protected LightState lightState;
private LightState offState = new LightState() {
@Override
public void on() {
System.out.println("Light ON");
lightState = onState;
}
@Override
public void off() {
System.out.println("Nothing");
}
};
private LightState onState = new LightState() {
@Override
public void on() {
System.out.println("Nothing");
}
@Override
public void off() {
System.out.println("Light OFF");
lightState = offState;
}
};
public Light() {
lightState = offState;
}
public void on() {
lightState.on();
}
public void off() {
lightState.off();
}
}
interface LightState {
void on();
void off();
}

View File

@@ -0,0 +1,2 @@
State pattern
- 상태를 객체로 나타내기