中国程序员联盟 正在重新改版中ing 不便之处还请见谅 改版后将内容涉及java delphi .net php
 
  首页 | 数据库开发 | 网络通讯 | 多线程 | 多媒体开发 | 图像处理 | 程序人生 | 系统函数 | 控件开发 | Web服务
 
  当前位置:笨鱼delphi技术网>数据库开发>设计模式>文章内容

delphi Singleton Pattern (设计模式)

来源:站内 关于:bill 发布时间:2007-06-23   [收藏] [推荐]

Ensures a class has only one instance and provides a global point of access to it.

  • Use when there must be exactly one instance of a class, and it must be accessible to clients from a well known point.
  • Good when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.

Structure of the Singleton Pattern

Singleton Graphic

Implementation of the Singleton Pattern

Two sample implementations are shown in TSingletonForm (uSingletonF.pas). The first one, returns a reference to allow the client class further control over the singleton instance. In this sample the Instance() class method acts as a well known point for clients to get a reference to the instance of TSingletonForm. This method acts as gatekeeper and it will only allow one instance of the class to be created.

type
TSingletonForm = class(TForm) ... public ...; class function Instance: TSingletonForm; end; implementation
{$R *.DFM} var SingletonFrm: TSingletonForm; class function TSingletonForm.Instance: TSingletonForm; begin
if not assigned(SingletonFrm) then SingletonFrm := TSingletonForm.Create(Application); result := SingletonFrm; end;

The alternative implementation does the same thing but is presented using the Open() class method, which provides a single point of entry to TSingletonForm class and does not return an instance reference. In a production application, this method could take parameters to further initialize the singleton instance.

class procedure TSingletonForm.Open; 
begin 
  Screen.Cursor := crHourglass; 
  try
    if not assigned(SingletonFrm) then
      SingletonFrm := TSingletonForm.Create(Application); 
    with SingletonFrm do
    try
      // do any form initialization here
      Show; 
    except
      on Exception do 
      begin
        Free; 
        raise;
      end;
    end;
  finally
    Screen.Cursor := crDefault; 
  end; 
end; 

Both samples rely on a global reference (implementation scope) SingletonFrm to the instance of TSingletonForm. The SingletonFrm is used to provide lazy initialization or on-demand construction of the instance, since TSingletonForm is not created until the client class accesses one of its well known entry points (Instance() or Open()). In the destructor, the SingletonFrm reference is set back to nil.

destructor TSingletonForm.Destroy;

begin
  SingletonFrm := nil;
  inherited Destroy; 
end; 

The OnClose event handler calls Release(), which Frees the form instance by using an asynchronous windows message to allow all event handlers to complete safely.

procedure TSingletonForm.FormClose(Sender: TObject; var Action: TCloseAction);

begin
  Release; 
end;

[浏览: 次]   
上一篇:delphi 获取打印驱动,打印端口名   下一篇:delphi Template Pattern (模板)
[收藏] [推荐] [返回顶部] [打印本页] [关闭窗口]  
    评论加载中…
google adsense热点文章
·delphi COM+对象缓冲池
·delphi Delphi_Delphi面向对象编程的20
·delphi Template Pattern (模板)
·delphi Observer Pattern
·delphi Adapter Pattern
·delphi Strategy Pattern (策略)
     delphi技术网 | firefox 下载 | Avant Browser下载 | dedecms 技术网 | drupal 爱好者 | php 技术网
  Copyright@www.delphichm.com,2006-2009.All Rights Reserved.
 
程序员联盟 | delphi Java .net|QQ:707102932