C# 开发架构

软件工程就行建筑工程一样,先架构好钢筋,如:接口、抽象类、抽象函数、虚函数、委托等,然后再加水泥,如:实例类、实现类等

抽象类实现

public partial class Window1 : Window
{
    internal static IRtcEngine Rtc;
    public Window1()
    {
        InitializeComponent();
        Rtc = RtcEngine.CreateRtcEngine();
        Rtc.InitEventHandler(new MyEventHandler());
    }

}
//主程序接口
public abstract class IRtcEngine
{
    public abstract void InitEventHandler(IRtcEngineEventHandler engineEventHandler);
}

//主程序实现
public sealed class RtcEngine : IRtcEngine
{
    //接口初始化
    private static readonly RtcEngine[] instance = { null, null };
    public static IRtcEngine CreateRtcEngine()
    {
        return instance[0] ?? (instance[0] = new RtcEngine());
    }

    //事件接口初始化
    public override void InitEventHandler(IRtcEngineEventHandler engineEventHandler)
    {
        RtcEngineEventHandlerNative.EngineEventHandlerArr[0] = engineEventHandler;
    }

}

internal static class RtcEngineEventHandlerNative
{
    internal static IRtcEngineEventHandler[] EngineEventHandlerArr = { null, null };
    internal static void OnEvent(string @event, string data)
    {

    }
}

//事件接口
public abstract class IRtcEngineEventHandler
{
    public virtual void OnWarning(int warn, string msg)
    {
    }

    public virtual void OnError(int err, string msg)
    {
    }
}

//事件实现
internal class MyEventHandler : IRtcEngineEventHandler
{
    public override void OnWarning(int warn, string msg)
    {
        Console.WriteLine("OnOnWarning");
    }

    public override void OnError(int err, string msg)
    {
        Console.WriteLine("OnOnError");
    }
}

  

posted @ 2021-08-06 17:54  microsoft-zhcn  阅读(377)  评论(0编辑  收藏  举报