Главная страница
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.016 c
15-1228385772
Palladin
2008-12-04 13:16
2009.01.25
HTTPS сервер


2-1228831788
savyhinst
2008-12-09 17:09
2009.01.25
Как приделать к exe ресурс, а потом использовать его?


2-1229089075
Андрей Ал.
2008-12-12 16:37
2009.01.25
Фильтр


2-1229087384
webpauk
2008-12-12 16:09
2009.01.25
Скорость


15-1228038652
Slider007
2008-11-30 12:50
2009.01.25
С днем рождения ! 30 ноября 2008 воскресенье