最近,以前一名同事问了一下工作流的事情,我现在将太概的模式写一下。
临时写的,可能有点乱,如果需要创建一个新的工作流模板的时候请新建一个工作流序列类,职责单一便于维护。
using System;
using System.Collections.Generic;
using System.Text;
namespace WF
{
/// <summary>
/// 工程名称:通过工作流状态进行工作流管理
/// <para>Author:Evan Lee</para>
/// <para>编写时间:2011-05-08</para>
/// <para>博客地址:http://www.cnblogs.com/magic_evan</para>
/// </summary>
class Program
{
/// <summary>
/// 通过工作流状态进行工作流管理
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
int start = 0;
while (true)
{//模板方式的工作流可以编写成模板工厂类的方式
int.TryParse(Console.ReadLine(), out start);
Work work = null;
Dictionary<int, int> stepList = new Dictionary<int, int>();//工作流模板的每一步的的状态<工作流步骤,工作流状态>
switch (start)
{
case 1://输入1的时候启用第1个工作流的模板
work = new Work(1);
stepList.Add(1, 0);
stepList.Add(2, 0);
work.StepList = stepList;
break;
case 2://输入2的时候启用第2个工作流的模板
work = new Work(2);
stepList.Add(1, 1);
stepList.Add(2, 0);
stepList.Add(3, 0);
work.StepList = stepList;
break;
default: return;//其它跳出
}
work.DoWork();
}
}
}
//工作流抽象状态
public abstract class State
{
public abstract void DoWork(Work w);
}
//工作流1
public class wf1 : State
{
public override void DoWork(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(new wf1_1());
w.DoWork();
}
}
//工作流1-1
public class wf1_1 : State
{
public override void DoWork(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(new wfend());
w.DoWork();
}
}
//工作流2
public class wf2 : State
{
public override void DoWork(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(new wf2_1());
w.DoWork();
}
}
//工作流2-1
public class wf2_1 : State
{
public override void DoWork(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(new wf2_2());
w.DoWork();
}
}
//工作流2-2
public class wf2_2 : State
{
public override void DoWork(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(new wfend());
w.DoWork();
}
}
public class wfend : State
{
public override void DoWork(Work w)
{
Console.WriteLine("工作流完成!显示内容");
}
}
//工作
public class Work
{
private State current;
public Work(int wfModel)
{
switch (wfModel)
{
case 1: current = new wf1(); break;
case 2: current = new wf2(); break;
}
}
private Dictionary<int, int> stepList = new Dictionary<int, int>();
/// <summary>
/// 步骤,状态
/// </summary>
public Dictionary<int, int> StepList
{
get { return stepList; }
set { stepList = value; }
}
/// <summary>
/// 是否启用下一个工作流
/// </summary>
private bool isStop = false;
public bool IsStop
{
get { return isStop; }
set { isStop = value; }
}
/// <summary>
/// 当前的步骤
/// </summary>
private int currentStep = 0;
public int CurrentStep
{
get { return (currentStep += 1); }
set { currentStep = value; }
}
/// <summary>
/// 设置当前的工作流实现
/// </summary>
/// <param name="s"></param>
public void SetState(State s)
{
current = s;
}
/// <summary>
/// 操作工作流
/// </summary>
public void DoWork()
{
current.DoWork(this);
}
}
}