学习 Singleton 模式 (100分)

  • 主题发起人 主题发起人 happyjoe
  • 开始时间 开始时间
H

happyjoe

Unregistered / Unconfirmed
GUEST, unregistred user!
说实话我到目前为止并不是很清楚Singleton模式,到网上找了些资料说明如下:
Singleton(单件)模式是一种很有用的设计模式。它的意图的是:仅仅创建一个类
的实例,并提供一个访问它的全局访问点。全局变量使的一个对象易被访问,但
不能防止你实例化多个对象。单件模式的目的就是确保在程序的生命周期内只有
一个实例存在。


如果要实现上面说的目的的话,delphi的vcl里就有现成的例子(参考Themes.pas单元)。
我写个简单例子如下:

...
type
TTest = class
private

public
constructor Create;
end;
TTestClass = class of TTest;

function MyTest: TTest;

implementation

var
InternalObject: TTest;

function MyTest: TTest;
begin
if InternalObject = nil then
InternalObject := TTest.Create;
Result := InternalObject;
end;

...

initialization
finalization
InternalObject.Free;

end.

不知道上面这种方法能不能实现Singleton的所有功能,和Singleton模式比有何不足?
 
补充:通过访问 MyTest 方法(非类成员),就可以保证整个程序中只有一个TTest的实例
 
这个不是singleton模式 用户可以不用mytset而直接使用 TTest.create建立 TTest的多个实例.在delphi中可以使用Modlemaker 方便的实现singleton 甚至不用写代码.
 
这是例子

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
unit Singleton;

interface

uses
SysUtils, Windows, Messages, Classes, Graphics, Controls,
Forms, Dialogs;

type


TDefault1 = class(TObject)
private
FInt: string;
protected
constructor CreateInstance;
class function AccessInstance(Request: Integer): TDefault1;
public
constructor Create;
destructor Destroy
override;
class function Instance: TDefault1;
procedure Member1;
class procedure ReleaseInstance;
property Int: string read FInt write FInt;
end;


implementation


{
********************************** TDefault1 ***********************************
}
constructor TDefault1.Create;
begin
inherited Create;
raise Exception.CreateFmt('Access class %s through Instance only', [ClassName]);
end;

constructor TDefault1.CreateInstance;
begin
inherited Create;
end;

destructor TDefault1.Destroy;
begin
if AccessInstance(0) = Self then AccessInstance(2);
inherited Destroy;
end;

class function TDefault1.AccessInstance(Request: Integer): TDefault1;

{$J+}
const FInstance: TDefault1 = nil;
{$J-}

begin
case Request of
0 : ;
1 : if not Assigned(FInstance) then FInstance := CreateInstance;
2 : FInstance := nil;
else
raise Exception.CreateFmt('Illegal request %d in AccessInstance', [Request]);
end;
Result := FInstance;
end;

class function TDefault1.Instance: TDefault1;
begin
Result := AccessInstance(1);
end;

procedure TDefault1.Member1;
begin
ShowMessage(Fint);
end;

class procedure TDefault1.ReleaseInstance;
begin
AccessInstance(0).Free;
end;


initialization

finalization
TDefault1.ReleaseInstance ;

end.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
这样调用它
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,Singleton, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

begin
TDefault1.Instance.Int:='1';
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
TDefault1.Instance.Member1;
end;

end.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
后退
顶部