《GOF设计模式》—单件(Singleton)—Delphi源码示例:单件接口(使用全局变量)
2010-10-30 16:07
561 查看
示例:单件接口(使用全局变量)
说明:
(1)、定义
保证一个类仅有一个实例,并提供一个访问它的全局访问点。
(2)、结构
Singleton:
定义一个Instance类操作,负责创建它自己的唯一实例。
客户只能通过Instance操作访问一个Singleton的实例。
(3)、特点
让类自身负责保存它的唯一实例。这个类可以保证没有其他实例可以被创建(通过截取创建新对象的请求),并且它可以提供一个访问该实例的方法。这就是单件模式。
(4)、效果
<1>、对唯一实例的受控访问
因为Singleton类封装它的唯一实例,所以它可以严格的控制客户怎样以及何时访问它。
<2>、缩小名空间
Singleton模式是对全局变量的一种改进。它避免了那些存储唯一实例的全局变量污染名空间。
代码:
unit uSingleton1;
interface
uses
SysUtils, Dialogs;
type
TSingleton = class(TObject)
private
FSingletonData: string;
function GetSingletonData: string;
procedure SetSingletonData(const Value: string);
public
constructor Create;
destructor Destroy; override;
//---
class function Instance: TSingleton;
procedure SingletonOperation;
//---
property SingletonData: string read GetSingletonData write SetSingletonData;
end;
implementation
var
unigueInstance: TSingleton;
constructor TSingleton.Create;
begin
if unigueInstance = nil then
unigueInstance := Self
else
abort;
end;
destructor TSingleton.Destroy;
begin
unigueInstance := nil;
//---
inherited;
end;
class function TSingleton.Instance: TSingleton;
begin
if unigueInstance = nil then
unigueInstance := TSingleton.Create;
//---
Result := unigueInstance;
end;
function TSingleton.GetSingletonData: string;
begin
Result := FSingletonData;
end;
procedure TSingleton.SetSingletonData(const Value: string);
begin
FSingletonData := Value;
end;
procedure TSingleton.SingletonOperation;
begin
ShowMessage(FSingletonData);
end;
end.
procedure TForm1.Button5Click(Sender: TObject);
begin
//---
with TSingleton.Instance do
begin
SingletonData := '123';
SingletonOperation;
end;
//---
with TSingleton.Instance do
begin
SingletonData := '456';
SingletonOperation;
end;
//---
TSingleton.Instance.Free;
end;
说明:
(1)、定义
保证一个类仅有一个实例,并提供一个访问它的全局访问点。
(2)、结构
Singleton:
定义一个Instance类操作,负责创建它自己的唯一实例。
客户只能通过Instance操作访问一个Singleton的实例。
(3)、特点
让类自身负责保存它的唯一实例。这个类可以保证没有其他实例可以被创建(通过截取创建新对象的请求),并且它可以提供一个访问该实例的方法。这就是单件模式。
(4)、效果
<1>、对唯一实例的受控访问
因为Singleton类封装它的唯一实例,所以它可以严格的控制客户怎样以及何时访问它。
<2>、缩小名空间
Singleton模式是对全局变量的一种改进。它避免了那些存储唯一实例的全局变量污染名空间。
代码:
unit uSingleton1;
interface
uses
SysUtils, Dialogs;
type
TSingleton = class(TObject)
private
FSingletonData: string;
function GetSingletonData: string;
procedure SetSingletonData(const Value: string);
public
constructor Create;
destructor Destroy; override;
//---
class function Instance: TSingleton;
procedure SingletonOperation;
//---
property SingletonData: string read GetSingletonData write SetSingletonData;
end;
implementation
var
unigueInstance: TSingleton;
constructor TSingleton.Create;
begin
if unigueInstance = nil then
unigueInstance := Self
else
abort;
end;
destructor TSingleton.Destroy;
begin
unigueInstance := nil;
//---
inherited;
end;
class function TSingleton.Instance: TSingleton;
begin
if unigueInstance = nil then
unigueInstance := TSingleton.Create;
//---
Result := unigueInstance;
end;
function TSingleton.GetSingletonData: string;
begin
Result := FSingletonData;
end;
procedure TSingleton.SetSingletonData(const Value: string);
begin
FSingletonData := Value;
end;
procedure TSingleton.SingletonOperation;
begin
ShowMessage(FSingletonData);
end;
end.
procedure TForm1.Button5Click(Sender: TObject);
begin
//---
with TSingleton.Instance do
begin
SingletonData := '123';
SingletonOperation;
end;
//---
with TSingleton.Instance do
begin
SingletonData := '456';
SingletonOperation;
end;
//---
TSingleton.Instance.Free;
end;
相关文章推荐
- 《GOF设计模式》—单件(Singleton)—Delphi源码示例:创建Singleton类的子类(使用环境变量)
- 《GOF设计模式》—单件(Singleton)—Delphi源码示例:单件接口(使用类字段)
- 《GOF设计模式》—单件(Singleton)—Delphi源码示例:创建Singleton类的子类(使用注册表)
- 《GOF设计模式》—单件(Singleton)—Delphi源码示例:创建Singleton类的子类(重载Instance方法)
- 《GOF设计模式》—单件(Singleton)—Delphi源码示例:基于单件的迷宫
- 《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:可插入的Adapter(使用代理对象)
- 《GOF设计模式》—观察者(OBSERVER)—Delphi源码示例:观察者接口
- 《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:最大化Component接口
- 《GOF设计模式》—原型(Prototype)—Delphi源码示例:原型接口
- 《GOF设计模式》—适配器(ADAPTER)—Delphi源码示例:使用双向适配器提供透明操作
- 《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:桥接接口
- 《GOF设计模式》—职责链(CHAIN OF RESPONSIBILITY)—Delphi源码示例:表示请求(使用处理函数)
- 《GOF设计模式》—命令(COMMAND)—Delphi源码示例:命令接口
- 《GOF设计模式》—中介者 (MEDIATOR)—Delphi源码示例:中介者接口
- 《GOF设计模式》—解释器 (INTERPRETER)—Delphi源码示例:解释器接口
- 《GOF设计模式》—职责链(CHAIN OF RESPONSIBILITY)—Delphi源码示例:表示请求(使用请求对象)
- 《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:使用高速缓冲存贮改善性能
- 《GOF设计模式》—迭代器 (ITERATOR)—Delphi源码示例:内部迭代器(使用函数指针)
- 《GOF设计模式》—解释器 (INTERPRETER)—Delphi源码示例:使用Flyweight模式共享终结符
- 《GOF设计模式》—中介者 (MEDIATOR)—Delphi源码示例:Colleague—Mediator通信(使用观察者模式)