[Design Pattern] 2. μν ν¨ν΄(state)
μΉ΄ν κ³ λ¦¬: Design Pattern
νκ·Έ: C#, Design Pattern
μν ν¨ν΄(state)
μν(state)ν¨ν΄
μνν¨ν΄ (state)
- κ°μ²΄μ μνμ λ°λΌ κ°μ²΄μ νλμ λ³κ²½ ν΄μ£Όλ λμμΈ ν¨ν΄
- μΊλ¦ν° μνκ° λμμμ΄ μ ν κ°μ²΄κ° λ΄λΆ μνλ₯Ό κΈ°λ°μΌλ‘ μμ§μ΄λ μμ€ν
- 쑰건문μΌλ‘ λλλ κ²μ΄ μλλΌ κ°μ²΄μ μνλ₯Ό λ³λμ ν΄λμ€λ‘ μΊ‘μν
- κ²μ μΊλ¦ν°μ μν(λκΈ°, μ΄λ, 곡격)μ λ°λΌ νλμ΄ λ¬λΌμ§κ² ꡬν
state μ½λ보기
// μν μΈν°νμ΄μ€
public interface IState
{
void HandleInput(Player player, string input);
void Update(Player player);
}
// μνμ λ°λ₯Έ ν΄λμ€λ€, λκΈ° μν ν΄λμ€
public class StandingState : IState
{
public void HandleInput(Player player, string input)
{
if (input == "SPACE")
{
player.SetState(new JumpingState());
}
}
public void Update(Player player)
{
// λκΈ° μνμμ ν νλ
}
}
// μ ν μν ν΄λμ€
public class JumpingState : IState
{
public void HandleInput(Player player, string input)
{
// μ ν μνμμμ μ
λ ₯ μ²λ¦¬
}
public void Update(Player player)
{
// μ ν μνμμ ν νλ
}
}
// νλ μ΄μ΄ ν΄λμ€
public class Player
{
private IState currentState;
public Player()
{
currentState = new StandingState();
}
public void SetState(IState newState)
{
currentState = newState;
}
public void HandleInput(string input)
{
currentState.HandleInput(this, input);
}
public void Update()
{
currentState.Update(this);
}
}
// μν ν¨ν΄ μ¬μ© μμ
Player player = new Player();
player.HandleInput("SPACE"); // μ ν μνλ‘ λ³κ²½
player.Update(); // νμ¬ μν(μ ν)μ λ§λ νλ μ€ν
μΆκ°
- μνλ₯Ό λΆλ¬λ΄λ νΈμΆμ -> μΈν°νμ΄μ€ -> μνABC
- Context ν΄λμ€λ κ°μ²΄μ λ΄λΆ μνλ₯Ό λ³κ²½νλλ‘ μμ²νλ ν΄λμ€
- IState μΈν°νμ΄μ€λ ꡬ체μ μΈ μν ν΄λμ€λ‘ μ°κ²°ν μ μλλ‘ μ€μ ν¨
- Context μ€λΈμ νΈκ° Istate μΈν°νμ΄μ€λ₯Ό ꡬνν ν΄λμ€λ€μ νΈμΆν¨
μ₯μ
Β Β 1. μΊ‘μν
Β Β 2. κΈ΄ 쑰건문, λ°©λν ν΄λμ€ κ΅¬ννλ κ²μ λ§μ
λ¨μ
Β Β 1. λ§μ½ μνκ° μ νλλ μ¬μ΄μμ λ°μνλ건 λ°λ‘ ꡬνμ ν΄μΌν¨.
μ½λ
CharaccterStateContext
, ICharacterState
, CharacterController
, CharacterStartState
state μμ
CharaccterStateContext
public class CharaccterStateContext
{
public ICharacterState CurrentState
{
get; set;
}
private readonly CharacterController _characterController;
public CharacterStateContext(CharacterController characterController)
{
_characterController = characterController;
}
public void Transition()
{
CurrentState.Handle(_characterController);
}
public void Transition(ICharacterState state)
{
CurrentState = state;
CurrentState.Handle(_characterController);
}
}
ICharacterState
public interface ICharacterState
{
void Handle(CharacterController controller);
}
using UnityEngine;
CharacterController
public class CharacterController : MonoBehaviour {
private ICharacterState
_startState, _stopState, _turnState;
private CharacterStateContext _characterStateContext;
private void Start() {
_chracterStateContext =
new CharacterStateContext(this);
_startState =
gameObject.AddComponent<CharacterStartState>();
_stopState =
gameObject.AddComponent<CharacterStopState>();
_turnState =
gameObject.AddComponent<CharacterTurnState>();
_characterStateContext.Transition(_stopState);
}
public void StartCharacter() {
_characterStateContext.Transition(_startState);
}
public void StopCharacter() {
_chracterStateContext.Transition(_stopState);
}
}
CharacterStartState
public class CharacterStartState : MonoBehaviour, ICharacterState
{
private CharacterController _characterController;
public void Handle(CharacterController characterController)
{
if (!_characterController)
_characterController = characterController;
_characterController.CurrentSpeed = 10.0f;
}
}
μ‘λ΄, μ 리
- λμμΈ ν¨ν΄μ μ μ΄μ νΉμ λ¬Έμ λ₯Ό ν΄κ²°νκΈ° μν΄ κ³ λ €λ κ².(μ±λ₯, λ©λͺ¨λ¦¬ μ¬μ© κ³ λ €)
[C#] λμμΈ ν¨ν΄ (Design Pattern)
TOP
λκΈλ¨κΈ°κΈ°