State pattern
2023. 10. 31. 01:20ㆍ디자인패턴
728x90
State pattern
: 객체의 내부 상태가 바뀜에 따라 객체의 행동을 바꿀 수 있다.
-> 마치 객체의 클래스가 바뀌는 것 같은 결과를 얻을 수 있다.
상태에 따라 동작을 다르게 하기 위해
자신이 직접 상태를 체크하여 호출하지 않고
상태를 클래스로 구현하여 캡슐화한 다음
동작을 각 상태 클래스에 위임하는 방법이다.

- Context를 client라 생각하고, request()를 호출하면 그 작업은 state 객체에게 맡겨진다.
- State 인터페이스에는 모든 상태 클래스에 대한 공통 메소드가 있다.
- ConcreteState에서는 Context로부터 전달된 요청을 처리한다.

public interface State{
public void insertQuarter();
public void ejectQuarter();
public void turnCrank();
public void dispense(); }
public class HasQuarterState implements State{
GumballMachine gumballMachine;
public HasQuarterState(GumballMachine gumballMachine){
this.gumballMachine=gumballMachine;
}
public void insertQuarter() {
System.out.println(“You can’t insert another quarter”);
}
public void ejectQuarter() {
System.out.println(“Quarter returned”);
gumballMachine.setState(gumballMachine.getNoQuarterState());
}
public void turnCrank() {
System.out.println(“You turned...”);
gumballMachine.setState(gumballMachine.getSoldState());
}
public void dispense() {
System.out.println(“No gumball dispensed”);
}
}
public class GumballMachine {
State soldOutState;
State noQuarterState;
State hasQuarterState;
State soldState;
State state = soldOutState;
int count = 0;
public GumballMachine(int numberGumballs) {
soldOutState = new SoldOutState(this);
noQuarterState = new NoQuarterState(this);
hasQuarterState = new HasQuarterState(this);
soldState = new SoldState(this);
this.count = numberGumballs;
if (numberGumballs > 0) {
state = noQuarterState;
}
}
public void insertQuarter() {
state.insertQuarter();
}
public void ejectQuarter() {
state.ejectQuarter();
}
public void turnCrank() {
state.turnCrank();
state.dispense();
}
void setState(State state) {
this.state = state;
}
void releaseBall() {
System.out.println(“A gumball comes rolling out the slot...”);
if (count != 0) {
count = count - 1;
}
// More methods here including getters for each State...
}
-> 각 상태의 행동을 개별 클래스가 책임
-> 각 상태 클래스들은 변경에 닫혀있으면서도, 새로운 상태 클래스를 추가하는 확장에 열려있다.
728x90
'디자인패턴' 카테고리의 다른 글
| Compound Patterns (2) | 2023.11.14 |
|---|---|
| Proxy Pattern (0) | 2023.11.07 |
| composite pattern (0) | 2023.10.30 |
| The iterator pattern (0) | 2023.10.22 |
| The Template Method Pattern (0) | 2023.10.22 |