????????????(SmartClock)????????????????????????(MilkSchedule)?ж??????Alarm?????
public void PrepareMilkInTheMorning()
{
_clock.Alarm += (clock?? args) =>
{
Message =
"Prepraring milk for the owner?? The time is {0}?? the electric quantity is {1}%".FormatWith(
args.AlarmTime?? args.ElectricQuantity*100);
Console.WriteLine(Message);
};
_clock.SetAlarmTime(TimeSpan.FromSeconds(2));
}
??????????濾?????????????_clock.Alarm+=(clock??args)=>{//it is time to roast bread}?????????????
?????????event????????????????????е??????????????????ò???????memory leak?????????????(obsever)?????????????????????????(?????????????????????)???ù?????????????????(??????????????????)?????Understanding and Avoiding Memory Leaks with Event Handlers and Event Aggregators???????????????????????(-=)??
???????????A?д????????????????y???????????????ν????????μ?Memory Leak????Weak Event Handlers??
????????????.net??IObservable<out T>??IObserver<in T>?????????
????IObservable<out T> ???????-???????????????(subject)??Observer???????????????
??????????????????????????IObservable???y????????????????IDisposable Subscribe(IObserver<T> observer);?÷????????????е??Σ?Subscribe?????????????????????????????(observer)????????(subject)??????????????(subject)??????????(observer)??????????????????????????£?????(subject)?????????????(observer)?б???????????????????????????????????IDisposable Subscribe(IObserver<T> observer)????
????public IDisposable Subscribe(IObserver<AlarmData> observer)
????{
????if (!_observers.Contains(observer))
????{
????_observers.Add(observer);
????}
????return new DisposedAction(() => _observers.Remove(observer));
????}
?????????????????????????????б?_observers???????????????????????й?????б?????????????????
????public override void ItIsTimeToAlarm()
????{
????var alarm = new AlarmData(_alarmTime.Value?? 0.92m);
????_observers.ForEach(o=>o.OnNext(alarm));
????}
?????????????????и?OnNext??????????????????AlarmData?????????????????????????????????????????????????????????????(observer)???????IObserver???
public  void Subscribe(TimeSpan timeSpan)
{
_unSubscriber = _clock.Subscribe(this);
_clock.SetAlarmTime(timeSpan);
}
public  void Unsubscribe()
{
_unSubscriber.Dispose();
}
public void OnNext(AlarmData value)
{
Message =
"Prepraring milk for the owner?? The time is {0}?? the electric quantity is {1}%".FormatWith(
value.AlarmTime?? value.ElectricQuantity * 100);
Console.WriteLine(Message);
}
????????????????????????濾?????????????????????Subscribe()??Unsubscribe()???????ù???
????var milkSchedule = new MilkSchedule();
????//Act
????milkSchedule.Subscribe(TimeSpan.FromSeconds(12));
????????Action?????????
?????????÷????????????????÷??????????????????????????????????????????????????????????????????????????????÷???
????????????У?????????(smartClock)????API?????????????
????public void SetAlarmTime(TimeSpan timeSpan??Action<AlarmData> alarmAction)
????{
????_alarmTime = _now().Add(timeSpan);
????_alarmAction = alarmAction;
????RunBackgourndRunner(_now?? _alarmTime);
????}
?????????????????????Action<T>?????????????????и?Action<T>????:
????public override void ItIsTimeToAlarm()
????{
????if (_alarmAction != null)
????{
????var alarmData = new AlarmData(_alarmTime.Value?? 0.92m);
????_alarmAction(alarmData);
????}
????}
?????????????????????API??????
????_clock.SetAlarmTime(TimeSpan.FromSeconds(1)?? (data) =>
????{
????Message =
????"Prepraring milk for the owner?? The time is {0}?? the electric quantity is {1}%".FormatWith(
????data.AlarmTime?? data.ElectricQuantity * 100);
????});
???????????ù?????????????API????fluent api?????????????????????
????????????(smartClock)?е?API:
????public Clock SetAlarmTime(TimeSpan timeSpan)
????{
????_alarmTime = _now().Add(timeSpan);
????RunBackgourndRunner(_now?? _alarmTime);
????return this;
????}
????public void OnAlarm(Action<AlarmData> alarmAction)
????{
????_alarmAction = alarmAction;
????}
?????????????н??е????
????_clock.SetAlarmTime(TimeSpan.FromSeconds(2))
????.OnAlarm((data) =>
????{
????Message =
????"Prepraring milk for the owner?? The time is {0}?? the electric quantity is {1}%".FormatWith(
????data.AlarmTime?? data.ElectricQuantity * 100);
????});
?????????????д??????????????.???????????().???????(()=>{??????1???})
????????????д????????????????????????????????????????????????濾???????????API?????????????????????????????????????????á?