简体   繁体   English

如何在C#中实现此模式

[英]How to implement this pattern in C#

Class Client{
  main(){
    MyRequest m = new MyRequest();
    m.function();
  }
  onSucess(string s){
    Debug.log("i get data from network:"+s);
  }
}
Class Network{
   sendMyrequest(MyRequest r){
     Thread thread = new Thread(() => sendMyrequestTask(r));
     thread.start(); 
   }
   private void sendMyrequestTask(MyRequest r){
     if(...){
        //call delegate function onSucess(string s)
     }
   }
}
Class MyRequest{
private Network network;
  function(){
    //do something
    network.sendMyrequest(MyRequest r);

  }

}

in this case, callback function onSucess(string s) should be a delegate, or a interface, how and where should I implement it? 在这种情况下,回调函数onSucess(string s)应该是一个委托或接口,我应该在哪里以及如何实现它? Any suggestion would be appreciate. 任何建议将不胜感激。 Thanks in advance!! 提前致谢!!

Edit: this problem is like: A call B,B call C, when C's job is done, C should call A. How to implement this? 编辑:这个问题就像:A呼叫B,B呼叫C,当C的工作完成时,C应该呼叫A。如何实现呢?

Thanks all guys. 谢谢大家。 I implement in this way. 我以这种方式实施。

public interface CallbackFunction{
   public onSucess(string s);
}
Class Client:CallbackFunction{
  main(){
    MyRequest m = new MyRequest();
    m.function(this);
  }
  onSucess(string s){
    Debug.log("i get data from network:"+s);
  }
}
Class Network{
   sendMyrequest(MyRequest r,CallbackFunction c){
     Thread thread = new Thread(() => sendMyrequestTask(r,c));
     thread.start(); 
   }
   private void sendMyrequestTask(MyRequest r,CallbackFunction c){
     if(...){
        //call delegate function onSucess(string s)
        c.onSucess("bla bla bla");
     }
   }
}
Class MyRequest{
private Network network;
  function(CallbackFunction c){
    //do something
    network.sendMyrequest(this,c);

  }

}

You can use async and `await. 您可以使用async和`await。

For more info : 有关更多信息:

Many methods do not immediately return. 许多方法不会立即返回。 A method may need to query an external source. 一种方法可能需要查询外部源。 This takes time. 这需要时间。 With async and await, we formalize and clarify how asynchronous, non-blocking methods begin and end. 通过异步和等待,我们形式化并阐明了异步,非阻塞方法的开始和结束方式。

http://msdn.microsoft.com/en-us/library/hh191443.aspx http://msdn.microsoft.com/en-us/library/hh191443.aspx

http://msdn.microsoft.com/en-us/library/hh156513.aspx http://msdn.microsoft.com/en-us/library/hh156513.aspx

It looks like you want to implement something similar to WebClient.DownloadStringCompleted event. 看来您想实现类似于WebClient.DownloadStringCompleted事件的功能。 Which uses following delegate type: 使用以下委托类型:

public delegate void DownloadStringCompletedEventHandler(
    Object sender
    DownloadStringCompletedEventArgs e)`

with following usage pattern: 具有以下使用模式:

WebClient client = new WebClient ();
client.DownloadStringCompleted += DownloadStringCallback2;
client.DownloadStringAsync (new Uri(address));

Note: if you don't expect multiple listeners using async / await will lead to much easier to understand code. 注意:如果您不希望使用async / await多个侦听器将使代码更容易理解。

By using the Action<> delegate as a constructor argument in MyRequest we can achieve something like this. 通过在MyRequest中将Action<>委托用作构造函数参数,我们可以实现类似的目的。

You can replace the Action<> type with any other delegate you might want to use. 您可以将Action<>类型替换为您可能要使用的任何其他委托。 Action<> or Func<> should cover just about anything. Action<>Func<>应该涵盖几乎所有内容。

By the way, your code, and hence my code below is not the observer pattern. 顺便说一下,您的代码以及下面的我的代码都不是观察者模式。

public class Client
{
    static void Main(string[] args)
    {
        var client = new MyRequest(OnSuccess);
        client.Function();
        //Output:
        //I'm in the callback
        //Foo.Bar()
        Console.ReadKey();
    }

    static void OnSuccess(string result)
    {
        Console.WriteLine("I'm in the callback");
        Console.WriteLine(result);
    }

}

public class Network
{
    public void SendMyRequest(MyRequest request)
    {
        var result = "Foo.Bar()";

        if (!String.IsNullOrEmpty(result))
        {
            request.SuccessCallback(result);
        }
    }
}

public class MyRequest
{
    public Action<string> SuccessCallback { get; private set; }
    private Network _network;

    public MyRequest(Action<string> successCallback)
    {
        _network = new Network();
        SuccessCallback = successCallback;
    }
    public void Function()
    {
        _network.SendMyRequest(this);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM