????Downloader.cs
class Downloader
{
public void Download(string resource)
{
for (int i = 1?? size = 10; i this.OnProrgess(i?? size);
Thread.Sleep(500);
}
}
public void AddMonitor(IProgressMonitor monitor)
{
this._monitors.Add(monitor);
}
public void RemoveMonitor(IProgressMonitor monitor)
{
this._monitors.Remove(monitor);
}
private void OnProrgess(int done?? int total)
{
foreach (var i in this._monitors)
{
i.OnProgress(done?? total);
}
}
private ICollection _monitors = new List();
//????????????
}
Program.cs
class Program
{
static void Main(string[] args)
{
var resouce = "visual studio.iso";
var downloader = new Downloader();
downloader.AddMonitor(new ProgressVeiw());
Console.WriteLine("???????? " + resouce);
downloader.Download(resouce);
Console.ReadKey();
}
}
????????????????????
??????????????
????ProgressView?????????“?????”????????????“??????”
?????????????????
?????????????????????б仯???????????????????Щ?????????????????????????
????ProgressView??????????????
??????????п???????????“???”??“??”??????????????????????????????????.Net????????????????????С?
????3.1 ????У?MulticastDelegate??
????????????

????.Net?????о????????????????????MulticastDelegate??????????????????飬??????????????У???????ν??“?????”?????????????????н??????????????п????????ж???????????磺
????Action act = ()=>Console.WriteLine("????1");
????act += ()=>Console.WriteLine("????2");
????act += ()=>Console.WriteLine("????3");
????act();
????//???????????????
??????????????????+=???????滻????Delegate.Combine(Delegate a?? Delegate b)???????÷??????????????????????????CombineImpl????????????????????????
??????????е????????????????????????????????????????????????????????????????????????
????Downloader.cs
class Downloader
{
public void Download(string resource)
{
for (int i = 1?? size = 10; i this.OnProrgess(i?? size);
Thread.Sleep(500);
}
}
//????ж???У????????????“???”??“???”??????
private void OnProrgess(int done?? int total)
{
if (this.DownloadProgress != null)
this.DownloadProgress(done?? total);
}
//NOTE: ????????? +=??-=?????????“???”??“???”???????????????????????????
public Actionint?? int> DownloadProgress;
//????????
}
???IProgressMonitor???????????????????伴?????????y???????
????Program.cs
class Program
{
static void Main(string[] args)
{
var resouce = "visual studio.iso";
var downloader = new Downloader();
downloader.DownloadProgress += new ProgressVeiw().OnProgress;
Console.WriteLine("???????? " + resouce);
downloader.Download(resouce);
Console.ReadKey();
}
}
???????????Downloader.cs????е?NOTE?????????????У?????????????????????????????????public???Σ?????????????????Υ?????????????????????????????????з???????
????3.2 ???
????????????????????????event?????
????//??event???Σ?????????????-=??+=?????????????????????????
????public event Actionint?? int> DownloadProgress;
????//???????
???????????????????????????????????????????????????????????????????????????????????????
???????????棬????????????Action????????????+=??-=???????public??addXxx??removeXxx??????
????????????????????????????????г?????????????????????磺
????public event Actionint?? int> DownloadProgress
????{
????add
????{
????_downloadProgress += value;
????}
????remove
????{
????_downloadProgress -= value;
????}
????}
????private Actionint?? int> _downloadProgress;
?????????????????????????????????????event??????????????????????
??????????????????
????C++
????C/C++?е?????????.Net????е?????????
????????????“????”???????unsigned int????????????????????CPU?????????
???????????????CLR????????????????????????????????????????????????
????C++?????????ù??????????????????????????qt??boost??????“???(signal)-??(slot)”?????????????.Net?е????????????????????????
???????boost??signals2????????????????????????????????????????slot????????????????????slot??????????????????????????????????slot??????????????У?????signal????????????????????????????????????????????????3???crash??????????
????Java
????java?????????????????????????”????????? + ?????????”???棬java8??????api?????????????????lambda???????????????????????
???????Щ????????????????????????????磺???guava?е?EventBus??
????Ruby
????ruby??????????????????????block??????飬?????????????????do |param| ...end ?? { |param| do_something param }?????????????Smalltalk????
??????????д???????????????“???”?????????????????????????????????????????ruby?????????????κη???????????????block???????????????????????б????????????????????????yield???????????????block???磺
????def process_data data
????do_work(data) # ????????????
????yield data if block_given? # ?????block???????
????end
????process_data([1?? 2?? 3]) do |data|
????puts '????????'
????end
??????????????е?File::open????
????# ????÷???????
????#     ????block?????????????block???????????????????????????????????鷳?????????????
????# ????
????#     ???????????
????File.open('file.txt') do |f|
????puts f.readlines # ???????????
????end
???????ruby?????web???Sinatra??API
????get '/home/index' do
????'?????get????????'
????end
????post '/home/index' do
????'?????post????????'
????end
??????????????дHTTP Web???????????????????API??????????????????HTTP??DSL?????????????????
??????????????????????????.Net????web???Nancy??????????д???????????API????????ASP.NET MVC??
???????????????????????content negotiation??????Э?????????????????????????????
????Get["/"] = parameters => {
????//????????????????nancy??content negotiation???
????//?????????????accept????????? ?????xml?????????л??xml???????json???????json
????return new MyModel();
????};