Java 객체지향 디자인 패턴 7장 스테이트

필자의 해석이 포함되어 있습니다!
실제로 책에서 의도한 의미와는 다를 수 가 있습니다
주의 부탁드립니다 !!

7장 : State. 상태


책의 내용이 굉장히 읽히지가 않아서

아래 코드로 대체하였다

출처: 얄코 디자인 패턴. https://www.youtube.com/watch?v=lJES5TQTTWE&list=PLpO7kx5DnyIHrxFpyXKkzUg2F_mr-8yY0&index=2

public class YalcoStateMain {
	public static void main(String[] args) {
		final ModeSwitch modeSwitch = new ModeSwitch();
		modeSwitch.onSwitch();
		modeSwitch.onSwitch();
		modeSwitch.onSwitch();
		modeSwitch.onSwitch();
	}

	private static class ModeSwitch {

		private ModeState modeState = new ModeStateLight();

		public void onSwitch() {
			modeState.toggle(this);
		}

		public void setState(ModeState modeState) {
			this.modeState = modeState;
		}
	}

	private static class ModeStateLight implements ModeState {
		@Override
		public void toggle(ModeSwitch modeSwitch) {
			System.out.println("FROM LIGHT TO DARK");
			modeSwitch.setState(new ModeStateDark());
		}

	}

	private static class ModeStateDark implements ModeState {
		@Override
		public void toggle(ModeSwitch modeSwitch) {
			System.out.println("FROM DARK TO LIGHT");
			modeSwitch.setState(new ModeStateLight());
		}
	}

	private interface ModeState {
		void toggle(ModeSwitch modeSwitch);
	}
}

패턴 없는 형광등 켰다 키는 프로그램

image

도저히 읽히지 않아서 코드 위주로 읽어보았다

public class HanbitStateMain {

	private static class Light {
		private static final boolean ON = true;
		private static final boolean OFF = false;
		private static boolean currentState;

		public Light() {
			currentState = false;
		}

		public void off() {
			if (currentState == OFF) {
				System.out.println("이미 Off 되어있는 상태입니다");
				return;
			}
			System.out.println("Light Off ! ");
			currentState = OFF;
		}

		public void on() {
			if (currentState == ON) {
				System.out.println("이미 On 되어있는 상태입니다");
				return;
			}
			System.out.println("Light On ! ");
			currentState = ON;
		}
	}

	private static class Client {
		public static void main(String[] args) {
			Light light = new Light();
			light.off(); // 그대로임
			light.on();
			light.on();
			light.off();
		}
	}
}

실행결과

이미 Off 되어있는 상태입니다
Light On !
이미 On 되어있는 상태입니다
Light Off !

SLEEPING 상태를 추가한다면 ..?

생각보다 쉽지는 않다 왜냐하면 Light 클래스 내의 메서드를 모두 수정해야 하기 때문이다

public class HanbitStateMainWithSleeping {

	private static class Light {
		private static final int OFF = 1;
		private static final int ON = 2;
		private static final int SLEEPING = 3;
		private static int currentState;

		public Light() {
			currentState = OFF;
		}

		public void off() {
			if (currentState == OFF) {
				System.out.println("이미 Off 되어있는 상태입니다");
				return;
			}
			System.out.println("Light Off ! ");
			currentState = OFF;
		}

		public void on() {
			if (currentState == ON) {
				System.out.println("Light Sleep ! ");
				currentState = SLEEPING;
				return;
			}
			System.out.println("Light On ! ");
			currentState = ON;
		}
	}

	private static class Client {
		public static void main(String[] args) {
			Light light = new Light();
			light.off(); // 그대로임
			light.on();
			light.on();
			light.off();
		}
	}
}

스테이트 패턴을 상상하면서 재 작성한 On/Off 스위치

public class HanbitStateMainRefac {

	private enum StateType {

		ON(On.getInstance()),
		OFF(Off.getInstance());

		private final State state;

		public State getState() {
			return state;
		}

		StateType(State state) {
			this.state = state;
		}
	}

	private static abstract class State {



		protected void changeState(State fromState, StateType toStateType) {
			String toStatusName = toStateType.name();

			if (fromState.equals(toStateType.getState())) {
				System.out.println("Already " + toStatusName);
				return;
			}
			System.out.println("Light " + toStatusName);
		}



	}

	private static class On extends State {

		protected static State state= new On();

		public static State getInstance() {
			return state;
		}
	}

	private static class Off extends State {

		protected static State state = new Off();

		public static State getInstance() {
			return state;
		}
	}

	private static class Light {

		private State currentState;

		public Light() {
			currentState = Off.getInstance();
		}

		public void off() {
			currentState.changeState(currentState, StateType.OFF);
			currentState = Off.getInstance();
		}

		public void on() {
			currentState.changeState(currentState, StateType.ON);
			currentState = On.getInstance();
		}
	}

	private static class Client {
		public static void main(String[] args) {
			Light light = new Light();
			light.off();
			light.on();
			light.on();
			light.off();
		}
	}
}




© 2020.12. by 따라쟁이

Powered by philz