《GOF设计模式》—单件(Singleton)—Delphi源码示例:创建Singleton类的子类(重载Instance方法)
2010-10-30 16:12
633 查看
示例:创建Singleton类的子类(重载Instance方法)
说明:
另一个选择Singleton的子类的方法是将Instance的实现从父类中分离出来并将它放入子类。
代码:
unit uSingleton3;
interface
uses
SysUtils, Dialogs;
type
TSingleton = class
private
class var
FInstance: TSingleton;
public
constructor Create;
destructor Destroy; override;
//---
class function Instance: TSingleton;virtual;
procedure Operate;virtual;
end;
TSingletonA = class(TSingleton)
public
class function Instance: TSingleton; override;
procedure Operate; override;
end;
TSingletonB = class(TSingleton)
public
class function Instance: TSingleton; override;
procedure Operate; override;
end;
implementation
constructor TSingleton.Create;
begin
if FInstance = nil then
FInstance := Self
else
abort;
end;
destructor TSingleton.Destroy;
begin
FInstance := nil;
//---
inherited;
end;
class function TSingleton.Instance: TSingleton;
begin
if FInstance = nil then
FInstance := TSingleton.Create;
//---
Result := FInstance;
end;
procedure TSingleton.Operate;
begin
ShowMessage('Singleton');
end;
class function TSingletonA.Instance: TSingleton;
begin
if FInstance = nil then
FInstance := TSingletonA.Create;
//---
Result := FInstance;
end;
procedure TSingletonA.Operate;
begin
ShowMessage('SingletonA');
end;
class function TSingletonB.Instance: TSingleton;
begin
if FInstance = nil then
FInstance := TSingletonB.Create;
//---
Result := FInstance;
end;
procedure TSingletonB.Operate;
begin
ShowMessage('SingletonB');
end;
end.
procedure TForm1.Button5Click(Sender: TObject);
begin
TSingleton.Instance.Operate;
TSingleton.Instance.Free;
//---
TSingletonA.Instance.Operate;
TSingletonA.Instance.Free;
//---
TSingletonB.Instance.Operate;
TSingletonB.Instance.Free;
end;
说明:
另一个选择Singleton的子类的方法是将Instance的实现从父类中分离出来并将它放入子类。
代码:
unit uSingleton3;
interface
uses
SysUtils, Dialogs;
type
TSingleton = class
private
class var
FInstance: TSingleton;
public
constructor Create;
destructor Destroy; override;
//---
class function Instance: TSingleton;virtual;
procedure Operate;virtual;
end;
TSingletonA = class(TSingleton)
public
class function Instance: TSingleton; override;
procedure Operate; override;
end;
TSingletonB = class(TSingleton)
public
class function Instance: TSingleton; override;
procedure Operate; override;
end;
implementation
constructor TSingleton.Create;
begin
if FInstance = nil then
FInstance := Self
else
abort;
end;
destructor TSingleton.Destroy;
begin
FInstance := nil;
//---
inherited;
end;
class function TSingleton.Instance: TSingleton;
begin
if FInstance = nil then
FInstance := TSingleton.Create;
//---
Result := FInstance;
end;
procedure TSingleton.Operate;
begin
ShowMessage('Singleton');
end;
class function TSingletonA.Instance: TSingleton;
begin
if FInstance = nil then
FInstance := TSingletonA.Create;
//---
Result := FInstance;
end;
procedure TSingletonA.Operate;
begin
ShowMessage('SingletonA');
end;
class function TSingletonB.Instance: TSingleton;
begin
if FInstance = nil then
FInstance := TSingletonB.Create;
//---
Result := FInstance;
end;
procedure TSingletonB.Operate;
begin
ShowMessage('SingletonB');
end;
end.
procedure TForm1.Button5Click(Sender: TObject);
begin
TSingleton.Instance.Operate;
TSingleton.Instance.Free;
//---
TSingletonA.Instance.Operate;
TSingletonA.Instance.Free;
//---
TSingletonB.Instance.Operate;
TSingletonB.Instance.Free;
end;
相关文章推荐
- 《GOF设计模式》—单件(Singleton)—Delphi源码示例:创建Singleton类的子类(使用环境变量)
- 《GOF设计模式》—单件(Singleton)—Delphi源码示例:创建Singleton类的子类(使用注册表)
- 《GOF设计模式》—工厂方法(Factory Method)—Delphi源码示例:为子类提供挂钩(hook)
- 《GOF设计模式》—单件(Singleton)—Delphi源码示例:单件接口(使用全局变量)
- 《GOF设计模式》—单件(Singleton)—Delphi源码示例:单件接口(使用类字段)
- 《GOF设计模式》—单件(Singleton)—Delphi源码示例:基于单件的迷宫
- 《GOF设计模式》—工厂方法(Factory Method)—Delphi源码示例:基于工厂方法的迷宫
- 《GOF设计模式》—模板方法(TEMPLATE METHOD)—Delphi源码示例:模板方法接口
- 《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:创建正确的Implementor对象(缺省的实现)
- 《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:创建正确的Implementor对象(参数)
- 《GOF设计模式》—模板方法(TEMPLATE METHOD)—Delphi源码示例:文档
- 《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:创建正确的Implementor对象(缺省的实现)
- 《GOF设计模式》—工厂方法(Factory Method)—Delphi源码示例:平行的类层级架构
- 《GOF设计模式》—模板方法(TEMPLATE METHOD)—Delphi源码示例:绘图
- 《GOF设计模式》—工厂方法(Factory Method)—Delphi源码示例:参数化工厂方法
- 《GOF设计模式》—迭代器 (ITERATOR)—Delphi源码示例:内部迭代器(依赖于子类)
- 《GOF设计模式》—工厂方法(Factory Method)—Delphi源码示例:基于工厂方法的迷宫
- 《GOF设计模式》—工厂方法(Factory Method)—Delphi源码示例:延迟初始化对象
- 《GOF设计模式》—原型(Prototype)—Delphi源码示例:基于Assign方法的拷贝
- 《GOF设计模式》—工厂方法(Factory Method)—Delphi源码示例:基于工厂方法的迷宫