Главная страница
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.015 c
2-1229273738
Morpheus
2008-12-14 19:55
2009.01.25
простой вопрос по RTTI


11-1196713465
creer
2007-12-03 23:24
2009.01.25
Прозрачность метки у KOLCheckBox


15-1227667590
Mobiller
2008-11-26 05:46
2009.01.25
языки для кпк


1-1206717670
Still Swamp
2008-03-28 18:21
2009.01.25
GradientFill


2-1227266715
grav
2008-11-21 14:25
2009.01.25
Как загнать и считать файл MS Word в базу?