Benefits of singletons
- Last Updated: August 17, 2021
- 1 minute read
- OpenEdge
- Version 12.2
- Documentation
When an instance is created, you have a reference to the instance. You are responsible for the life cycle of the instance. In some cases, you may want to have a global instance that is available to all code running in the ABL Virtual Machine (AVM).
For example, you may want to have a single class instance that provides security for your application that all application code can access. You can implement this by defining a class that has a static constructor and static data members. One of the static data members is used to hold an instance of the class in the AVM. This data member must be public. This single class instance is called a singleton.
A static constructor behaves differently from a regular constructor. You cannot call a static constructor in your code. When an application starts, the static constructors for all singletons are called automatically. You must add code to these static constructors to create an instance of the class and assign it to the static data member. Any code that runs in the AVM has access to this static data member. You do not define parameters for a static constructor.
For more information, see Comparison with procedure-based programming main block.