博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Weak Event Patterns
阅读量:5225 次
发布时间:2019-06-14

本文共 5137 字,大约阅读时间需要 17 分钟。

In applications, it is possible that handlers that are attached to event sources will not be destroyed in coordination with the listener object that attached the handler to the source.

This situation can lead to memory leaks.

Windows Presentation Foundation (WPF) introduces a design pattern that can be used to address this issue, by providing a dedicated manager class for particular events and implementing an interface on listeners for that event.

This design pattern is known as the weak event pattern.

 

Why Implement the Weak Event Pattern?

Listening for events can lead to memory leaks. The typical technique for listening to an event is to use the language-specific syntax that attaches a handler to an event on a source.

For example, in C#, that syntax is: source.SomeEvent += new SomeEventHandler(MyEventHandler).

 

This technique creates a strong reference from the event source to the event listener.

Ordinarily, attaching an event handler for a listener causes the listener to have an object lifetime that is influenced by the object lifetime of the source (unless the event handler is explicitly removed).

But in certain circumstances, you might want the object lifetime of the listener to be controlled by other factors, such as whether it currently belongs to the visual tree of the application, and not by the lifetime of the source.

Whenever the source object lifetime extends beyond the object lifetime of the listener, the normal event pattern leads to a memory leak: the listener is kept alive longer than intended.

 

The weak event pattern is designed to solve this memory leak problem.

The weak event pattern can be used whenever a listener needs to register for an event, but the listener does not explicitly know when to unregister.

The weak event pattern can also be used whenever the object lifetime of the source exceeds the useful object lifetime of the listener. (In this case, useful is determined by you.)

The weak event pattern allows the listener to register for and receive the event without affecting the object lifetime characteristics of the listener in any way.

In effect, the implied reference from the source does not determine whether the listener is eligible for garbage collection.

The reference is a weak reference, thus the naming of the weak event pattern and the related APIs.

The listener can be garbage collected or otherwise destroyed, and the source can continue without retaining noncollectible handler references to a now destroyed object.

 

 

Who Should Implement the Weak Event Pattern?

 

Implementing the weak event pattern is interesting primarily for control authors. As a control author, you are largely responsible for the behavior and containment of your control and the impact it has on applications in which it is inserted. This includes the control object lifetime behavior, in particular the handling of the described memory leak problem.

 

Certain scenarios inherently lend themselves to the application of the weak event pattern. One such scenario is data binding. In data binding, it is common for the source object to be completely independent of the listener object, which is a target of a binding. Many aspects of WPF data binding already have the weak event pattern applied in how the events are implemented.

 

 

 

How to Implement the Weak Event Pattern?

Implementing the weak event pattern consists of the following three aspects:

  • Derive a manager from the  class.

  • Implement the  interface on any class that wants to register listeners for the weak event without generating a strong reference to the source.

  • When registering listeners, do not use the conventional add and remove accessors of the event where you want the listener to use the pattern. Instead, use the AddListener and RemoveListener implementations in the dedicated  for that event.

 

WeakEventManager

To implement the weak event pattern, you typically create a manager class with a 1:1 relationship to the event.

For example, if you have an event named Spin, you would create a SpinEventManager class that is the dedicated weak event manager for the event.

If the event exists in more than one class, behaves generally the same in each class, and shares the same event data type, the same manager can be used for each event.

When you derive from the  class, you override two virtual methods and expose several other members whose names are not specifically governed by a virtual template, but should exist nonetheless.

The overrides are used to initiate or terminate event delivery mode by the WPF infrastructure.

The other members provide functionality so that your own  implementations can use the to attach listeners to the event.

For more information about deriving from , see the "Notes to Inheritors" section in the  reference topic.

 

IWeakEventListener

The  interface has a single interface method named . The  implementation must be a centralized implementation that directs any event reference that exists on that class to the appropriate .

For more information about implementing the  interface, see the "Notes to Implementers" section in the method reference topic.

转载于:https://www.cnblogs.com/chucklu/p/4899034.html

你可能感兴趣的文章
设计模式-学习
查看>>
button标签点击实现数量加减
查看>>
重置GNOME-TERMINAL
查看>>
quartz 实现调度任务 SchedulerManager
查看>>
new jordans 9 Nets
查看>>
redis哨兵集群、docker入门
查看>>
rmdir
查看>>
[翻译][架构设计]The Clean Architecture
查看>>
状态压缩DP
查看>>
Shell从入门到精通进阶之四:流程控制
查看>>
腾讯QQ、新浪微博等知名社交网络图标素材
查看>>
正则表达式2
查看>>
Unity3D_(插件)小地图自刷新制作Minimap小地图
查看>>
为什么分布式一定要有Redis?
查看>>
hihoCoder 1233 : Boxes(盒子)
查看>>
HihoCoder 1328 BFS 搜索
查看>>
Day2-h和p标签
查看>>
[回归分析][7]--定性预测变量
查看>>
团队的绩效评估计划
查看>>
纯css实现警示框页面(带关闭窗口按钮)
查看>>