Главная страница
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.019 c
15-1227399487
Petr V. Abramov
2008-11-23 03:18
2009.01.25
Звуки для PC-спикера


15-1228425965
TUser
2008-12-05 00:26
2009.01.25
60 лет


1-1207046453
Sergey2
2008-04-01 14:40
2009.01.25
TMainMenu и Hint у контролов в рантайме отображается неправильно


1-1207386579
Da109
2008-04-05 13:09
2009.01.25
HTMLViewer


15-1228401946
utkin295
2008-12-04 17:45
2009.01.25
Создание интерпретатора