https://refactoring.guru/design-patterns/state
State
Use the State pattern when you have an object that behaves differently depending on its current state, the number of states is enormous, and the state-specific code changes frequently. The pattern suggests that you extract all state-specific code into a se
refactoring.guru
특정 모듈의 상태를 하나의 객에 안에서 통합처리하는 것이 아닌, 각자의 상태 객체에서 처리를 전담하게 한다.

예제
- Music Player
- Mini Game state Processor
예제 코드
더보기
public interface IState
{
IState Work();
}
public class LoadingState : IState
{
public IState Work()
{
// workA, workA
// return next step
return new PlayState();
}
}
public class PlayState : IState
{
public IState Work()
{
return new EndState();
}
}
public class EndState : IState
{
public IState Work()
{
return new LoadingState();
}
}
public class MiniStateGame
{
IState _curState, _nextState;
public void SetState(IState state)
{
_curState = state;
_nextState = _curState.Work();
}
}
internal class StateDemo
{
public void Run()
{
MiniStateGame game = new MiniStateGame();
game.SetState(new LoadingState());
}
}
'Work & Programming > System Design' 카테고리의 다른 글
[Behavioral Pattern] - Template Method (0) | 2024.05.16 |
---|---|
[Behavioral Pattern] - Strategy (0) | 2024.05.16 |
[Behavioral Pattern] - Observer (0) | 2024.05.16 |
[Behavioral Pattern] - Memento (0) | 2024.05.16 |
[Behavioral Pattern] - Command (0) | 2024.05.16 |