Article View: pl.comp.lang.pascal
Article #93022Singleton w FPC 2.5.1
From: 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
Technical Details
Message-ID:
<60b5.0000096c.4c10a0ff@newsgate.onet.pl>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.rmf.pl!agh.edu.pl!news.agh.edu.pl!news.onet.pl!newsgate.onet.pl!niusy.onet.plShow Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.rmf.pl!agh.edu.pl!news.agh.edu.pl!news.onet.pl!newsgate.onet.pl!niusy.onet.pl From: webmajsterek@poczta.onet.pl Newsgroups: pl.comp.lang.pascal Subject: Singleton w FPC 2.5.1 Date: Thu, 10 Jun 2010 10:23:27 +0200 Organization: Onet.pl Lines: 108 Sender: news@newsgate.onet.pl Message-ID: <60b5.0000096c.4c10a0ff@newsgate.onet.pl> NNTP-Posting-Host: newsgate.onet.pl Mime-Version: 1.0 X-Trace: newsgate.onet.pl 1276158207 15532 213.180.130.18 (10 Jun 2010 08:23:28 GMT) X-Complaints-To: niusy@onet.pl NNTP-Posting-Date: Thu, 10 Jun 2010 08:23:27 +0000 (UTC) X-Mailer: http://niusy.onet.pl X-Forwarded-For: 79.184.184.40, 10.174.28.53 X-User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit