转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
Introduction to Design Patterns in Delphi         ★★★★

Introduction to Design Patterns in Delphi

作者:闵涛 文章来源:闵涛的学习笔记 点击数:3486 更新时间:2009/4/23 18:42:47
Day: 0..31; Month: 1..12; Year: 0..99; end; TOldCustomer = class FCustomerID: Integer; FName: string; FDOB: TOldDOB; public constructor Create(CustID: Integer); property CustomerID: Integer read FCustomerID; property Name: string read FName; property DOB: TOldDOB read FDOB; end; constructor TOldCustomer.Create(CustID: Integer); begin FCustomerID := CustomerID; FName := ''''An Old_Customer''''; with FDOB do begin Day := 1; Month := 1; Year := 1; end; end; type { The Adapter class } TAdaptedCustomer = class(TNewCustomer) private FOldCustomer: TOldCustomer; protected function GetCustomerID: Longint; override; function GetFirstName: string; override; function GetLastName: string; override; function GetDOB: TDateTime; override; public constructor Create(CustID: Longint); override; destructor Destroy; override; end; { The Adapter class } constructor TAdaptedCustomer.Create(CustID: Longint); begin inherited Create(CustID); FOldCustomer := TOldCustomer.Create(CustID); end; destructor TAdaptedCustomer.Destroy; begin FOldCustomer.Free; inherited Destroy; end; function TAdaptedCustomer.GetCustomerID: Longint; begin Result := FOldCustomer.CustomerID; end; function TAdaptedCustomer.GetFirstName: string; var SpacePos: integer; begin SpacePos := Pos('''' '''', FOldCustomer.Name); if SpacePos = 0 then Result := '''''''' else Result := Copy(FOldCustomer.Name,1,SpacePos-1); end; function TAdaptedCustomer.GetLastName: string; var SpacePos: integer; begin SpacePos := Pos('''' '''', FOldCustomer.Name); if SpacePos = 0 then Result := FOldCustomer.Name else Result := Copy(FOldCustomer.Name,SpacePos+1,255); end; function TAdaptedCustomer.GetDOB: TDateTime; var FullYear: Word; begin if CustomerID > Last_OldCustomer_At_Year_2000 then FullYear := 2000 + FOldCustomer.DOB.Year else FullYear := 1900 + FOldCustomer.DOB.Year; Result := EncodeDate(FullYear, FOldCustomer.DOB.Month, FOldCustomer.DOB.Day); end; function GetCustomer(CustomerID: Longint): TNewCustomer; begin if CustomerID > Last_OldCustomer_In_Database then Result := TNewCustomer.Create(CustomerID) else Result := TAdaptedCustomer.Create(CustomerID) as TNewCustomer; end; end.


Pattern: Template Method

Definition

"Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm''''s structure."

This pattern is essentially an extension of abstract methods to more complex algorithms.

Applications in Delphi

Abstraction is implemented in Delphi by abstract virtual methods. Abstract methods differ from virtual methods by the base class not providing any implementation. The descendant class is completely responsible for implementing an abstract method. Calling an abstract method that has not been overridden will result in a runtime error.

A typical example of abstraction is the TGraphic class.

TGraphic is an abstract class used to implement TBitmap, TIcon and TMetafile. Other developers have frequently used TGraphic as the basis for other graphics objects such as PCX, GIF, JPG representations. TGraphic defines abstract methods such as Draw, LoadFromFile and SaveToFile which are then overridden in the concrete classes. Other objects that use TGraphic, such as a TCanvas only know about the abstract Draw method, yet are used with the concrete class at runtime.

Many classes that use complex algorithms are likely to benefit from abstraction using the template method approach. Typical examples include data compression, encryption and advanced graphics processing.

Implementation Example

To implement template methods you need an abstract class and concrete classes for each alternate implementation. Define a public interface to an algorithm in an abstract base class. In that public method, implement the steps of the algorithm in calls to protected abstract methods of the class. In concrete classes derived from the base class, override each step of the algorithm with a concrete implementation specific to that class.

This example shows some very simple alogrithm steps, but illustrates the principle of deferring implementation to a subclass.



unit Tpl_meth;



interface



type

  TAbstractTemplateClass = class(TObject)

  protected

    function Algorithm_StepA: Integer; virtual; abstract;

    function Algorithm_StepB: Integer; virtual; abstract;

    function Algorithm_StepC: Integer; virtual; abstract;

  public

    function Algorithm: Integer;

  end;



  TConcreteClassA = class(TAbstractTemplateClass)

  protected

    function Algorithm_StepA: Integer; override;

    function Algorithm_StepB: Integer; override;

    function Algorithm_StepC: Integer; override;

  end;



  TConcreteClassB = class(TAbstractTemplateClass)

  protected

    function Algorithm_StepA: Integer; override;

    function Algorithm_StepB: Integer; override;

    function Algorithm_StepC: Integer; override;

  end;




Pattern: Builder

Definition

"Separate the construction of a complex object from its representation so that the same construction process can create different representations."

A Builder seems similar in concept to the Abstract Factory. The difference as I see it is the Builder refers to single complex objects of different concrete classes but containing multiple parts, whereas the abstract factory lets you create whole families of concrete classes. For example, a builder might construct a house, cottage or office. You might employ a different builder for a brick house or a timber house, though you would give them both similar instructions about the size and shape of the house. On the other hand the factory generates parts and not the whole. It might produce a range of windows for buildings, or it might produce a quite different range of windows for cars.

Applications in Delphi

The functionality used in Delphi''''s VCL to create forms and components is similar in concept to the builder. Delphi creates forms using a common interface, through Application.CreateForm and through the TForm class constructor. TForm implements a common constructor using the resource information (DFM file) to instantiate the components owned by the form. Many descendant classes reuse this same construction process to create different representations. Delphi also makes developer extensions easy. TForm''''s OnCreate event also adds a hook into the builder process to make the functionality easy to extend.

Implementation Example

The following example includes a class TAbstractFormBuilder and two concrete classes TRedFormBuilder and TBlueFormBuilder. For ease of development some common functionality of the concrete classes has been moved into the shared TAbstractFormBuilder class.



type

  TAbstractFormBuilder = class

  private

    FForm: TForm;

    procedure BuilderFormClose(Sender: TObject; var Action: TCloseAction);

  protected

    function GetForm: TForm; virtual;

  public

    procedure CreateForm(AOwner: TComponent); virtual;

    procedure CreateSpeedButton; virtual; abstract;

    procedure CreateEdit; virtual; abstract;

    procedure CreateLabel; virtual; abstract;

    property Form: TForm read GetForm;

  end;



type

  TRedFormBuilder = class(TAbstractFormBuilder)

  private

    FNextLeft, FNextTop: Integer;

  public

    procedure CreateForm(AOwner: TComponent); override;

    procedure CreateSpeedButton; override;

    procedure CreateEdit; override;

    procedure CreateLabel; override;

  end;



type

  TBlueFormBuilder = class(TAbstractFormBuilder)

  private

    FNextLeft, FNextTop: Integer;

  public

    procedure CreateForm(AOwner: TComponent); override;

    procedure CreateSpeedButton; override;

    procedure CreateEdit; override;

    procedure CreateLabel; override;

  end;



At runtime the client application instructs one of the concrete classes to create parts using the public part creation procedures. The concrete builder instance is passed to the folliwing procedure:



procedure TForm1.Create3ComponentFormUsingBuilder(ABuilder: TAbstractFormBuilder);

var

  NewForm: TForm;

begin

  with ABuilder do begin

    CreateForm(Application);

    CreateEdit;

    CreateSpeedButton;

    CreateLabel;

    NewForm := Form;

    if NewForm <> nil then NewForm.Show;

  end;

end;




Pattern: Abstract Factory

Definition

"Provide an interface for creating families of related or dependant objects without specifying their concrete classes."

The Factory Method pattern below is commonly used in this pattern.

Applications in Delphi

This pattern is ideal where you want to isolate your application from the implementation of the concrete classes. For example if you wanted to overlay Delphi''''s VCL with a common VCL layer for both 16 and 32 bit applications, you might start with the abstract factory as a base.

Implementation Example

The following example uses an abstract factory and two concrete factory classes to implement different styles of user interface components. TOAbstractFactory is a singleton class, since we usually want one factory to be used for the whole application.



TOAbstractFactory = class(TObject)

  public

    constructor Create;

    destructor Destroy; override;

    { abstract widget constructors }

    function CreateSpeedButton(AOwner: TComponent): TSpeedButton; virtual; abstract;

    function CreateEdit(A

上一页  [1] [2] [3] [4]  下一页


[办公软件]在Excel中插入时间/日期TODAY和NOW函数  [网络安全]激活型触发式AutoRun.inf病毒和rose病毒的清除方案
[Web开发]asp.net c#其它语句之break、continue、goto实例介…  [Web开发]一大堆常用的超强的正则表达式及RegularExpressio…
[平面设计]制作动态GIF图像的好帮手─Coffee GIf Animator  [平面设计]功能超强的GIF动画制作软件─Ulead Gif Animator
[平面设计]AutoCAD常用快捷命令(外加中文说明)  [平面设计]AutoCAD常用快捷命令(字母类,外加中文说明)
[平面设计]AutoCAD快捷命令介绍  [平面设计]多种方法解决AutoCAD打印时出现错误
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台