7.3 SINGLETON PATTERN

In designing a trading platform, we often come across objects that need to be instantiated exactly once. A live market data feed, for instance, needs to exist in the program memory as a single instance. Such single instances are implemented using the singleton pattern. Other singleton objects in a trading platform may include the aggregate of the market data or the trade data, where duplication may prove expensive in terms of computing resources. At a higher level, modern trading systems may adopt a market data server or a trade data server, which may be implemented as singletons on an application server on the network. More generally, the singleton pattern is used in the factory or virtual constructor patterns commonly used to implement software components.

We can use the concept of the singleton pattern in a non-OOP language as well, but we may have to resort to global variables and programming discipline to implement it. In other words, in the non-OOP world, the implementation for a singleton depends on developer discipline. We may be tempted to follow the same trick of declaring an object in the global namespace to implement the singleton idea in OOP as well. This trick will not work well because it does not enforce the condition that there should only be one instance of the object. However, it turns out that the singleton pattern is very easy to implement in an object-oriented language. In C++, we can define and enforce the singleton as in the code below. ...

Get Principles of Quantitative Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.