编辑
2025-09-28
C#
00

目录

摘要
正文
应用场景
示例:电梯控制系统

摘要

状态模式是一种行为型设计模式,它允许一个对象在其内部状态改变时改变其行为。状态模式将对象的行为封装在不同的状态类中,使得对象可以根据其内部状态的变化而改变其行为,而无需在对象中使用大量的条件语句。

正文

应用场景

状态模式适用于以下场景:

  • 当一个对象的行为取决于其内部状态,并且该行为在运行时可能发生变化时。
  • 当一个类有多个状态,且这些状态之间可以相互转换时。
  • 当需要在运行时动态地添加新的状态时。

状态模式可以帮助我们将复杂的条件逻辑分散到不同的状态类中,使得代码更加清晰、可维护和可扩展。

示例:电梯控制系统

假设我们正在开发一个电梯控制系统,该系统可以控制电梯的运行和停止。电梯有三个状态:停止状态、运行状态和故障状态。在不同的状态下,电梯的行为是不同的。

首先,我们创建一个状态接口IElevatorState,定义了电梯状态的方法:

C#
public interface IElevatorState { void Open(); void Close(); void Run(); void Stop(); }

然后,我们创建三个具体状态类,分别实现了状态接口:

C#
public class StoppedState : IElevatorState { public void Open() { Console.WriteLine("The elevator door is open."); } public void Close() { Console.WriteLine("The elevator door is closed."); } public void Run() { Console.WriteLine("The elevator starts running."); } public void Stop() { Console.WriteLine("The elevator is already stopped."); } } public class RunningState : IElevatorState { public void Open() { Console.WriteLine("The elevator is running and cannot open the door."); } public void Close() { Console.WriteLine("The elevator is running and cannot close the door."); } public void Run() { Console.WriteLine("The elevator is already running."); } public void Stop() { Console.WriteLine("The elevator stops running."); } } public class FaultState : IElevatorState { public void Open() { Console.WriteLine("The elevator is in fault state and cannot open the door."); } public void Close() { Console.WriteLine("The elevator is in fault state and cannot close the door."); } public void Run() { Console.WriteLine("The elevator is in fault state and cannot run."); } public void Stop() { Console.WriteLine("The elevator is already stopped."); } }

接下来,我们创建一个电梯类Elevator,该类包含了一个当前状态的成员变量,并提供了对状态的操作方法:

C#
public class Elevator { private IElevatorState _state; public Elevator() { _state = new StoppedState(); } public void SetState(IElevatorState state) { _state = state; } public void Open() { _state.Open(); } public void Close() { _state.Close(); } public void Run() { _state.Run(); } public void Stop() { _state.Stop(); } }

最后,我们可以在客户端代码中使用状态模式来控制电梯的运行:

C#
var elevator = new Elevator(); elevator.Open(); // 输出:The elevator door is open. elevator.Close(); // 输出:The elevator door is closed. elevator.Run(); // 输出:The elevator starts running. elevator.Stop(); // 输出:The elevator stops running. elevator.SetState(new RunningState()); elevator.Open(); // 输出:The elevator is running and cannot open the door. elevator.Close(); // 输出:The elevator is running and cannot close the door. elevator.Run(); // 输出:The elevator is already running. elevator.Stop(); // 输出:The elevator stops running. elevator.SetState(new FaultState()); elevator.Open(); // 输出:The elevator is in fault state and cannot open the door. elevator.Close(); // 输出:The elevator is in fault state and cannot close the door. elevator.Run(); // 输出:The elevator is in fault state and cannot run. elevator.Stop(); // 输出:The elevator is already stopped.

image.png

通过使用状态模式,我们可以根据电梯的状态来改变其行为,而无需在电梯类中使用大量的条件语句。状态模式可以帮助我们更好地组织和管理复杂的状态逻辑。

本文作者:技术老小子

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!