Single case one of Delphi version design pattern

Maybe the Delphi dynasty has really fallen. After checking on the Internet for a long time, there are some repetitive, outdated, and incomplete posts. They are like ancient martial arts cheats. You can only get the first or second chapter each time, and there are still May be fragments

Design patterns represent best practices and are usually adopted by experienced object-oriented software developers. Design patterns are solutions to general problems faced by software developers in the software development process. These solutions are summed up by many software developers after a long period of trial and error.

Quote from rookie tutorial

From the above words, it is not difficult to see that the so-called design pattern is a set of relatively high-quality solutions summarized for some common problems, so this is not limited to language, all languages ​​are common

Singleton (Singleton) mode definition: refers to a class has only one instance, and the class can create this instance by itself. For example, only one task manager can be opened in Windows, which can avoid the waste of memory resources caused by opening multiple task manager windows, or errors such as inconsistent display content of each window. Singleton mode has 3 characteristics

  • The singleton class has only one instance object;
  • The singleton object must be created by the singleton class itself;
  • The singleton class provides a global access point to the singleton;

Some people say that singleton is the simplest of all design patterns. I can only hehe. There are 7 or 8 ways to write singleton in Java as far as I know. Delphi is still exploring and learning. In order to facilitate learning, case It is still presented in the form of a console application to minimize the references of libraries and units

Code

The code in this article has passed the test under the DelphiXE10.4 version

unit uCommonUtil;

interface

uses
    System.SysUtils;

type
   { TSingle }
    TSingle = class(TObject)
    private
        FNum: string;
    protected
    public
        constructor Create;
        class function GetInstance(): TSingle;
        class function NewInstance: TObject; override;
        procedure FreeInstance; override;
        property Str: string read FNum write FNum;
    published
    end;

implementation

var
    GlobalSingle: TSingle;
   { TSingle }

constructor TSingle.Create;
begin
    Str := '单例模式测试';
end;

procedure TSingle.FreeInstance;
begin
    inherited;
    GlobalSingle := nil;
end;

class function TSingle.GetInstance: TSingle;
begin
    //判断当前全局变量GlobalSingle不空则创建对象,这里在并发环境下存在安全隐患
    if GlobalSingle = nil then begin
        GlobalSingle := TSingle.create();
    end;
    Result := GlobalSingle;
end;

class function TSingle.NewInstance: TObject;
begin
    if GlobalSingle = nil then
        //重载方法通过父类  NewInstance方法获取对象,强制转换为 TSingle类型
        GlobalSingle := TSingle(inherited NewInstance);
    Result := GlobalSingle;
end;

end.

Test code

program ProjectSingle;

{$APPTYPE CONSOLE}
{$R *.res}

uses
    System.SysUtils,
    System.Types,
    uCommonUtil in 'uCommonUtil.pas';

var
    P1, P2: Pointer;

begin

    try
        var com1 := TSingle.GetInstance();
        p1 := com1;
        P2 := TSingle.GetInstance();
        //通过输出的地址可见是同一个对象
        writeln(IntToStr(Integer(p2)));
        writeln(IntToStr(Integer(p1)));
    except
        on e: Exception do
            writeln(e.Message);
    end;

    readln;

end.

Guess you like

Origin blog.csdn.net/farmer_city/article/details/113102329