状态模式将每种状态对应的行为抽象出来成为单独新的对象,这样状态的变化不再依赖于对象内部的行为。
状态模式——允许一个对象在其内部状态改变时自动改变其行为,对象看起来就像是改变了它的类。
意图:允许对象在内部状态发生改变时改变它的行为,对象看起来好像修改了它的类。
主要解决:对象的行为依赖于它的状态(属性),并且可以根据它的状态改变而改变它的相关行为。
何时使用:代码中包含大量与对象状态有关的条件语句。
如何解决:将各种具体的状态类抽象出来。
关键代码:通常命令模式的接口中只有一个方法。而状态模式的接口中有一个或者多个方法。
而且,状态模式的实现类的方法,一般返回值,或者是改变实例变量的值。也就是说,状态模式一般和对象的状态有关。
实现类的方法有不同的功能,覆盖接口中的方法。状态模式和命令模式一样,也可以用于消除 if...else 等条件选择语句。
优点: 1、封装了转换规则。 2、枚举可能的状态,在枚举状态之前需要确定状态种类。
3、将所有与某个状态有关的行为放到一个类中,并且可以方便地增加新的状态,只需要改变对象状态即可改变对象的行为。
4、允许状态转换逻辑与状态对象合成一体,而不是某一个巨大的条件语句块。
5、可以让多个环境对象共享一个状态对象,从而减少系统中对象的个数。
缺点: 1、状态模式的使用必然会增加系统类和对象的个数。
2、状态模式的结构与实现都较为复杂,如果使用不当将导致程序结构和代码的混乱。
3、状态模式对"开闭原则"的支持并不太好,对于可以切换状态的状态模式,增加新的状态类需要修改那些负责状态转换的源代码,
否则无法切换到新增状态,而且修改某个状态类的行为也需修改对应类的源代码。
使用场景: 1、行为随状态改变而改变的场景。 2、条件、分支语句的代替者。
注意事项:在行为受状态约束的时候使用状态模式,而且状态不超过 5 个。
在以下情况下可以考虑使用状态者模式。
当一个对象状态转换的条件表达式过于复杂时可以使用状态者模式。把状态的判断逻辑转移到表示不同状态的一系列类中,
可以把复杂的判断逻辑简单化。
当一个对象行为取决于它的状态,并且它需要在运行时刻根据状态改变它的行为时,就可以考虑使用状态者模式。
该实例基于WPF实现,直接上代码,下面为三层架构的代码。
一 Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 设计模式练习.Model.状态者模式
{
//2,创建上下文类
public class Context
{
private State state;
public State State { get => state; set => state = value; }
public Context()
{
state = null;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 设计模式练习.Model.状态者模式
{
//创建一系列表示状态的类
public class RunState:State
{
//向外传递状态信息
public string StateInfo { get; set; }
public void doAction(Context context)
{
StateInfo = "现在程序正在运行,状态为: run";
context.State = this;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 设计模式练习.Model.状态者模式
{
//3,开始定义一系列的状态类
public class StartState : State
{
//向外传递状态信息
public string StateInfo { get; set; }
public void doAction(Context context)
{
StateInfo = "开始游戏了,状态为 start";
context.State = this;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 设计模式练习.Model.状态者模式
{
//1,创建状态接口
public interface State
{
public void doAction(Context context);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 设计模式练习.Model.状态者模式
{
//创建一系列表示状态的类
public class StopState : State
{
//向外传递状态信息
public string StateInfo { get; set; }
public void doAction(Context context)
{
StateInfo = "游戏停止了,现在的状态是: stop";
context.State = this;
}
}
}
二 View
<Window x:Class="设计模式练习.View.状态者模式.StateWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:设计模式练习.View.状态者模式"
mc:Ignorable="d"
Title="StateWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding Res}"/>
<TextBlock Grid.Row="1" Text="{Binding Res2}"/>
<TextBlock Grid.Row="2" Text="{Binding Res3}"/>
<TextBlock Grid.Row="3" Text="{Binding CurrentState}"/>
</Grid>
<Button Content="状态切换" Grid.Column="1" Command="{Binding StateCommand}"/>
</Grid>
</Window>
三 ViewModel
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 设计模式练习.Model.状态者模式;
namespace 设计模式练习.ViewModel.状态者模式
{
partial class StateWindow_ViewModel : ObservableObject
{
//实例化状态记录类
Context context = new Context();
[ObservableProperty]
private string res;
[ObservableProperty]
private string res2;
[ObservableProperty]
private string res3;
[ObservableProperty]
private string currentState;
[RelayCommand]
private void State()
{
设计模式练习.Model.状态者模式.State[] states = { new StartState(), new RunState(), new StopState() };
int index = new Random().Next(0, states.Length);
switch (index)
{
case 0:
StartState s1 = states[index] as StartState;
s1.doAction(context);
Res = s1.StateInfo + ":" + DateTime.Now.ToString();
break;
case 1:
RunState s2 = states[index] as RunState;
s2.doAction(context);
Res2 = s2.StateInfo + ":" + DateTime.Now.ToString();
break;
case 2:
StopState s3 = states[index] as StopState;
s3.doAction(context);
Res3 = s3.StateInfo + ":" + DateTime.Now.ToString();
break;
}
//也可以通过context类的state属性判定当前属于哪一个状态
if (context.State is StartState)
{
CurrentState = $"当前状态为:{new StartState().GetType().Name}";
}
if (context.State is RunState)
{
CurrentState = $"当前状态为:{new RunState().GetType().Name}";
}
if (context.State is StopState)
{
CurrentState = $"当前状态为:{new StopState().GetType().Name}";
}
}
}
}