Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which is the best way to control a object-field type life-cycle?

Tags:

oop

delphi

TMyClass = class(TObject)
private
  FMyObject: TObject;
  function GetMyObject: TObject;
public
  property MyObject: TObject read GetMyObject write FMyObject;
end;

function TMyClass.GetMyObject: TObject;
begin
  if FMyObject = nil then
    FMyObject := TObject.Create;

  Result := FMyObject;
end;

Sometimes, "MyObject" is not created internally but externally created and assigned to the parameter. If this object is created externally, I can not free it in this context.

Should I create a TList and Add in all objects that were created internally and destroy everything on the destructor?

How can I control the lifetime of a parameter if it is created internally or not? What you suggest to do? Is there any pattern to do this?

like image 727
Eduardo Maia Avatar asked Jul 04 '11 12:07

Eduardo Maia


People also ask

Which two types of actions can Lifecycle rules apply to an object?

These actions can be either transition actions (which makes the current version of the S3 objects transition between various S3 storage classes) or they could be expiration actions (which defines when an S3 object expires).

What is object life cycle in Java?

We can break the life of an object into three phases: creation and initialization, use, and destruction. Object lifecycle routines allow the creation and destruction of object references; lifecycle methods associated with an object allow you to control what happens when an object is created or destroyed.

What is life cycle management in S3 bucket?

An S3 Lifecycle configuration is an XML file that consists of a set of rules with predefined actions that you want Amazon S3 to perform on objects during their lifetime. You can also configure the lifecycle by using the Amazon S3 console, REST API, AWS SDKs, and the AWS Command Line Interface (AWS CLI).

What are the different types of actions in object lifecycle management in Amazon S3?

S3 Object Lifecycle Management Rules S3 Standard storage class –> other storage class. Any storage class –> S3 Glacier or S3 Glacier Deep Archive storage classes. S3 Intelligent-Tiering storage class –> S3 One Zone-IA storage class. S3 Glacier storage class –> S3 Glacier Deep Archive storage class.


1 Answers

I'd set a flag in the Property Setter

procedure TMyClass.SetMyObject(AObject: TObject);
begin
  if Assigned(MyObject) and FIsMyObject then
    FMyObject.Free;
  FIsMyObject := False;
  FMyObject := AObject;
end;

function TMyClass.GetMyObject: TObject;
begin
  if FMyObject = nil then
  begin
    FMyObject := TObject.Create;
    FIsMyObject := True;
  end;

  Result := FMyObject;
end;

Destructor TMyClass.Destroy;
begin
    if FIsMyObject then
        FMyObject.Free;
end;
like image 77
James Barrass Avatar answered Nov 15 '22 05:11

James Barrass