-
-
Save mdbs99/a37d69af39bf859c0c9da77ce48f6a3b to your computer and use it in GitHub Desktop.
aggregated-object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
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
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.