如何处理三个以内的事件类

How to handle event within three classes

请看我的代码。我有 ThirdClass 来触发一个事件。在第二 Class 中,我处理该事件。但是如何在我的程序中处理该事件 Class。在此 class 中,我没有 ThirdClass 对象来订阅事件。我是否必须在 Second class 中取消另一个事件才能触发 MyPurpose() 方法?

public class Program
{        
    static void Main(string[] ars)
    {
        Program myProgram = new Program();

        SecondClass second = new SecondClass();
        second.LaunchSecondClass();

        //A want to run this method when OnCounted event fired
        //...
        //myProgram.MyPurpose();
        //...
    }


    public void MyPurpose()
    {
        Console.WriteLine("Program Class here!");
        Console.ReadLine();
    }
}

public class SecondClass
{
    public void LaunchSecondClass()
    {
        ThirdClass third = new ThirdClass();
        third.myEvent += this.OnCounted;

        third.count();
    }

    private void OnCounted(object sender, EventArgs e)
    {
        Console.WriteLine("Second Class Here.");
        //Console.ReadLine();
    }
}



public class ThirdClass
{
    public event EventHandler myEvent;

    public void count()
    {
        for (int i = 0; i < 3; i++)
        {
            //Only for testing
            Thread.Sleep(1000);
        }
        OnCounted();
    }

    protected virtual void OnCounted()
    {
        if (myEvent != null)
            myEvent(this, EventArgs.Empty);
    }
}

正如我所说,有很多方法可以做你想做的事。目标是可以实现的,尽管如此,我还是强烈建议大家关注一下@Glubus 的评论,.

以下是其中三种方式:

选项 1 - 在 SecondClass

上公开一个新事件
public class SecondClass
{
    public event EventHandler SecondEvent;

    public void LaunchSecondClass()
    {
        ThirdClass third = new ThirdClass();
        third.myEvent += this.OnCounted;

        third.count();
    }

    private void OnCounted(object sender, EventArgs e)
    {
        Console.WriteLine("Second Class Here.");
        //Console.ReadLine();
        SecondEvent?.Invoke(); // Based on C# 6
    }
}

在你的主程序中:

static void Main(string[] ars)
{
    Program myProgram = new Program();

    SecondClass second = new SecondClass();
    second.SecondEvent += MyPurpose;
    second.LaunchSecondClass();
}

选项 2 - 将 SecondClass 上的 ThirdClass 公开为 属性

public class SecondClass
{
    public ThirdClass ThirdClass { get; }

    public SecondClass()
    {
        ThirdClass = new ThirdClass();
        ThirdClass.myEvent += this.OnCounted;
    }

    public void LaunchSecondClass()
    {
        if(ThirdClass == null)
            ThirdClass = new ThirdClass();

        ThirdClass.count();
    }

    private void OnCounted(object sender, EventArgs e)
    {
        Console.WriteLine("Second Class Here.");
        //Console.ReadLine();
    }
}

在你的主程序中:

static void Main(string[] ars)
{
    Program myProgram = new Program();

    SecondClass second = new SecondClass();
    second.ThirdClass.myEvent += MyPurpose;
    second.LaunchSecondClass();
}

选项 3 - 通过 Action(委托)由 SecondClass 的方法执行

public class SecondClass
{
    public void LaunchSecondClass(Action action)
    {
        ThirdClass third = new ThirdClass();
        third.myEvent += this.OnCounted;

        if(action != null)
            third.myEvent += (o, a) => action.Invoke();

        third.count();
    }

    private void OnCounted(object sender, EventArgs e)
    {
        Console.WriteLine("Second Class Here.");
        //Console.ReadLine();
    }
}

在你的主程序中:

static void Main(string[] ars)
{
    Program myProgram = new Program();

    SecondClass second = new SecondClass();
    second.LaunchSecondClass(MyPurpose);
}

请记住,如果不知道您打算应用它的真实场景,就无法保证选择最佳实践。因此,也许您必须 search up a design pattern 解决您的问题并在规划您的解决方案时遵循 SOLID 原则

这是获得干净高效代码的最佳方式。

这里是关于这个主题的好读物:

希望对你有所帮助,抱歉我的英语不好