Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 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.017 c
1-1207311044
abhtr
2008-04-04 16:10
2009.01.25
OpenDialog.InitialDir ? :-о


1-1207561428
Yuri Btr
2008-04-07 13:43
2009.01.25
Отключить автопрокрутку в окне редактора Delphi


2-1229278700
Впервые
2008-12-14 21:18
2009.01.25
Странная ошибка после сортировки


1-1206543130
voe
2008-03-26 17:52
2009.01.25
Описание Ссылки в Webbrowser


2-1229007872
9899100
2008-12-11 18:04
2009.01.25
ClipRect в TCanvas