https://refactoring.guru/design-patterns/state
특정 모듈의 상태를 하나의 객에 안에서 통합처리하는 것이 아닌, 각자의 상태 객체에서 처리를 전담하게 한다.
예제
- 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 |