Direkt zum Hauptbereich

Introduction to Design Patterns (1): The Lazy Getter

The Laze Getter pattern


The term "Lazy Getter" refers to the Java vocabular, where a Getter is a method, that returns a feedback value or object. And lazy means, that the effect of this Getter method is somehow delayed. Some more precise, it is delayed until one need it. But what do we need? An object, of course! So we need an instantiation of an object delayed. or maybe we don't need this object at all and this is, why we are using a Lazy Getter.
To save resources, it is inevitable to not create the maybe required object at startup. But of course we need to create it, when it is needed. To reach this, the Lazy Getter method is used as a synonyme for the late created object in the source code. This method does nothing else than looking, if the object is created - if yes, then return it - or not - then create and return it. That's all. A simple basic pattern.

Example for Java:

// somewhere you have declared a public variable of type MessageBox
public MessageBox instance;

...

// You define a Lazy Getter method that cares for its instantiation
private MessageBox getMessageBox(String message)
{
  if (instance == null)
    instance = new MessageBox();
  instance.setMessage( message );
  instance.pack();
}

...

// later you use getMessageBox() instead of a direct variable call
getMessageBox("Hello").show();

// that's it ...

Example for Lazarus / Delphi and language hint

Language hint:
In Lazarus and Delphi there is one thing more to consider. During Java has a Garbage Collector that cares for freeing resources again after their instatntiation, in Lazarus and Delphi the programmer has to care for. And of course a Laze Getter caan't handle disallocation and destruction of objects.
A proper section for freeing resources in window-applications is the BeforeDestruction method of your class. There You should NOT use the way, always shown on the web (fTimer.Free; fTimer := nil;) but the method call to FreeAndNil: FreeAndNil(fTimer);, because this methods has exactly this one purpose and I recommend to use it.

// in the declaration part, You have defined a public variable
public
...
  fTime: TTimer;
...

// somewhere in the code, You implement the Getter:
function getTimer(): TTimer;
begin
  if not assigned(fTimer) then
    fTimer := TTimer.Create();

  result := fTimer;
end;
...

// somwhere else you use the Lazy Getter
...
  getTimer().Enabled = true;
...

// before end of the program you have to free the resources again
FreeAndNil(fTimer);

// that's it ...

Kommentare

Beliebte Posts aus diesem Blog

Pi And More 11 - QMC5883 Magnetic Field Sensor Class

A little aside from the analytical topics of this blog, I also was occupied with a little ubiquitous computing project. It was about machine learning with a magnetic field sensor, the QMC5883. In the Arduino module GY-271, usually the chip HMC5883 is equipped. Unfortunately, in cheap modules from china, another chip is used: the QMC5883. And, as a matter of course, the software library used for the HMC5883 does not work with the QMC version, because the I2C adress and the usage is a little bit different. Another problem to me was, that I  didn't find any proper working source codes for that little magnetic field device, and so I had to debug a source code I found for Arduino at Github  (thanks to dthain ). Unfortunately it didn't work properly at this time, and to change it for the Raspberry Pi into Python. Below you can find the "driver" module for the GY-271 with the QMC5883 chip. Sorry for the bad documentation, but at least it will work on a Raspberry Pi 3. ...

How to use TOracleConnection under Lazarus for Win64

Lazarus Programmers have had no possibility to use TOracleConnection under 64 Bit Windows and Lazarus for years. Even if you tried to use the TOracleConnection with a correctly configured Oracle 11g client, you were not able to connect to the Oracle Database. The error message was always: ORA-12154: TNS:could not resolve the connect identifier specified Today I found a simple workaround to fix this problem. It seems like the OCI.DLL from Oracle Client 11g2 is buggy. All my attempts to find identify the error ended here. I could exclude problems with the TNS systems in Oracle - or the Free Pascal file oracleconnection.pp though the error messages suggestes those problems. After investigating the function calls with Process Monitor (Procmon) I found out, that even the file TNSNAMES.ORA was found and read correctly by the Lazarus Test applictaion. So trouble with files not found or wrong Registry keys could also be eliminated. Finally I installed the Oracle Instant Client 12.1c - aft...

Lazarus IDE and TOracleConnection - A How-To

Free programming IDEs are a great benefit for everybody who's interested in Programming and for little but ambitious companies. One of these free IDEs is the Lazarus IDE . It's a "clone" of the Delphi IDE by Embarcadero (originally by Borland). But actually Lazarus is much more than a clone: Using the Free Pascal-Compiler , it was platform-independent and cross-compiling since it was started. I am using Lazarus very often - especially for building GUIs easily because Java is still Stone-Age when a GUI is required (though there is a couple of GUI-building tools - they all are much less performant than Delphi / Lazarus). In defiance of all benefits of Lazarus there still is one Problem. Not all Components are designed for use on a 64 bit systems. Considering that 64 bit CPUs are common in ordinary PCs since at least 2008, this is very anpleasant. One of the components which will not be available on 64 bit installations is the TOracleConnection of Lazarus' SQLDB ...