interface IState
{
string Name { get; set; }
//后件处理
IList<IState> Nexts { get; set; }
Func<IState /*this*/, IState /*next*/> Selector { get; set; }
}
class State : IState
{
public string Name { get; set; } = "State";
IList<IState> IState.Nexts { get; set; } = new List<IState>();
public Func<IState, IState> Selector { get; set; }
}
delegate bool HandleType<T>(IState current, IState previous,ref T value);
interface IContext<T> : IEnumerator<T>, IEnumerable<T>
{
//data
T Value { get; set; }
//前件处理
IDictionary<Tuple<IState/*this*/, IState/*previous*/>, HandleType<T>> Handles { get; set; }
IState CurrentState { get; set; }
bool transition(IState next);
}
class Context<T> : IContext<T>
{
T data;
T IContext<T>.Value { get=>data ; set=>data = value; }
IDictionary<Tuple<IState, IState>, HandleType<T>> IContext<T>.Handles { get; set; }
= new Dictionary<Tuple<IState, IState>, HandleType<T>>();
public IState CurrentState { get; set;}
T IEnumerator<T>.Current => (this as IContext<T>).Value ;
object IEnumerator.Current => (this as IContext<T>).Value;
bool IContext<T>.transition(IState next)
{
IContext<T> context= this as IContext<T>;
if (context.CurrentState == null || context.CurrentState.Nexts.Contains(next))
{
//前件处理
var key = Tuple.Create(next, context.CurrentState);
if (context.Handles.ContainsKey(key) && context.Handles[key] !=null)
if (!context.Handles[key](next, context.CurrentState,ref this.data))
return false;
context.CurrentState = next;
return true;
}
return false;
}
bool IEnumerator.MoveNext()
{
//后件处理
IContext<T> context = this as IContext<T>;
IState current = context.CurrentState;
if (current == null)
throw new Exception("必须设置初始状态");
if (context.CurrentState.Selector != null)
{
IState next= context.CurrentState.Selector(context.CurrentState);
return context.transition(next);
}
return false;
}
void IEnumerator.Reset()
{
throw new NotImplementedException();
}
#region IDisposable Support
private bool disposedValue = false; // 要检测冗余调用
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// TODO: 释放托管状态(托管对象)。
}
// TODO: 释放未托管的资源(未托管的对象)并在以下内容中替代终结器。
// TODO: 将大型字段设置为 null。
disposedValue = true;
}
}
// TODO: 仅当以上 Dispose(bool disposing) 拥有用于释放未托管资源的代码时才替代终结器。
// ~Context() {
// // 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。
// Dispose(false);
// }
// 添加此代码以正确实现可处置模式。
void IDisposable.Dispose()
{
// 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。
Dispose(true);
// TODO: 如果在以上内容中替代了终结器,则取消注释以下行。
// GC.SuppressFinalize(this);
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return this;
}
IEnumerator IEnumerable.GetEnumerator()
{
return this;
}
#endregion
}
bool IContext<T>.transition(IState next)
{
IContext<T> context= this as IContext<T>;
if (context.CurrentState == null || context.CurrentState.Nexts.Contains(next))
{
//前件处理
var key = Tuple.Create(next, context.CurrentState);
if (context.Handles.ContainsKey(key) && context.Handles[key] !=null)
if (!context.Handles[key](next, context.CurrentState,ref this.data))
return false;
context.CurrentState = next;
return true;
}
return false;
}
bool IEnumerator.MoveNext()
{
//后件处理
IContext<T> context = this as IContext<T>;
IState current = context.CurrentState;
if (current == null)
throw new Exception("必须设置初始状态");
if (context.CurrentState.Selector != null)
{
IState next= context.CurrentState.Selector(context.CurrentState);
return context.transition(next);
}
return false;
}
//测试代码
//---------创建部分---------
string mess = "";//3
IState s3 = new State() { Name = "s3" };
//2
IState s2 = new State() { Name = "s2" };
//1
IState s1 = new State() { Name = "s1" };
//---------组合起来---------
s1.Nexts = new List<IState> { s2, s3 };
s2.Nexts = new List<IState> { s1, s3 };
s3.Nexts = new List<IState> { }; //注意end写法
//---------上下文---------
//transition
IContext<int> cont = new Context<int> { CurrentState=s1};//begin
cont.Value = 0;
//---------状态处理器---------
HandleType<int> funcLaji = (IState current, IState previous, ref int v) => { mess += $"{current.Name}:垃圾{previous.Name}\n"; v++; return true; };
//1
cont.Handles.Add(Tuple.Create(s1 , default(IState)), funcLaji);
cont.Handles.Add(Tuple.Create(s1, s2), funcLaji);
//2
cont.Handles.Add(Tuple.Create(s2, s1), funcLaji);
//3
cont.Handles.Add(Tuple.Create(s3, s1), funcLaji);
cont.Handles.Add(Tuple.Create(s3, s2), funcLaji);
//---------状态选择器---------
var rval = new Random();
Func<int,int> round = x => rval.Next(x);
s1.Selector = st => round(2)==0? s2:s3;
s2.Selector = st => round(2)==0? s1:s3;
//选择器跳转
mess += "选择器跳转:\n------------------------\n";
foreach (var stor in cont)
mess+=$"状态转变次数:{stor}\n";
//直接控制跳转
mess += "\n直接控制状态跳转:\n------------------------\n";
cont.transition(s1);
cont.transition(s2);
cont.transition(s3);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有