State Pattern
This commit is contained in:
22
design-pattern/src/State/Application.java
Normal file
22
design-pattern/src/State/Application.java
Normal 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();
|
||||
}
|
||||
}
|
||||
48
design-pattern/src/State/Light.java
Normal file
48
design-pattern/src/State/Light.java
Normal 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();
|
||||
}
|
||||
2
design-pattern/src/State/State
Normal file
2
design-pattern/src/State/State
Normal file
@@ -0,0 +1,2 @@
|
||||
State pattern
|
||||
- 상태를 객체로 나타내기
|
||||
Reference in New Issue
Block a user