这些类之间的通信实现

时间:2018-11-23 19:26:47

标签: c# object events communication

我正在寻找实现以下目标的最佳方法的提示:

我有一个类,它每秒接收一次输入,并在接收到该输入时触发一个事件(在输入中发送信息)。

此事件已由其他几个类订阅。
每个类都有一个:

  • “排名”(一个$Header = ("date","infohostname","Version","SMTP","Value_1","Value_2","Value_3","Value_4","Value_5","Value_6","Value_7","Value_8","Value_9") $Import_IP = Import-Csv -Path "$destination_RAW_NAS\audit_nas_8_$((Get-Date).ToString('yyyy-MM-dd')).txt" -Header $Header | Where-Object {![string]::IsNullOrWhiteSpace($_.infohostname)} 字段),
  • 布尔方法int(其中 根据事件的输入检查条件)和 方法称为run。

触发事件时,所有类都将调用Condition方法。如果方法返回true,那么我希望只对具有最高排名的类调用run方法。

我实现它的方式是在事件触发时,所有类将排名和run方法添加到列表中(如果不满足条件,则添加0排名)。
当列表达到设定的数量时,它将选择最高的排名并调用相关的方法。

代码如下:

Condition

1 个答案:

答案 0 :(得分:2)

我认为订阅模型不适用于此问题。您的“ MainClass”需要更多地充当协调器(选择可运行的最高排名)来执行而不是盲目生成事件。

因此,让我们从“跑步者”类的界面开始:

  interface RankedRunner
  {
      // The rank of the runner (this must be immutable).
      int Rank { get; }

      // Whether we can run this runner.
      bool Runnable(int input);

      // Run the runner.
      void Run();
  }

现在让我们添加一些实现:

   class Runner1 : RankedRunner
    {
        public int Rank => 3;

        public void Run()
        {
        }

        public bool Runnable(int input)
        {
            return input > 20;
        }
    }

    class Runner2 : RankedRunner
    {
        public int Rank => 4;

        public void Run()
        {
        }

        public bool Runnable(int input)
        {
            return input > 10;
        }
    }

最后,让我们根据一些输入来安排跑步者:

    class Orchestration
    {
        private SortedList<RankedRunner, RankedRunner> runners;

        Orchestration()
        {
            // We need to sort our runners based on their rank. If two runners
            // have the same rank then use the object comparator.
            // Note that x and y get swapped so the largest rank will be ordered first.
            this.runners = new SortedList<RankedRunner, RankedRunner>(
                Comparer<RankedRunner>.Create(
                    (x, y) =>
                    {
                        return x.Rank == y.Rank
                            ? Comparer<RankedRunner>.Default.Compare(y, x)
                            : Comparer<int>.Default.Compare(y.Rank, x.Rank);
                    }));
        }

        Orchestration addRankedRunner(RankedRunner runner)
        {
            this.runners.Add(runner, runner);
            return this;
        }

        void Input(int input)
        {  
            // Find the highest ranked runner that is runnable.
            foreach(RankedRunner runner in runners.Values)
            {
                if(runner.Runnable(input))
                {
                    runner.Run();
                    break;
                }
            }
        }
    }

示例用法:

      var orchestration = new Orchestration();
        orchestration.addRankedRunner(new Runner1());
        orchestration.addRankedRunner(new Runner2());

        orchestration.Input(5);
        orchestration.Input(10);
        orchestration.Input(50);