Thread View: pl.comp.lang.pascal
1 messages
1 total messages
Started by webmajsterek
Thu, 10 Jun 2010 08:23
Singleton w FPC 2.5.1
Author: webmajsterek
Date: Thu, 10 Jun 2010 08:23
Date: Thu, 10 Jun 2010 08:23
108 lines
3690 bytes
3690 bytes
Singleton to taki obiekt który może istnieć tylko w jednej kopi .
TU przykład jak można go tworzyć http://kelpi.com/script/e6eb64 .
Jednak mi taka konstrukcja jest troche mało elegancka -- trzeba było definiować
obiekt oraz jego globalny wskaznik , no i taki wskaźnik można potem zmienić na
inny adres .
Skoro najnowsze wersji FPC daje możliwośc ladniejszego opakowania tego
zagadnienia . Globalny wskaznik jest chowany w deklaracji kalsy .
//----------------------------------------------------------------//
unit UnitSingleton;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
type
TSingleton=class(TObject)
Private
class var FInstance:TSingleton;
Public
procedure Test;
class Function NewInstance: TObject; Override;
Destructor Destroy;override;
class procedure DestroyInstance;
class property Instance:TSingleton Read FInstance Default Nil;
end;
implementation
procedure TSingleton.Test;
begin
Writeln(' Test Singleton');
end;
class Function Tsingleton.NewInstance:TObject;
begin
If Instance = Nil then Begin
FInstance := TSingleton(Inherited NewInstance);
Writeln(' Tworze Singleton');
end Else Writeln(' Singleton istnial - przekazuje wskaznik do obiektu ');
Result:=Instance;
End;
Destructor TSingleton.Destroy;
begin
Writeln('Singleton Destroy');
FInstance:=Nil;
Inherited Destroy;
end;
Class procedure TSingleton.DestroyInstance;
begin
Writeln(' Class TSingleton DestroyInstance ');
If Instance<>Nil then Instance.Free Else
Writeln(' Nie ma obiektu do zniszczenia');
end;
initialization
TSingleton.Create;
// TSingleton.FInstance:=TSingleton($FF);
finalization
TSingleton.DestroyInstance;
Readln;
end.
//-------------------------------------------------------------//
//-------------------------------------------------------------//
program singleton1;
{$mode objfpc}{$H+}
uses UnitSingleton;
{$R *.res}
var Singleton:TSingleton;
begin
// Singleton.FInstance:=TSingleton($FF);
Singleton:=TSingleton.Instance;
Singleton.Test;
Singleton:=TSingleton.Create;
Singleton.Destroy;
Readln;
end.
//----------------------------------------------------------//
i tu widze jedna mała niescisłośc bo mimo ze pole FInstance jest zadeklarowane
jako prywatne to mozna sie do niego odwołać z dowolnego miejsca kodu . np
wystarczy w głownym unicie podać
Singleton.FInstance:=TSingleton($FF);
i kompilator to przepuszcza .
To błąd kompilatora czy wszystkie pola typu "class var' z definicji są
publiczne i nie można zabronić dostępu do nich .
--
Wysłano z serwisu OnetNiusy: http://niusy.onet.pl
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads