usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceWF
{//工程名称:通过工作流状态进行工作流管理///Author:Evan Lee///编写时间:2011-05-08///博客地址:http://www.cnblogs.com/magic_evan///classProgram
{//通过工作流状态进行工作流管理//staticvoidMain(string[] args)
{intstart=0;while(true)
{//模板方式的工作流可以编写成模板工厂类的方式int.TryParse(Console.ReadLine(),outstart);
Work work=null;
DictionarystepList=newDictionary();//工作流模板的每一步的的状态switch(start)
{case1://输入1的时候启用第1个工作流的模板work=newWork(1);
stepList.Add(1,0);
stepList.Add(2,0);
work.StepList=stepList;break;case2://输入2的时候启用第2个工作流的模板work=newWork(2);
stepList.Add(1,1);
stepList.Add(2,0);
stepList.Add(3,0);
work.StepList=stepList;break;default:return;//其它跳出}
work.DoWork();
}
}
}//工作流抽象状态publicabstractclassState
{publicabstractvoidDoWork(Work w);
}//工作流1publicclasswf1 : State
{publicoverridevoidDoWork(Work w)
{if(w.IsStop)return;if(w.StepList[w.CurrentStep]>0)
{
Console.WriteLine("{0}步工作流完成!显示内容", w.CurrentStep=w.CurrentStep-1);
}else{
Console.WriteLine("{0}步工作流未完成!显示输入内容", w.CurrentStep=w.CurrentStep-1);
w.IsStop=true;
}
w.SetState(newwf1_1());
w.DoWork();
}
}//工作流1-1publicclasswf1_1 : State
{publicoverridevoidDoWork(Work w)
{if(w.IsStop)return;if(w.StepList[w.CurrentStep]>0)
{
Console.WriteLine("{0}步工作流完成!显示内容", w.CurrentStep=w.CurrentStep-1);
}else{
Console.WriteLine("{0}步工作流未完成!显示输入内容", w.CurrentStep=w.CurrentStep-1);
w.IsStop=true;
}
w.SetState(newwfend());
w.DoWork();
}
}//工作流2publicclasswf2 : State
{publicoverridevoidDoWork(Work w)
{if(w.IsStop)return;if(w.StepList[w.CurrentStep]>0)
{
Console.WriteLine("{0}步工作流完成!显示内容", w.CurrentStep=w.CurrentStep-1);
}else{
Console.WriteLine("{0}步工作流未完成!显示输入内容", w.CurrentStep=w.CurrentStep-1);
w.IsStop=true;
}
w.SetState(newwf2_1());
w.DoWork();
}
}//工作流2-1publicclasswf2_1 : State
{publicoverridevoidDoWork(Work w)
{if(w.IsStop)return;if(w.StepList[w.CurrentStep]>0)
{
Console.WriteLine("{0}步工作流完成!显示内容", w.CurrentStep=w.CurrentStep-1);
}else{
Console.WriteLine("{0}步工作流未完成!显示输入内容", w.CurrentStep=w.CurrentStep-1);
w.IsStop=true;
}
w.SetState(newwf2_2());
w.DoWork();
}
}//工作流2-2publicclasswf2_2 : State
{publicoverridevoidDoWork(Work w)
{if(w.IsStop)return;if(w.StepList[w.CurrentStep]>0)
{
Console.WriteLine("{0}步工作流完成!显示内容", w.CurrentStep=w.CurrentStep-1);
}else{
Console.WriteLine("{0}步工作流未完成!显示输入内容", w.CurrentStep=w.CurrentStep-1);
w.IsStop=true;
}
w.SetState(newwfend());
w.DoWork();
}
}publicclasswfend : State
{publicoverridevoidDoWork(Work w)
{
Console.WriteLine("工作流完成!显示内容");
}
}//工作publicclassWork
{privateState current;publicWork(intwfModel)
{switch(wfModel)
{case1: current=newwf1();break;case2: current=newwf2();break;
}
}privateDictionarystepList=newDictionary();//步骤,状态///publicDictionaryStepList
{get{returnstepList; }set{ stepList=value; }
}//是否启用下一个工作流///privateboolisStop=false;publicboolIsStop
{get{returnisStop; }set{ isStop=value; }
}//当前的步骤///privateintcurrentStep=0;publicintCurrentStep
{get{return(currentStep+=1); }set{ currentStep=value; }
}//设置当前的工作流实现//publicvoidSetState(State s)
{
current=s;
}//操作工作流///publicvoidDoWork()
{
current.DoWork(this);
}
}
}