您在常用的Delphi编程中实现什么样的设计模式? [英] What Design Patterns do you implement in common Delphi programming?

查看:149
本文介绍了您在常用的Delphi编程中实现什么样的设计模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

常用Delphi编程中实现什么样的设计模式? Delphi编程中适应哪种模式更容易? (每种语言在不同的领域都是非常出色的,所以在使用Delphi时,什么样的模式很可能是非常强大的结构?)



如果你能说一些Delphi 2009/2010 (因为那些支持泛型和RTTI在2010年)的设计模式的变化。



有很多文章野生互联网,但他们不讨论日常的可用性和模式的变化。 (大多数人只是讨论语言细节,体系结构的变化)。

解决方案

只有少数Delphi开发人员知道每个Delphi开发人员使用工厂模式(delphi.about.com有一个常规Delphi中的示例),但是使用虚拟的Create构造函数实现。



所以,有时间阐明一下: - )



虚拟构造函数就像虚拟方法类似对象实例一样。 p>

工厂模式的整个想法是,将逻辑决定为什么(在这种情况下为类)(在这种情况下为对象实例)为创建实际的创建。



它使用虚拟的Create构造函数:



TComponent有一个 virtual Create构造函数,所以可以被任何降序类覆盖:

  type 
TComponent = class(TPersistent,...)
构造函数Create(AOwner:TComponent);虚拟;
...
end;

例如 TDirectoryListBox.Create构造函数覆盖它:

 键入
TDirectoryListBox = class(...)
构造函数Create(AOwner:TComponent);覆盖
...
end;

您可以将类引用(对象实例引用的类比喻)存储在类型的变量中'类型'。对于组件类,有一个预定义类型类组件中的TComponentClass

  type 
TComponentClass = TComponent类;

当您有一个类型为TComponentClass的变量(或参数)时,可以进行多态构造,即非常非常类似于工厂模式:

  var 
ClassToCreate:TComponentClass;

...

程序SomeMethodInSomeUnit;
begin
ClassToCreate:= TButton;
结束

...

procedure AnotherMethodInAnotherUnit;
var
CreatedComponent:TComponent;
begin
CreatedComponent:= ClassToCreate.Create(Application);
...
end;

Delphi RTL在这里使用这个例子:

  Result:= TComponentClass(FindClass(ReadStr))。Create(nil); 

这里:

  //创建此类网格的另一个实例
SubGrid:= TCustomDBGrid(TComponentClass(Self.ClassType).Create(Self));

Delphi RTL中的首次使用是整个创建过程如何工作的形式,数据结构,框架和正在从DFM文件读取的组件。



表单(datamodule / frame / ...)实际上具有(已发布)的组件列表表单(datamodule / frame / ...)。该列表包括每个组件的实例名称和类引用。
当阅读DFM文件时,Delphi RTL然后:


  1. 查找组件实例名称

  2. 使用该名称查找基础类引用,

  3. 然后使用类引用来动态创建正确的对象

一个常规的Delphi开发人员通常从来没有看到这种情况发生,但是没有它,整个Delphi RAD经验将不会存在。



Allen Bauer (Embarcadero首席科学家)写了一个简短的博客 关于这个话题的文章以及。
还有一个关于 SO 问题构造函数被使用>使用虚拟构造函数。



让我知道虚拟Create构造函数主题是否足够: )



- jeroen


What Design Patterns do you implement in common Delphi programming? What patterns are easier to adapt in Delphi programming? (Every language is excellent in different fields, so what patterns are likely to be very strong structures when using Delphi?)

I would be glad, if you could tell about some changes in design patterns for Delphi 2009 / 2010 (since those support generics, and RTTI in 2010).

There are many articles out there in the wild Internet, but they doesn't discuss the everyday usability and changes in patterns. (Most of them just discuss changes in language specifics, architecture).

解决方案

Only a minority of the Delphi developers knows that every Delphi developer uses a Factory pattern (delphi.about.com has an example in "regular" Delphi), but then implemented using virtual Create constructors.

So: time to shed some light on that :-)

Virtual constructors are to classes like virtual methods are like object instances.

The whole idea of the factory pattern is that you decouple the logic that determines what kind (in this case "class") of thing (in this case "object instance") to create from the actual creation.

It works like this using virtual Create constructors:

TComponent has a virtual Create constructor so, which can be overridden by any descending class:

type
  TComponent = class(TPersistent, ...)
    constructor Create(AOwner: TComponent); virtual;
    ...
  end;

For instance the TDirectoryListBox.Create constructor overrides it:

type
  TDirectoryListBox = class(...)
    constructor Create(AOwner: TComponent); override;
    ...
  end;

You can store a class reference (the class analogy to an object instance reference) in a variable of type 'class type'. For component classes, there is a predefined type TComponentClass in the Classes unit:

type
  TComponentClass = class of TComponent;

When you have a variable (or parameter) of type TComponentClass, you can do polymorphic construction, which is very very similar to the factory pattern:

var
  ClassToCreate: TComponentClass;

...

procedure SomeMethodInSomeUnit;
begin
  ClassToCreate := TButton;
end;

...

procedure AnotherMethodInAnotherUnit;
var
  CreatedComponent: TComponent;
begin
  CreatedComponent := ClassToCreate.Create(Application);
  ...
end;

The Delphi RTL uses this for instance here:

Result := TComponentClass(FindClass(ReadStr)).Create(nil);

and here:

// create another instance of this kind of grid
SubGrid := TCustomDBGrid(TComponentClass(Self.ClassType).Create(Self));

The first use in the Delphi RTL is how the whole creation process works of forms, datamodules, frames and components that are being read from a DFM file.

The form (datamodule/frame/...) classes actually have a (published) list of components that are on the form (datamodule/frame/...). That list includes for each component the instance name and the class reference. When reading the DFM files, the Delphi RTL then:

  1. finds about the components instance name,
  2. uses that name to find the underlying class reference,
  3. then uses the class reference to dynamically create the correct object

A regular Delphi developer usually never sees that happen, but without it, the whole Delphi RAD experience would not exist.

Allen Bauer (the Chief Scientist at Embarcadero), wrote a short blog article about this topic as well. There is also a SO question about where virtual constructors are being used.

Let me know if that was enough light on the virtual Create constructor topic :-)

--jeroen

这篇关于您在常用的Delphi编程中实现什么样的设计模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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