在Delphi中实现观察器模式的最佳方式 [英] Best way to implement observer pattern in Delphi

查看:184
本文介绍了在Delphi中实现观察器模式的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Delphi中发现了观察者模式的不同实现,如:
Sourcemaking Design Patterns < a>和德尔福业余爱好者



一般来说,在Delphi中实现观察者的最佳方法是什么?

我会说使用接口,因为代码更易读。 p>

解决方案

没有好或最好的方式来实现模式。

您选择的实现取决于关于您如何使用它。



您可以使用泛型功能(Delphi 2009可用)可以简化许多模式的使用。 p>

如果您使用Pre Delphi-3版本,或者想避免引用计数,则无法使用接口。

(引用计数可以打开一个新的与传统的业主/所有者的生活时间管理混合时可以蠕虫;请确保下载从正确的祖先公开接口的类,例如 TInterfacedObject ,然后观察生命周期管理)。



除了纯粹的问题如何实现观察者模式,还能很好地识别Delphi中的实现观察者模式。



例如 TDataSet / TDataSource 还实现了观察者模式

数据感知控件取决于它,全部通过 TDataLink



我已经写了一个 TDataLink ,它反映了<$ c中的所有虚拟方法的SourceControl / changeset / view / 57889#1271339rel =noreferrer> TDataLinkReflector 组件$ c> TDataLink 到 TDataLinkReflector 中的事件。



基于 TDataLinkReflector 我写了 TDataAwareControlController 组件根据TDataSet,其TField和TDataSource连接到TDataSet(着色为只读,必需等),对数据感知控件进行各种有趣的事情。



但是,即使像事件一样看似简单的事情也可以看作是基于这种模式(虽然事件是单一的,只有一个观察者可以观看一个事件)。



另一个实现这一点的是 TApplicationEvents ;每个实例都可以让您听取TApplication上的任何事件。



我希望能够清楚地看到Delphi中使用观察者模式。



- jeroen



PS:任何对我写的组件感兴趣的人都可能想看到此处提到的CodeRage视频


I found different implementations of the observer pattern in Delphi, like: Sourcemaking Design Patterns and Delphi Hobbyist.

In general, what is the best way to implement an observer in Delphi?
I would say using interfaces because the code is more readable.

解决方案

There is no "good" or "best" way to implement patterns.
The implementation you choose depends on how you want to use it.

You could for instance also use the generics feature (available since Delphi 2009) to simplify the use of a lot of patterns.

And if you use Pre Delphi-3 versions, or want to avoid reference counting you cannot use interfaces.
(Reference counting can open a new can of worms when mixed with traditional Owner/Ownee based life time management; be sure to descend classes that expose interfaces from the right ancestor - like TInterfacedObject - and watch your life time management).

Apart from the "pure" question on how to implement the observer pattern, it is also good to be able to recognize classes in Delphi that implement the observer pattern.

For instance the TDataSet/TDataSource also implement the observer pattern.
The whole concept of Data Aware Controls depends on it, all bound through the TDataLink.

I have written a TDataLinkReflector component based on the TDataLink, which reflects all the virtual methods in TDataLink to events in TDataLinkReflector.

Base on TDataLinkReflector I wrote TDataAwareControlController components that does all kind of interesting things to Data Aware Controls based on the TDataSet, its TFields and the TDataSource linking to the TDataSet (coloring on read-only, required, etc).

But even a seemingly simple thing like events can be seen as based on that pattern (though events are single cast, so only one observer can watch one event).

Another class implementing this is the TApplicationEvents; each instance lets you listen to any of the events on TApplication.

I hope that sheds some light on where the observer patterns is used in Delphi.

--jeroen

PS: Anyone interested in the components I wrote might want to see the CodeRage video mentioned here.

这篇关于在Delphi中实现观察器模式的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆