Results 1 to 8 of 8

Thread: Semi-singleton in Delphi?

  1. #1

    Semi-singleton in Delphi?

    Hello

    As you might know there is a pattern called singleton, which is as class that can be instanced only once in the application. Every new instance of the class, is a reference to the unique object in memory. That mechanism is implemented in the constructor method.

    Now, I would like to make a pattern to work with resources, that sometimes is a normal class and sometimes is a singleton. For example, if I have a model with certain filepath, when it is loaded for the first time, a new instance should be created. But when it is loaded again, the constructor should return the reference to the object already in memory.

    By the moment I'm using functions to emulate this behavior. But sometimes it's hard to remember when I should use the global function or the constructor to load.

    I think this is probably a work with the internals of OOP's mechanisms of Delphi, which I don't know enough. Any idea on how to do this will be greatly appreciated.

  2. #2

    Semi-singleton in Delphi?

    Look in the library in the Advanced Programming Section. There are two links to tutorials on singleton creation.

  3. #3

  4. #4

    Semi-singleton in Delphi?

    Yes, good articles. I prefer this one, because any class you inherit from the singleton class will also be a singleton class. That way you can create one apple and one orange, instead of being limited to creating one fruit.
    [size=10px]"In science one tries to tell people, in such a way as to be understood by everyone, something that no one ever knew before. But in poetry, it's the exact opposite." -- Paul Dirac[/size]

  5. #5

    Semi-singleton in Delphi?

    Sounds good, I'll check it out too :thumbup:

  6. #6

    Semi-singleton in Delphi?

    I've found another way of making inheritable singleton. I put constructor in protected section and write separate function to create and return instance of singleton. The instance is declared in the implementation section so it is not visible outside the unit. The constructor is protected so none can call it except derived classes. There is more typing since for each sngleton you have to declare a variable and GetSingleton function.
    Here is an example:
    [pascal]
    unit sing; //first singleton

    {$mode objfpc}{$H+}

    interface

    type

    TSingleton = class
    private
    fData : string;
    protected
    constructor Create;
    public
    procedure PrintData; virtual;
    end;

    function GetSingleton : TSingleton;

    implementation

    var
    asing : TSingleton = nil;

    function GetSingleton : TSingleton;
    begin
    if asing = nil then
    asing := TSingleton.Create;
    Result := asing;
    end;

    constructor TSingleton.Create;
    begin
    inherited;
    fData := 'hello world';
    end;

    procedure TSingleton.PrintData;
    begin
    Writeln(fData);
    end;

    end.

    unit sing2; //second singleton

    {$mode objfpc}{$H+}

    interface

    uses
    sing;

    type

    TSingleton2 = class(TSingleton)
    private
    fData2 : string;
    protected
    constructor Create;
    public
    procedure PrintData; override;
    end;

    function GetSingleton2 : TSingleton2;

    implementation

    var
    asing2 : TSingleton2 = nil;

    function GetSingleton2 : TSingleton2;
    begin
    if asing2 = nil then
    asing2 := TSingleton2.Create;
    Result := asing2;
    end;

    constructor TSingleton2.Create;
    begin
    inherited;
    fData2 := 'hello world 2';
    end;

    procedure TSingleton2.PrintData;
    begin
    inherited;
    Writeln(fData2);
    end;

    end.

    program singtest; //test program

    {$mode objfpc}{$H+}

    uses
    sing, sing2;

    var
    c : char;

    begin

    GetSingleton.PrintData;
    GetSingleton2.PrintData;

    ReadLn(c);

    GetSingleton.Free;
    GetSingleton2.Free;

    end.

    [/pascal]

    The if statement can be removed from Get function by moving singleton creation to initialization section of the unit.

    I think this solution is closest to C++ implementation based on the static class member.

    So, what do you think?

  7. #7

    Semi-singleton in Delphi?

    My method does not work . Even though constructor is in protected section the TSingleton object can be created by calling TSingleton.Create.
    It seems that the visibility of the method cannot be changed in derived classes.

  8. #8

    Semi-singleton in Delphi?

    The idea is not using a global function, I already do that

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •