Einzelnen Beitrag anzeigen

TiGü

Registriert seit: 6. Apr 2011
Ort: Berlin
3.070 Beiträge
 
Delphi 10.4 Sydney
 
#16

AW: Verwaltung aller Forms eines Programms

  Alt 6. Jan 2021, 12:15
Ich werfe eine erste Idee in den Raum, ausgehend davon, was wir schon wissen.
Geht bestimmt noch besser, eleganter, flexibler und/oder nehme-doch-dieses-Framework-ist-ja-alles-schon-drin.
Ist nur eine Diskussionsgrundlage.

Delphi-Quellcode:
program FormManagerDemo;

uses
  Vcl.Forms,
  FormManager in 'FormManager.pas',
  Unit1 in 'Unit1.pas{MainForm},
  Unit2 in 'Unit2.pas{Form2},
  Unit3 in 'Unit3.pas{Form3};

{$R *.res}

begin
  ReportMemoryLeaksOnShutdown := DebugHook <> 0;
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TMainForm, MainForm);
  GetFormManager.RegisterForm(TForm2);
  GetFormManager.RegisterForm(TForm3);
  Application.Run;
end.
Delphi-Quellcode:
unit FormManager;

interface

uses
  System.SysUtils, System.Classes,
  System.Generics.Collections,
  Vcl.Forms;

type
  TFormManager = class(TObject)
  private
    class constructor Create;
    class destructor Destroy;
  private
    FContainer: TObjectDictionary<TFormClass, TForm>;
  public
    constructor Create;
    destructor Destroy; override;

    function GetForm<T: class, constructor>: T;
    procedure RegisterForm(const AFormClass: TFormClass);
  end;

function GetFormManager: TFormManager;

implementation

var
  _FormManager: TFormManager;

function GetFormManager: TFormManager;
begin
  Result := _FormManager;
end;

constructor TFormManager.Create;
begin
  inherited;
  FContainer := TObjectDictionary<TFormClass, TForm>.Create([doOwnsValues]);
end;

class constructor TFormManager.Create;
begin
  _FormManager := TFormManager.Create;
end;

class destructor TFormManager.Destroy;
begin
  _FormManager.Free;
end;

destructor TFormManager.Destroy;
begin
  FContainer.Free;
  inherited;
end;

function TFormManager.GetForm<T>: T;
var
  LForm: TForm;
begin
  if FContainer.TryGetValue(TFormClass(T), LForm) then
  begin
    if not Assigned(LForm) then
    begin
      LForm := TFormClass(T).Create(nil);
      FContainer.AddOrSetValue(TFormClass(T), LForm);
    end;
    Result := T(LForm);
  end else
  begin
    Result := nil;
  end;
end;

procedure TFormManager.RegisterForm(const AFormClass: TFormClass);
begin
  FContainer.Add(AFormClass, nil);
end;

end.
Delphi-Quellcode:
unit Unit1;

interface

uses
  System.Classes,
  Vcl.Controls, Vcl.Forms, Vcl.StdCtrls,
  Unit2, Unit3;

type
  TMainForm = class(TForm)
    btnForm2: TButton;
    btnForm3: TButton;
    procedure btnForm2Click(Sender: TObject);
    procedure btnForm3Click(Sender: TObject);
  private
  public
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

procedure TMainForm.btnForm2Click(Sender: TObject);
begin
  Form2.ShowModal;
end;

procedure TMainForm.btnForm3Click(Sender: TObject);
begin
  Form3.ShowModal;
end;


end.
Delphi-Quellcode:
unit Unit2;

interface

uses
  System.SysUtils, System.Classes,
  Vcl.Controls, Vcl.Forms, FormManager;

type
  TForm2 = class(TForm)
  end;

  function Form2: TForm2;

implementation

{$R *.dfm}

function Form2: TForm2;
begin
  Result := FormManager.GetFormManager.GetForm<TForm2>;
end;

end.
Delphi-Quellcode:
unit Unit3;

interface

uses
  System.SysUtils, System.Classes,
  Vcl.Controls, Vcl.Forms, FormManager;

type
  TForm3 = class(TForm)
  end;

  function Form3: TForm3;

implementation

{$R *.dfm}

function Form3: TForm3;
begin
  Result := FormManager.GetFormManager.GetForm<TForm3>;
end;

end.
Angehängte Dateien
Dateityp: zip FormManagerDemo_2021-01-06_12-14-25.zip (2,8 KB, 1x aufgerufen)
  Mit Zitat antworten Zitat