zoukankan      html  css  js  c++  java
  • 关于使用“状态模式”做工作流概要。

    最近,以前一名同事问了一下工作流的事情,我现在将太概的模式写一下。
    临时写的,可能有点乱,如果需要创建一个新的工作流模板的时候请新建一个工作流序列类,职责单一便于维护。
    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);
    }
    }
    }
  • 相关阅读:
    ABAP 销售范围
    C语言深度剖析自测题8解析
    Ubutun13.10下安装fcitx
    各种Web服务器与Nginx的对比
    Hadoop2.2.0在Ubuntu编译失败解决方法
    网站上在给出邮箱地址时不直接使用@符号的原因
    IPython notebook在浏览器中显示不正常的问题及解决方法
    input只读效果
    阿里实习生电面题目:输出给定字符串的全部连续子串
    DNS 放大
  • 原文地址:https://www.cnblogs.com/magic_evan/p/2040713.html
Copyright © 2011-2022 走看看