diff --git a/design-pattern/src/State/Application.java b/design-pattern/src/State/Application.java new file mode 100644 index 00000000..f6f8a006 --- /dev/null +++ b/design-pattern/src/State/Application.java @@ -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(); + } +} diff --git a/design-pattern/src/State/Light.java b/design-pattern/src/State/Light.java new file mode 100644 index 00000000..c7234249 --- /dev/null +++ b/design-pattern/src/State/Light.java @@ -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(); +} \ No newline at end of file diff --git a/design-pattern/src/State/State b/design-pattern/src/State/State new file mode 100644 index 00000000..c0f0c396 --- /dev/null +++ b/design-pattern/src/State/State @@ -0,0 +1,2 @@ +State pattern + - 상태를 객체로 나타내기 \ No newline at end of file