Delphi 泛型类可以从其类参数派生吗?

发布于 2024-09-28 12:33:30 字数 960 浏览 8 评论 0原文

我一直在尝试定义一个通用的、可继承的 TSingleton 类。这是我正在进行的工作:

  TSingleton<RealClass, InheritsFrom : class> = class(InheritsFrom)
  strict private
    class var FInstance : RealClass;
  protected
    procedure InstanceInitialization;virtual;
  public
    destructor Destroy; override;
    class procedure Create; reintroduce;
    class function Instance : RealClass;
    class procedure InstanceFree;
  end;

目标是能够将单例模式“插入”继承树中。因此,我不会声明这样的内容:

  TMySingletonComponent = class(TComponent)

  end;

并且需要在那里实现单例模式,我会声明这样的内容:

  TMyGenericSingletonComponent = class(TSingleton<TMyGenericSingletonComponent,TComponent>)
  end;

遗憾的是,这行不通。我收到以下错误(在 D2010 中):

  TSingleton<RealClass, InheritsFrom : class> = class(InheritsFrom) ///E2021 Class type required

现在我想知道这在 Delphi XE 中是否有效?我可以使用一些“干净的技巧”来在 D2010 中完成这项工作吗?有一些根本原因导致这行不通吗?

I've been trying to define a generic, inheritable TSingleton class. Here's what I had in progress:

  TSingleton<RealClass, InheritsFrom : class> = class(InheritsFrom)
  strict private
    class var FInstance : RealClass;
  protected
    procedure InstanceInitialization;virtual;
  public
    destructor Destroy; override;
    class procedure Create; reintroduce;
    class function Instance : RealClass;
    class procedure InstanceFree;
  end;

The goal was to be able to "insert" the singleton pattern in an inheritance tree. so instead of declaring something like this :

  TMySingletonComponent = class(TComponent)

  end;

And need to implement the singleton pattern there, I would declare something like this :

  TMyGenericSingletonComponent = class(TSingleton<TMyGenericSingletonComponent,TComponent>)
  end;

Sadly, this won't work. I'm getting the following error(In D2010):

  TSingleton<RealClass, InheritsFrom : class> = class(InheritsFrom) ///E2021 Class type required

Now I was wondering, would this work in Delphi XE? Is there some "clean hack" I could use to make this work in D2010? Is there some fundamental reasons why this can't work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笔落惊风雨 2024-10-05 12:33:30

根据设计,您无法创建从其类型参数之一派生的泛型类。

By design, you can't create a generic class which derives from one of its type arguments.

小兔几 2024-10-05 12:33:30

不,那行不通。您试图根据自身来定义一个类。无论您在参数中放入什么内容,都必须已经完全定义。

No, that won't work. You're trying to define a class in terms of itself. Whatever you put inside the parameters has to be fully defined already.

山色无中 2024-10-05 12:33:30

你想获得什么?

恕我直言,单身人士是邪恶的。它们的引入是因为 C++ 的糟糕的 OOP 设计(据我所知,用于访问控制台应用程序中的输入/输出流)。而且它们往往很难维护。

没有他们你仍然可以生活。它绝对不是“Delphi 经典”编程方式,因为 Delphi 不会遇到我提到的 C++ 问题。

一些 Java 项目(ab)使用了单例。谷歌一下,你就会明白我的意思。

将普通类的属性与 getter 一起使用,如果相应字段仍为 nil,则初始化实例;如果实例已创建,则直接将字段指针返回到该实例。您将拥有单例功能,具有良好的性能、良好的代码、良好的 OOP 实践(没有“全局”类),并且能够在没有任何单例功能的情况下运行该类,如果您以后不需要此功能(用于测试)例如,目的)。

What do you want to obtain?

IMHO, singletons are evil. They were introduced because of bad OOP design of C++ (for access to input/output streams in console applications, as far as I remember). And they tend to be like hell to maintain.

You can always live without them. It's definitively not a "Delphi classical" way of programing, because Delphi doesn't suffer the C++ problems I mentioned.

Some Java project (ab)uses of singleton. Google for it, and you'll find out what I mean.

Use a property of a common class with a getter, initializing an instance if the corresponding field is still nil, or directly returning the field pointer to the instance if it was already created. You'll have the singleton feature, with good performance, nice code, good OOP practice (no "global" class), and the ability to run the class without any singleton feature, if you don't need this feature later (for testing purpose, for instance).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文