Canonical Delphi Singleton Pattern – Stack Overflow
Posted by jpluimers on 2017/11/29
Though I try to refrain from using the singleton pattern when possible (I prefer dependency injection over accessing “globals” that usually tend up to be not so global after all), sometimes I need to.
In C# there has been a canonical singleton pattern implementation by John Skeet for a very long time at [WayBack] C# in Depth: Implementing the Singleton Pattern.
I just found out there has been similar implementations in Delphi both in this question (which like many useful questions is marked “closed”, oh the joy of StackOverflow): [WayBack] “Delphi Singleton Pattern – Stack Overflow:
- [WayBack] a implementation using locking by [WayBack] David Heffernan that creates at most one instance
- [WayBack] a lock free implementation by [WayBack] Ian Boyd that could create two instances but always returns the first and disposes the second when it detects the first was instantiated
I like both implmentation, but usually take the first as mostly locking has little overhead and sometimes two instances cannot co-exist (though that’s lessened when part of the singleton uses lazy initialisation, for instance from Spring4D).
–jeroen
via: [WayBack] May I please ask for your opinion on the following code:..singleton… – John Kouraklis – Google+
Leave a Reply