On 6/1/07, Alvise Nicoletti <[EMAIL PROTECTED]> wrote:
Does the delphi way work good in fpc mode? i never understand the
difference between the two modes and actually in my project I got a
"{$mode objfpc}" on the code.

I prefer the 'objfpc' mode because it is more strict with syntax
etc...  If you share code between Delphi and Free Pascal, it tends to
be less work using the 'delphi' mode. It minimizes the amount of
$IFDEF statements you will need.

Are there any difference between that implementation and linux? (I'm
going to use it on a multithreading service application)

I'm not quite sure what you mean with this... If it's regarding
creating singletons under Delphi or FPC+Linux, no there is no
difference really in how you create a Singleton. As for the Object
Pascal language and Singletons. I found that the language actually
limits you in how to implement a singleton (a lot more work than some
other language). For example you cannot demote the visibility of the
constructor, but there are other ways to get around this.

Search the FPC-Users mailing list archives and you will see. I had a
discussion about implementing Singletons in Object Pascal before. My
most used method now is what I call the 'lazy mans singleton' as show
below:

-----------------------------------------------
interface section

 TMySingletonClass = class(TObject)
 public
   procedure SomeCoolMethod;
 end;

// Global visible function returning the instance.
// This function will be use throughout my application.
function gMySingleton: TMySingletonClass;

implementation section

var
 uMySingleton: TMySingleton;  // variable local to this unit

function gMySingleton: TMySingletonClass;
begin
 if not Assigned(uMySingleton) then
    uMySingleton := TMySingletonClass.Create;
 result := uMySingleton;  // return the instance
end;

initialization section
  uMySingleton := nil;

finalization section
  uMySingleton.Free;

-----------------------------------------------

So to use the singleton, you just need to include that unit in the
uses clause and then use the global function

eg:
  gMySingleton.SomeCoolMethod;


It's quick and dirty, but does the job perfectly for me.  :-)


--
Graeme Geldenhuys
General error, hit any user to continue.

_________________________________________________________________
    To unsubscribe: mail [EMAIL PROTECTED] with
               "unsubscribe" as the Subject
  archives at http://www.lazarus.freepascal.org/mailarchives

Reply via email to