做网站的域名,购物网站建设费用,优购物官方网站手机版,广州金山大厦 网站建设状态模式是一种行为设计模式#xff0c;它允许对象在其内部状态发生变化时改变其行为。这种模式的核心思想是将状态封装在独立的对象中#xff0c;而不是将状态逻辑散布在整个程序中。
用途
简化复杂的条件逻辑#xff1a;通过将不同的状态封装在不同的类中#xff0c;可…状态模式是一种行为设计模式它允许对象在其内部状态发生变化时改变其行为。这种模式的核心思想是将状态封装在独立的对象中而不是将状态逻辑散布在整个程序中。
用途
简化复杂的条件逻辑通过将不同的状态封装在不同的类中可以避免大量的条件语句使代码更清晰、更易于维护。提高可扩展性添加新的状态或修改现有状态的行为时只需修改或新增相应的状态类而不需要修改现有的代码。提高代码的复用性状态对象可以在多个上下文对象之间共享提高代码的复用性。 示例代码及类图
示例1交通信号灯
public interface ILightState
{void Handle(TrafficLight context);
}public class RedLightState : ILightState
{public void Handle(TrafficLight context){Console.WriteLine(Red Light);context.SetState(new GreenLightState());}
}public class GreenLightState : ILightState
{public void Handle(TrafficLight context){Console.WriteLine(Green Light);context.SetState(new YellowLightState());}
}public class YellowLightState : ILightState
{public void Handle(TrafficLight context){Console.WriteLine(Yellow Light);context.SetState(new RedLightState());}
}public class TrafficLight
{private ILightState _state;public TrafficLight(ILightState initialState){_state initialState;}public void ChangeLight(){_state.Handle(this);}public void SetState(ILightState state){_state state;}
}类图 #mermaid-svg-3qXjmv4ZOw4aPCc6 {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-3qXjmv4ZOw4aPCc6 .error-icon{fill:#552222;}#mermaid-svg-3qXjmv4ZOw4aPCc6 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-3qXjmv4ZOw4aPCc6 .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-3qXjmv4ZOw4aPCc6 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-3qXjmv4ZOw4aPCc6 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-3qXjmv4ZOw4aPCc6 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-3qXjmv4ZOw4aPCc6 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-3qXjmv4ZOw4aPCc6 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-3qXjmv4ZOw4aPCc6 .marker.cross{stroke:#333333;}#mermaid-svg-3qXjmv4ZOw4aPCc6 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-3qXjmv4ZOw4aPCc6 g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-3qXjmv4ZOw4aPCc6 g.classGroup text .title{font-weight:bolder;}#mermaid-svg-3qXjmv4ZOw4aPCc6 .nodeLabel,#mermaid-svg-3qXjmv4ZOw4aPCc6 .edgeLabel{color:#131300;}#mermaid-svg-3qXjmv4ZOw4aPCc6 .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-3qXjmv4ZOw4aPCc6 .label text{fill:#131300;}#mermaid-svg-3qXjmv4ZOw4aPCc6 .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-3qXjmv4ZOw4aPCc6 .classTitle{font-weight:bolder;}#mermaid-svg-3qXjmv4ZOw4aPCc6 .node rect,#mermaid-svg-3qXjmv4ZOw4aPCc6 .node circle,#mermaid-svg-3qXjmv4ZOw4aPCc6 .node ellipse,#mermaid-svg-3qXjmv4ZOw4aPCc6 .node polygon,#mermaid-svg-3qXjmv4ZOw4aPCc6 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-3qXjmv4ZOw4aPCc6 .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-3qXjmv4ZOw4aPCc6 g.clickable{cursor:pointer;}#mermaid-svg-3qXjmv4ZOw4aPCc6 g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-3qXjmv4ZOw4aPCc6 g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-3qXjmv4ZOw4aPCc6 .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-3qXjmv4ZOw4aPCc6 .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-3qXjmv4ZOw4aPCc6 .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-3qXjmv4ZOw4aPCc6 .dashed-line{stroke-dasharray:3;}#mermaid-svg-3qXjmv4ZOw4aPCc6 #compositionStart,#mermaid-svg-3qXjmv4ZOw4aPCc6 .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-3qXjmv4ZOw4aPCc6 #compositionEnd,#mermaid-svg-3qXjmv4ZOw4aPCc6 .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-3qXjmv4ZOw4aPCc6 #dependencyStart,#mermaid-svg-3qXjmv4ZOw4aPCc6 .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-3qXjmv4ZOw4aPCc6 #dependencyStart,#mermaid-svg-3qXjmv4ZOw4aPCc6 .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-3qXjmv4ZOw4aPCc6 #extensionStart,#mermaid-svg-3qXjmv4ZOw4aPCc6 .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-3qXjmv4ZOw4aPCc6 #extensionEnd,#mermaid-svg-3qXjmv4ZOw4aPCc6 .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-3qXjmv4ZOw4aPCc6 #aggregationStart,#mermaid-svg-3qXjmv4ZOw4aPCc6 .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-3qXjmv4ZOw4aPCc6 #aggregationEnd,#mermaid-svg-3qXjmv4ZOw4aPCc6 .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-3qXjmv4ZOw4aPCc6 .edgeTerminals{font-size:11px;}#mermaid-svg-3qXjmv4ZOw4aPCc6 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} ILightState void Handle(TrafficLight context) RedLightState void Handle(TrafficLight context) GreenLightState void Handle(TrafficLight context) YellowLightState void Handle(TrafficLight context) TrafficLight -ILightState _state TrafficLight(ILightState initialState) void ChangeLight() void SetState(ILightState state) 示例2订单状态管理
public interface IOrderState
{void Handle(Order context);
}public class PendingState : IOrderState
{public void Handle(Order context){Console.WriteLine(Order is pending);context.SetState(new ProcessingState());}
}public class ProcessingState : IOrderState
{public void Handle(Order context){Console.WriteLine(Order is being processed);context.SetState(new ShippedState());}
}public class ShippedState : IOrderState
{public void Handle(Order context){Console.WriteLine(Order has been shipped);context.SetState(new DeliveredState());}
}public class DeliveredState : IOrderState
{public void Handle(Order context){Console.WriteLine(Order has been delivered);}
}public class Order
{private IOrderState _state;public Order(IOrderState initialState){_state initialState;}public void Process(){_state.Handle(this);}public void SetState(IOrderState state){_state state;}
}类图 #mermaid-svg-ZZWbicKD4aWCXxo4 {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-ZZWbicKD4aWCXxo4 .error-icon{fill:#552222;}#mermaid-svg-ZZWbicKD4aWCXxo4 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ZZWbicKD4aWCXxo4 .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-ZZWbicKD4aWCXxo4 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ZZWbicKD4aWCXxo4 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ZZWbicKD4aWCXxo4 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ZZWbicKD4aWCXxo4 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ZZWbicKD4aWCXxo4 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ZZWbicKD4aWCXxo4 .marker.cross{stroke:#333333;}#mermaid-svg-ZZWbicKD4aWCXxo4 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ZZWbicKD4aWCXxo4 g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-ZZWbicKD4aWCXxo4 g.classGroup text .title{font-weight:bolder;}#mermaid-svg-ZZWbicKD4aWCXxo4 .nodeLabel,#mermaid-svg-ZZWbicKD4aWCXxo4 .edgeLabel{color:#131300;}#mermaid-svg-ZZWbicKD4aWCXxo4 .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-ZZWbicKD4aWCXxo4 .label text{fill:#131300;}#mermaid-svg-ZZWbicKD4aWCXxo4 .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-ZZWbicKD4aWCXxo4 .classTitle{font-weight:bolder;}#mermaid-svg-ZZWbicKD4aWCXxo4 .node rect,#mermaid-svg-ZZWbicKD4aWCXxo4 .node circle,#mermaid-svg-ZZWbicKD4aWCXxo4 .node ellipse,#mermaid-svg-ZZWbicKD4aWCXxo4 .node polygon,#mermaid-svg-ZZWbicKD4aWCXxo4 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ZZWbicKD4aWCXxo4 .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-ZZWbicKD4aWCXxo4 g.clickable{cursor:pointer;}#mermaid-svg-ZZWbicKD4aWCXxo4 g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-ZZWbicKD4aWCXxo4 g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-ZZWbicKD4aWCXxo4 .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-ZZWbicKD4aWCXxo4 .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-ZZWbicKD4aWCXxo4 .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-ZZWbicKD4aWCXxo4 .dashed-line{stroke-dasharray:3;}#mermaid-svg-ZZWbicKD4aWCXxo4 #compositionStart,#mermaid-svg-ZZWbicKD4aWCXxo4 .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZZWbicKD4aWCXxo4 #compositionEnd,#mermaid-svg-ZZWbicKD4aWCXxo4 .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZZWbicKD4aWCXxo4 #dependencyStart,#mermaid-svg-ZZWbicKD4aWCXxo4 .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZZWbicKD4aWCXxo4 #dependencyStart,#mermaid-svg-ZZWbicKD4aWCXxo4 .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZZWbicKD4aWCXxo4 #extensionStart,#mermaid-svg-ZZWbicKD4aWCXxo4 .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZZWbicKD4aWCXxo4 #extensionEnd,#mermaid-svg-ZZWbicKD4aWCXxo4 .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZZWbicKD4aWCXxo4 #aggregationStart,#mermaid-svg-ZZWbicKD4aWCXxo4 .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZZWbicKD4aWCXxo4 #aggregationEnd,#mermaid-svg-ZZWbicKD4aWCXxo4 .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZZWbicKD4aWCXxo4 .edgeTerminals{font-size:11px;}#mermaid-svg-ZZWbicKD4aWCXxo4 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} IOrderState void Handle(Order context) PendingState void Handle(Order context) ProcessingState void Handle(Order context) ShippedState void Handle(Order context) DeliveredState void Handle(Order context) Order -IOrderState _state Order(IOrderState initialState) void Process() void SetState(IOrderState state) 示例3游戏角色状态
public interface ICharacterState
{void Handle(Character context);
}public class IdleState : ICharacterState
{public void Handle(Character context){Console.WriteLine(Character is idle);context.SetState(new WalkingState());}
}public class WalkingState : ICharacterState
{public void Handle(Character context){Console.WriteLine(Character is walking);context.SetState(new RunningState());}
}public class RunningState : ICharacterState
{public void Handle(Character context){Console.WriteLine(Character is running);context.SetState(new IdleState());}
}public class Character
{private ICharacterState _state;public Character(ICharacterState initialState){_state initialState;}public void Move(){_state.Handle(this);}public void SetState(ICharacterState state){_state state;}
}类图 #mermaid-svg-OFNpI5NS7NZyFIyO {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-OFNpI5NS7NZyFIyO .error-icon{fill:#552222;}#mermaid-svg-OFNpI5NS7NZyFIyO .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-OFNpI5NS7NZyFIyO .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-OFNpI5NS7NZyFIyO .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-OFNpI5NS7NZyFIyO .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-OFNpI5NS7NZyFIyO .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-OFNpI5NS7NZyFIyO .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-OFNpI5NS7NZyFIyO .marker{fill:#333333;stroke:#333333;}#mermaid-svg-OFNpI5NS7NZyFIyO .marker.cross{stroke:#333333;}#mermaid-svg-OFNpI5NS7NZyFIyO svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-OFNpI5NS7NZyFIyO g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-OFNpI5NS7NZyFIyO g.classGroup text .title{font-weight:bolder;}#mermaid-svg-OFNpI5NS7NZyFIyO .nodeLabel,#mermaid-svg-OFNpI5NS7NZyFIyO .edgeLabel{color:#131300;}#mermaid-svg-OFNpI5NS7NZyFIyO .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-OFNpI5NS7NZyFIyO .label text{fill:#131300;}#mermaid-svg-OFNpI5NS7NZyFIyO .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-OFNpI5NS7NZyFIyO .classTitle{font-weight:bolder;}#mermaid-svg-OFNpI5NS7NZyFIyO .node rect,#mermaid-svg-OFNpI5NS7NZyFIyO .node circle,#mermaid-svg-OFNpI5NS7NZyFIyO .node ellipse,#mermaid-svg-OFNpI5NS7NZyFIyO .node polygon,#mermaid-svg-OFNpI5NS7NZyFIyO .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-OFNpI5NS7NZyFIyO .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-OFNpI5NS7NZyFIyO g.clickable{cursor:pointer;}#mermaid-svg-OFNpI5NS7NZyFIyO g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-OFNpI5NS7NZyFIyO g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-OFNpI5NS7NZyFIyO .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-OFNpI5NS7NZyFIyO .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-OFNpI5NS7NZyFIyO .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-OFNpI5NS7NZyFIyO .dashed-line{stroke-dasharray:3;}#mermaid-svg-OFNpI5NS7NZyFIyO #compositionStart,#mermaid-svg-OFNpI5NS7NZyFIyO .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-OFNpI5NS7NZyFIyO #compositionEnd,#mermaid-svg-OFNpI5NS7NZyFIyO .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-OFNpI5NS7NZyFIyO #dependencyStart,#mermaid-svg-OFNpI5NS7NZyFIyO .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-OFNpI5NS7NZyFIyO #dependencyStart,#mermaid-svg-OFNpI5NS7NZyFIyO .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-OFNpI5NS7NZyFIyO #extensionStart,#mermaid-svg-OFNpI5NS7NZyFIyO .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-OFNpI5NS7NZyFIyO #extensionEnd,#mermaid-svg-OFNpI5NS7NZyFIyO .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-OFNpI5NS7NZyFIyO #aggregationStart,#mermaid-svg-OFNpI5NS7NZyFIyO .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-OFNpI5NS7NZyFIyO #aggregationEnd,#mermaid-svg-OFNpI5NS7NZyFIyO .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-OFNpI5NS7NZyFIyO .edgeTerminals{font-size:11px;}#mermaid-svg-OFNpI5NS7NZyFIyO :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} ICharacterState void Handle(Character context) IdleState void Handle(Character context) WalkingState void Handle(Character context) RunningState void Handle(Character context) Character -ICharacterState _state Character(ICharacterState initialState) void Move() void SetState(ICharacterState state) 代码解析
状态接口 (IState)定义了一个 Handle 方法所有具体状态类都需要实现这个方法。具体状态 (StateA 和 StateB)实现了 IState 接口具体实现了 Handle 方法。在处理完当前状态后可以改变上下文的当前状态。上下文类 (Context)维护一个对当前状态的引用并提供一个方法 Request 来调用当前状态的 Handle 方法。上下文还提供了一个属性 State 来改变当前状态并在状态改变时输出相关信息。
优点
简化条件逻辑通过将状态逻辑封装在不同的类中减少了大量的条件语句使代码更清晰、更易于维护。提高可扩展性添加新的状态或修改现有状态的行为时只需修改或新增相应的状态类而不需要修改现有的代码。提高代码的复用性状态对象可以在多个上下文对象之间共享提高代码的复用性。
缺点
增加类的数量每种状态都需要一个类这可能会导致类的数量增多增加系统的复杂性。状态转换逻辑分散状态转换的逻辑分布在不同的状态类中有时可能难以追踪和理解。过度设计对于简单的状态转换使用状态模式可能会显得过于复杂增加不必要的开销。
总结
状态模式适用于需要管理多种状态和状态转换的场景特别是在状态逻辑较为复杂的情况下。通过将状态逻辑封装在独立的类中可以使代码更加模块化、易于维护和扩展。然而在简单的情况下使用状态模式可能会增加不必要的复杂性。