Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive Properties (Delphi)

I'm writing an application that is mapping out a directory structure that needs to hold as much information about each file as possible so the end user can them apply custom filters for manipulating the files and folders within. The Class that also does the file searching provides basic information back to the UI to give the end user an idea of how much data is involved. What I am trying to accomplish is recursion in to the sub objects of the class via properties to get the information that I need so that I only ever have to access the top level class to get requiered the information and not have to worry about the information in any number of sub classes.

type 
  TSomeClass = class(TObject)
  private    
    FContainerForSubObjects: TObjectList<TSomeClass>;
    FSomethingToBeCounted: Integer;
    FHasTheDataChanged: Boolean;
  private
    function GetSomethingToBeCounted: Integer;
    function GetHasTheDataChanged: Boolean;
  public
    property SomethingToBeCounted: Integer read GetSomethingToBeCounted;
    property HasTheDataChanged: Boolean read GetHasTheDataChanged;

To make the class declaration make a little more sense. Look at it like this. The ContainerForSubObjects contains a folder so if you were to look at your hard drive the root class would be C:\ if there was a folder called data there would be a sub object that represented the C:\Data folder and the SomethingToBeCounted would be the number of files in each folder.

Now what I want to do in the GetSomethingToBeCounted function if as follows

  1. See if there are any Sub Objects (Which is easy the part)
  2. if there are Sub Objects, Query their SomethingToBeCounted property which in turn should call the GetSomethingToBeCounted function to return the value of the FSomethingToBeCounted field of the class BUT the function should act according to the condition of the HasTheDataChanged state of the class This is where I'm hoping recursion will kick in
    • if the HasTheDataChanged property is set to false for any contained Sub Objects is up to date then it should return it's value and no more processing should be done
    • if the HasTheDataChanged property is set to true data is not up to date and it should be recalculated and the new recalculated value returned. It should also set the appropriate HasTheDataChanged state so it reduces further reprocessing.

I'm also assuming that this similar type of propagation would have to be done with the HasTheDataChanged property as well so that if a value changes somewhere in the middle of the tree all parent objects would be updated accordingly.

Hopefully those requirements make sense

Now to the meat of the question. First of all. is my thinking correct and that just by accessing the sub objects properties that the correct values will propagate their way up to root object so I don't have to spend countless lines of code searching each sub object when there could be thousands of sub objects. Or is it a case that I'm trying to reinvent the wheel here and I'm just over looking a class that already exists and could be using instead of making my own. and Last but not least. would this be the most efficient way of doing things?

like image 652
Chris Cook Avatar asked Dec 05 '25 10:12

Chris Cook


1 Answers

For something like this, I might go in the other direction. Instead of having the parent object trying to figure out what child objects have changed, I would have a child object notify its parent when a change occurs, which then notifies its parent, and its parent, and so on. Let the information bubble upwards, instead of searching for it downwards. That puts the bulk of the work on the activity that is initiating the change, and makes searches faster since all of the information is pre-cached in the tree without having to hunt for it.

type  
  TSomeClass = class(TObject) 
  private     
    FParent: TSomeClass; 
    FSubObjects: TObjectList<TSomeClass>; 
    FSomethingToBeCounted: Integer; 
    FHasTheDataChanged: Boolean; 
    procedure Changed; 
  protected
    procedure SubObjectChanged(ASubObject: TSomeClass); 
  public 
    constructor Create(AParent: TSomeClass = nil);
    destructor Destroy; override;
    procedure DoSomethingToMakeAChange;
    property SomethingToBeCounted: Integer read FSomethingToBeCounted; 
    property HasTheDataChanged: Boolean read FHasTheDataChanged; 
  end;

constructor TSomeObject.Create(AParent: TSomeClass = nil);
begin
  inherited Create;
  FParent := AParent;
  FSubObjects := TObjectList<TSomeClass>.Create;
  //...
end;

destructor TSomeClass.Destroy;
begin
  //...
  FSubObjects.Free;
  inherited Destroy;
end;

procedure TSomeObject.DoSomethingToMakeAChange;
begin
  // update FSomethingToBeCounted as needed
  Changed;
end;

procedure TSomeClass.SubObjectChanged(ASubObject: TSomeClass); 
begin
  // update FSomethingToBeCounted as needed, based on which child was changed
  Changed;
end;

procedure TSomeClass.Changed; 
begin
  FHasTheDataChanged := True; 
  if FParent <> nil then
    FParent.SubObjectChanged(Self); 
end;
like image 108
Remy Lebeau Avatar answered Dec 08 '25 00:12

Remy Lebeau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!