Einzelnen Beitrag anzeigen

Benutzerbild von KodeZwerg
KodeZwerg

Registriert seit: 1. Feb 2018
3.685 Beiträge
 
Delphi 11 Alexandria
 
#13

AW: Klasse mit Oberfläche verheiraten

  Alt 26. Jul 2022, 19:33
aber woher weis dann die View das es jetzt was neues in der Klasse gibt wenn ich ein Add mache.
Indem man ein Event erstellt was gefeuert wird.
Beispiel mit meinem Schnippsel nun auch mit Delphi getestet ( zumindest das Event )
Delphi-Quellcode:
unit Unit25;

interface

uses
  System.Classes;

type
  THouse = packed record
    FHeight: double;
    FLength: double;
    FName: string;
    FID: integer;
  end;
  THouses = array of THouse;

 TOnAddEvent = procedure(const Sender: TObject; const AHouse: THouse) of object;
 THouseClass = class(TObject)
   strict private
     FHouses: THouses; // interne verwaltung
     FIndex: Integer; // interne verwaltung
     FCount: Integer; // interne verwaltung
     FOnAddEvent: TOnAddEvent; // interne verwaltung
   private
     procedure SetHouse(const AHouse: THouse); // schreibe das index element
     function GetHouse: THouse; // hole das index element hervor
     procedure SetIndex(const AIndex: Integer); // versuche gewünschten index zu setzen
   public
     constructor Create; // um die interne verwaltung zu initialisieren
     procedure Add(const AHeight, ALength: Double; const AName: string; const AID: Integer); overload; // haupt methode zum simplen adden
     procedure Add(const AHouse: THouse); overload; // neben methode die intern die haupt methode aufruft
     procedure Remove; // löscht aktuellen index vom array
   public
     property Houses: THouses read FHouses write FHouses; // direkter zugriff aufs interne array (ich würde es entfernen)
     property House: THouse read GetHouse write SetHouse; // zugriff auf ein element basierend vom index
     property Index: Integer read FIndex write SetIndex; // steuerung für einzel array zugriffe
     property Count: Integer read FCount; // sagt wieviel elemente wir haben
     property OnAddEvent: TOnAddEvent read FOnAddEvent write FOnAddEvent; // feuert ein Event
   end;

implementation

constructor THouseClass.Create;
begin
  inherited Create;
  FOnAddEvent := nil;
  FIndex := -1;
  FCount := 0;
end;

procedure THouseClass.SetHouse(const AHouse: THouse);
begin
  if ((FIndex > -1) and (FIndex < FCount)) then
    FHouses[FIndex] := AHouse;
end;

function THouseClass.GetHouse: THouse;
begin
  if ((FIndex > -1) and (FIndex < FCount)) then
    Result := FHouses[FIndex];
end;

procedure THouseClass.SetIndex(const AIndex: Integer);
begin
  if ((AIndex > -1) and (AIndex < FCount)) then
    FIndex := AIndex;
end;

procedure THouseClass.Add(const AHeight, ALength: Double; const AName: string; const AID: Integer);
var
  i: Integer;
begin
  i := Length(FHouses);
  SetLength(FHouses, i + 1);
  FHouses[i].FHeight := AHeight;
  FHouses[i].FLength := ALength;
  FHouses[i].FName := AName;
  FHouses[i].FID := AID;
  FIndex := i;
  FCount := Length(FHouses);
  if Assigned(FOnAddEvent) then
    FOnAddEvent(Self, FHouses[i]);
end;

procedure THouseClass.Add(const AHouse: THouse);
begin
  Self.Add(AHouse.FHeight, AHouse.FLength, AHouse.FName, AHouse.FID);
end;

procedure THouseClass.Remove;
begin
  if ((FIndex > -1) and (FIndex < FCount)) then
    begin
      Delete(FHouses, FIndex, 1);
      FCount := Length(FHouses);
      if FIndex >= FCount then
        FIndex := Pred(FIndex);
    end;
end;

end.
und ein Demo Projekt:
Delphi-Quellcode:
unit Unit24;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
  unit25;

type
  TForm24 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    FHouses: THouseClass;
    procedure OnAddEvent(const Sender: TObject; const AHouse: THouse);
  public
    { Public declarations }
  end;

var
  Form24: TForm24;

implementation

{$R *.dfm}

procedure TForm24.Button1Click(Sender: TObject);
begin
  FHouses.Add(0.0, 0.0, 'Test', 0);
end;

procedure TForm24.OnAddEvent(const Sender: TObject; const AHouse: THouse);
begin
  Memo1.Lines.Add(AHouse.FName);
end;

procedure TForm24.FormCreate(Sender: TObject);
begin
  FHouses := THouseClass.Create;
  FHouses.OnAddEvent := OnAddEvent;
end;

end.
Gruß vom KodeZwerg
  Mit Zitat antworten Zitat