Текущий архив: 2009.01.25;
Скачать: CL | DM;
Вниз
Вопрос про интерфейсы Найти похожие ветки
← →
Дмитрий С (2008-11-30 03:24) [0]Какая реализация интерфейса IChild в следующем коде правильная?
unit SomeUnit;
interface
type
IChild = interface;
IParent = interface
function NewChild_First: IChild;
function NewChild_Second: IChild;
end;
IChild = interface
end;
TParent = class(TInterfacedObject, IParent)
function NewChild_First: IChild;
function NewChild_Second: IChild;
end;
TChild_First = class(TInterfacedObject, IChild)
private
FParent: TParent;
public
constructor Create(AParent: TParent);
end;
TChild_Second = class(TInterfacedObject, IChild)
private
FParent: TParent;
public
constructor Create(AParent: TParent);
destructor Destroy; override;
end;
implementation
{ TChild_First }
constructor TChild_First.Create(AParent: TParent);
begin
FParent := AParent;
end;
{ TChild_Second }
constructor TChild_Second.Create(AParent: TParent);
begin
FParent := AParent;
FParent._AddRef;
end;
destructor TChild_Second.Destroy;
begin
FParent._Release;
inherited;
end;
{ TParent }
function TParent.NewChild_First: IChild;
begin
Result := TChild_First.Create(Self);
end;
function TParent.NewChild_Second: IChild;
begin
Result := TChild_Second.Create(Self);
end;
end.
← →
jack128_ (2008-11-30 13:47) [1]смотря что те нужно? если те нужно, чтобы парент не уничтожался, если жив Child, то наследуй Child от TContainedObject, этот класс как раз для таких случаев и создан...
← →
jack128_ (2008-11-30 13:54) [2]Один момент: если TChild наследуется от TContainedObject, то отвественность за его(чайлда) уничтожение лежит на родителе. Я так предполагаю, что те именно это и нуно.. Если же тя устраивает, что ребёнок будет уничтожаться, когда обнулится его счетчик ссылок, то мона и TChild_Second использовать..
Страницы: 1 вся ветка
Текущий архив: 2009.01.25;
Скачать: CL | DM;
Память: 0.47 MB
Время: 0.015 c