Two classes with two circular references

◇◆丶佛笑我妖孽 提交于 2019-12-02 05:09:35

问题


I have two different classes made in two different units, how do i will creat circular references? in Delphi(the classes are in different units)

unit1:

Uses unit2;

type Ta = class(tobject)
   public
       b:Tb;
end;

Unit2:

type Tb = class(tobject)
   public
       a:Ta;
end;

回答1:


It is indeed possible to circumvent the circular unit reference, using class helpers.

By adding a common UnitClassHelper including class helpers for both Ta and Tb it could be solved like this :

unit Unit1;

interface

type Ta = class(tobject)
   public
       b : TObject;
end;

implementation

uses UnitClassHelper;

-

unit Unit2;

interface

type Tb = class(tobject)
   public
       a : TObject;
end;

implementation

uses UnitClassHelper;

-

unit UnitClassHelper;

interface

uses Unit1,Unit2;

Type TaHelper = Class Helper for Ta
   private
     function GetTb : Tb;
     procedure SetTb( obj : Tb);
   public
     property B : Tb read GetTb write SetTb;
 End;

Type TbHelper = Class Helper for Tb
       private
         function GetTa : Ta;
         procedure SetTa( obj : Ta);
       public
         property A : Ta read GetTa write SetTa;
     End;

implementation

function TaHelper.GetTb: Tb;
begin
  Result:= Self.B;
end;

procedure TaHelper.SetTb(obj: Tb);
begin
  Self.B:= obj;
end;

function TbHelper.GetTa: Ta;
begin
  Result:= Self.A;
end;

procedure TbHelper.SetTa(obj: Ta);
begin
  Self.A:= obj;
end;

end.

-

program Test;

uses
  Unit1 in 'Unit1.pas',
  Unit2 in 'Unit2.pas',
  UnitClassHelper in 'UnitClassHelper.pas';

var
  AObj : Ta;
  BObj : Tb;

begin
  AObj:= Ta.Create;
  BObj:= Tb.Create;
  try
    AObj.B:= BObj;
    BObj.A:= AOBj;
    // Do something

  finally
    AObj.Free;
    BObj.Free;
  end;
end.

See also this excellent summary of class helpers : solving-circular-unit-references-with-class-helpers




回答2:


I assume you mean how do I get rid of them!

Put them both in one file, and given your structure you might as well is one answer.

type tb = class;

type Ta = class(TObject)
   public
       b:Tb;
end;

type Tb = class(TObject)
   public
       a:Ta;
end;

Other ways are situational, for instance can you abstract out a class that can own a Ta or a Tb, or a class could be owned by a Ta or Tb...

However I'd recomend you look at Interfaces....

Okay two different files

Well no 'erm three...

Unit3;
    type tc = class(TObject)
        public c:Tc;
    end;

Unit1;
    type Ta = class(TObject)
       public
           b:Tc;
    end;

Unit2;
    type Tb = class(TObject)
       public
           a:Tc;
    end;

Or

Unit3;
type Ic = interface; end;

Unit1;
    type Ta = class(TObject)
       public
           b:Ic;
    end;

Unit2;    
    type Tb = class(TObject)
       public
           a:Ic;
    end;

Find the common bit, out it in a third unit have the other two use it basically. Aside from anything else it will give you a better design.



来源:https://stackoverflow.com/questions/8716202/two-classes-with-two-circular-references

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!