Delphi like properties in packages

How about you can put Delphi like properties in packages. That way you can build code in packages that mimics the getting and setting of properties like it does in Delphi code.

As you may (or may not) know, the code for creating a property in Delphi (with a getter and a setter) is as follows:

interface

private
  fFoo: boolean;
  function getfoo: boolean;
  procedure setfoo(const AValue: boolean);
public
  property foo: boolean read getfoo write setfoo;

implementation
function bar.getfoo: boolean;
begin
  Result := fFoo;
end;

procedure bar.setfoo(const AValue: boolean);
begin
  fFoo := AValue;
end;

Note that I use a set procedure and a get function to get more control over the property to get and set the value.

Now, convert this approach to a PL/SQL Package:
The header (interface)

CREATE OR REPLACE PACKAGE bar
IS
— Author : PBAREL
— Purpose : demonstrate properties 
  PROCEDURE foo(foo_in IN BOOLEAN ); 
  FUNCTION foo RETURN BOOLEAN;
END bar;

The body (implementation)

CREATE OR REPLACE PACKAGE BODY bar
IS
  fFoo BOOLEAN;
 
PROCEDURE foo(foo_in IN BOOLEAN )
  IS
  BEGIN
    fFoo := foo_in;
  END;

  FUNCTION foo RETURN BOOLEAN
  IS
  BEGIN
    RETURN fFoo;
  END;
END bar;

This way you can address the private package variable as if you are just addressing a public package variable, but you have more control on how it behaves. In the set procedure, for instance, you can take action on how your package should behave after the ‘property’ has been set to a certain value. You can even control what is returned depending on a certain value of the private package variable.

For instance, you can create a boolean property that internally behaves as an integer value. Pass it true in the set procedure and the integer is raised by one. If you retrieve the value using the get function, the value returned will be true if the integer is greater than 0. That way you can set a flag multiple times consecutively and check the value at any time without having to worry about it’s exact value.

Leave a Reply

Your email address will not be published. Required fields are marked *