状态模式允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//State(抽象状态类)
//封装与环境类的一个特定状态相关的行为,
//在抽象状态类中声明了各种不同状态对应的方法,
//而在其子类中实现类这些方法,
//由于不同状态下对象的行为可能不同,因此在不同子类中方法的实现可能存在不同,相同的方法可以写在抽象状态类中。
public class DP_State
{
void Main()
{
Context c = new Context();
c.setState(new ConcreteStateA());
c.todo();
//todo check 其他条件导致..
c.setState(new ConcreteStateB());
c.todo();
}
public abstract class State
{
//不同的状态有不同的实现,也可是是不同的状态有不同的方法
public abstract void todo();
}
public class ConcreteStateA : State
{
public override void todo()
{
Debug.Log("审核失败");
}
}
public class ConcreteStateB : State
{
public override void todo()
{
Debug.Log("审核过");
}
}
public class Context
{
private State state;
public void setState(State state)
{
this.state = state;
}
public void todo()
{
this.state.todo();
}
}
}
public class StateManage
{
//state 1
public class JumpingState : HeroineBaseState
{
private Heroine _heroine;
public JumpingState(Heroine heroine)
{
_heroine = heroine;
Debug.Log("------------------------Heroine in JumpingState~!(进入跳跃状态!)");
}
public void Update()
{
}
public void HandleInput()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
Debug.Log("get GetKeyDown.UpArrow! but already in Jumping! return!(已经在跳跃状态中!)");
return;
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
Debug.Log("get KeyCode.DownArrow!");
_heroine.SetHeroineState(new DrivingState(_heroine));
}
}
}
//state 2
public class StandingState : HeroineBaseState
{
private Heroine _heroine;
public StandingState(Heroine heroine)
{
_heroine = heroine;
Debug.Log("------------------------Heroine in StandingState~!(进入站立状态!)");
}
public void Update()
{
}
public void HandleInput()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
Debug.Log("get KeyCode.UpArrow!");
_heroine.SetHeroineState(new JumpingState(_heroine));
}
}
}
//state 3
public class DrivingState : HeroineBaseState
{
private Heroine _heroine;
public DrivingState(Heroine heroine)
{
_heroine = heroine;
Debug.Log("------------------------Heroine in DrivingState~!(进入下斩状态!)");
}
public void Update()
{
}
public void HandleInput()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
Debug.Log("get KeyCode.UpArrow!");
_heroine.SetHeroineState(new StandingState(_heroine));
}
}
}
//-----------------------------------------------------Hero
public interface HeroineBaseState
{
void Update();
void HandleInput();
}
public class Heroine
{
HeroineBaseState _state;
public Heroine()
{
_state = new StandingState(this);
}
public void SetHeroineState(HeroineBaseState newState)
{
_state = newState;
}
public void HandleInput()
{
}
public void Update()
{
_state.HandleInput();
}
}
//-----------------------------------------------------Test
public class Test : MonoBehaviour
{
private Heroine _heroine;
void Start()
{
_heroine = new Heroine();
}
void Update()
{
_heroine.Update();
}
}
}
优点
解决switch-case、if-else带来的难以维护的问题,这个很明显,没什么好说的;
结构清晰,提高了扩展性,不难发现,Context类简洁清晰了,扩展时,几乎不用改变,而且每个状态子类也简洁清晰了,扩展时也只需要极少的改变。
通过单例或享元可使状态在多个上下文间共享。
缺点
随着状态的扩展,状态类数量会增多,这个老生常谈了,几乎所有解决类似问题的设计模式都存在这个缺点;