> ## Content Index
> Fetch the complete content index at: https://blogs.remobjects.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Oxygene by Example - Singleton
- URL: https://blogs.remobjects.com/2011/12/16/oxygene-by-example-singleton/
- Published: 2011-12-16T21:15:34.000Z
- Updated: 2018-08-22T06:17:43.000Z
- Author: Carlo Kok
- Tags: #Import 2026-08-17 20:17

This is the first item in my series “Oxygene by Example”, this time about singletons.

Singleton classes are used when there’s only ever one instance of a class needed. They differ from [Static Classes](https://blogs.remobjects.com/en/Classes#Static%5FClasses) in that they have an actual instance, can implement an interface and you can use them as parameters to a method. There are different ways of writing singletons, but the easiest way is by using a readonly class variable:

| type IObjectCache \=interfacemethod GetOrCreate(aName: string; aNeedNewValue: Func): T;end; GlobalObjectCache \=class(IObjectCache)private fCache: Dictionary :\=new Dictionary; constructor;empty;// required to be private; so nobody can instantiate itpublicclassproperty Instance: MySingleton :\=new MyObjectCache;readonly; method GetOrCreate(aName: string; aNeedNewValue: Func): T;end; method GlobalObjectCache.GetOrCreate(aName: string; aNeedNewValue: Func): T;beginlocking fCache dobeginvar lValue: Object;if fCache.TryGetValue(aName,out lValue)thenexit T(lValue);result:\= aNeedNewValue(); fCache.Add(aName,result);end;end; |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

Using the singleton is simple, GlobalObjectCache.Instance gives access to all instance members of the class:

| lblSystemName.Text:\= GlobalObjectCache.Instance.GetOrCreate('ComputerName',\-\> Environment.MachineName); |
| ---------------------------------------------------------------------------------------------------------- |

The latest version of this article can be found \[Here\](http://wiki.oxygenelanguage.com/en/Oxygene\_by\_Example:Singleton).