Skip to content

State

Context: Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

public interface IState
{
void Handle(Context context);
}
public class ConcreteStateA : IState
{
public void Handle(Context context) => context.State = new ConcreteStateB();
}
public class Context
{
public IState State { get; set; }
public void Request() => State.Handle(this);
}

Document workflow: A document can be in Draft, Review, Approved, or Published states. Each state defines allowed operations (edit, approve, reject) and transitions.

Example: In .NET, state machine in System.Threading.Tasks (async state machine) – though not classic pattern. Also, StateMachine in Unity and Workflow Foundation.