链式生产环境装配

链式生产环节装配:

    /// <summary>
    /// 数据,它会反复接收工厂里各个环节的加工
    /// </summary>
    public class DataStream
    {
        public string Status { get; set; }
    }

 

    /// <summary>
    /// 节点抽象类,模拟各种机器
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class MechineNode<T> : IMechine<T>
        where T : DataStream, new()
    {
        public IMechine<T> Next { get; set; }

        public T Data { get; set; }
         

        public abstract IMechine<T> Execute();
        

        public IMechine<T> Append(IMechine<T> next) 
        {
            this.Next = next;
            next.Data = this.Data;
            return next;
        }
    }

 

    /// <summary>
    /// 机器节点1
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public sealed class Mechine1<T> : MechineNode<T>
        where T : DataStream, new()
    { 
        public override IMechine<T> Execute()
        {
            this.Data.Status = "updated by mechine 1";
            return this;
        }

    }

 

    /// <summary>
    /// 机器节点2
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public sealed class Mechine2<T> : MechineNode<T>
        where T : DataStream, new()
    {
        public override IMechine<T> Execute()
        {
            this.Data.Status += ",updated by mechine 2";

            return this;
        }
    }

 

下面是应用:

            DataStream data = new DataStream();

            IMechine<DataStream> mnode = new Mechine1<DataStream>();
            mnode.Data = data;


            mnode.Execute()
                .Append(new Mechine2<DataStream>()).Execute()
                //.Append(new Mechine2<DataStream>()).Execute() //<---通过动态链式环节,自由组合各台机器的装配顺序
                ;

 

你可能感兴趣的:(环境)