Skip to content

Instantly share code, notes, and snippets.

@mdbs99
Created December 4, 2016 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdbs99/a37d69af39bf859c0c9da77ce48f6a3b to your computer and use it in GitHub Desktop.
Save mdbs99/a37d69af39bf859c0c9da77ce48f6a3b to your computer and use it in GitHub Desktop.
aggregated-object
program project;
{$mode objfpc}{$H+}
uses
Classes, SysUtils;
type
IValue = interface
function AsString: string;
end;
TIntegerValue = class(TInterfacedObject, IValue)
private
FValue: Integer;
public
constructor Create(Value: Integer);
destructor Destroy; override;
function AsString: string;
end;
TAggregateValue = class(TAggregatedObject, IValue)
private
FOrigin: IValue;
public
constructor Create(const aController: IUnknown; Origin: IValue);
function AsString: string;
end;
TMyApp = class(TInterfacedObject, IValue)
private
FValue: TAggregateValue;
public
constructor Create(Value: Integer);
destructor Destroy; override;
property Value: TAggregateValue read FValue implements IValue;
end;
{ TIntegerValue }
constructor TIntegerValue.Create(Value: Integer);
begin
inherited Create;
FValue := Value;
WriteLn('TIntegerValue.Create');
end;
destructor TIntegerValue.Destroy;
begin
WriteLn('TIntegerValue.Destroy');
inherited Destroy;
end;
function TIntegerValue.AsString: string;
begin
Result := 'Number is ' + IntToStr(FValue);
end;
{ TAggregateValue }
constructor TAggregateValue.Create(const aController: IUnknown;
Origin: IValue);
begin
inherited Create(aController);
FOrigin := Origin;
end;
function TAggregateValue.AsString: string;
begin
Result := FOrigin.AsString;
end;
{ TMyApp }
constructor TMyApp.Create(Value: Integer);
begin
inherited Create;
FValue := TAggregateValue.Create(
Self,
TIntegerValue.Create(Value)
);
WriteLn('TMyApp.Create');
end;
destructor TMyApp.Destroy;
begin
WriteLn('TMyApp.Destroy');
FValue.Free;
inherited Destroy;
end;
// Program
procedure ExecuteIntegerValue;
var
V: IValue;
begin
WriteLn;
WriteLn('IntegerValue:');
V := TIntegerValue.Create(5);
WriteLn(V.AsString);
end;
procedure ExecuteMyApp;
var
App: TMyApp;
begin
WriteLn;
WriteLn('MyApp:');
App := TMyApp.Create(10);
try
WriteLn(App.Value.AsString);
finally
App.Free;
end;
end;
procedure ExecuteMyAppAsInterface;
var
V: IValue;
begin
WriteLn;
WriteLn('MyAppAsInterface:');
V := TMyApp.Create(20);
WriteLn(V.AsString);
end;
begin
ExecuteIntegerValue;
ExecuteMyApp;
ExecuteMyAppAsInterface;
ReadLn;
end.
@SergioLabres
Copy link

Como ficaria uma implementação de uma nova classe, caso necessário um DoubleValue sendo utilizado na classe MyApp ? Em vista de que no Delphi não é possível a feature "Implements" para a mesma interface mais de uma vez dentro de uma única classe.

@JulianoRamos
Copy link

Bem observado @SergioLabres

Como ficaria uma implementação de uma nova classe, caso necessário um DoubleValue sendo utilizado na classe MyApp ? Em vista de que no Delphi não é possível a feature "Implements" para a mesma interface mais de uma vez dentro de uma única classe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment