//---singleton--------------------------------------------------------
// creational design pattern
// ---intent
// ensure one instance of a class with a global point of access
// ---benefits
// the sole instance of a class may vary
// ---applicability-------------------------------------------------
// one instance of a class
// accessible to clients from a well-known access point
// extensible by subclassing
// extended instance should be good without changing the code
// ---structure
// | singleton |
// |-----------------------|
// |static instance() |
// | return uniqueinstance |
// |singletonoperation() |
// |getsingletondata() |
// |-----------------------|
// |static uniqueinstance |
// |singletondata |
//
#include <SRC\Patterns\Patterns.mqh>
namespace Singleton
{
//---participants-----------------------------------------------------
class Singleton
// instance method
// lets clients access its unique instance
// class operation
// static member function in c++
// own unique instance may be created
{
public:
static Singleton* Instance();
void SingletonOperation();
string GetSingletonData();
protected:
Singleton();
static Singleton* uniqueInstance;
string singletonData;
};
Singleton::Singleton(void) {} //constructor
Singleton* Singleton::uniqueInstance=NULL; //static member
void Singleton::SingletonOperation(void) //operation
{
Print("Setting singleton data");
singletonData="Singleton data";
}
string Singleton::GetSingletonData(void) //get data
{
Print("Reading singleton data");
return singletonData;
}
Singleton* Singleton::Instance(void) //get instance
{
if(!CheckPointer(uniqueInstance))
uniqueInstance=new Singleton;
return uniqueInstance;
}
//---participants-----------------------------------------------------
class Client:public ClientExample
{
public:
string Output();
void Run();
};
string Client::Output() {return __FUNCTION__;}
//---collaborations---------------------------------------------------
void Client::Run() //clients access a singleton solely through instance()
{
Singleton* instance1=Singleton::Instance();
Singleton* instance2=Singleton::Instance();
string compareInstances=(instance1==instance2)?
"Instances are the same objects":
"Instances are different objects";
Print(compareInstances);
//---
instance1.SingletonOperation();
string singletonData=instance2.GetSingletonData();
Print(singletonData);
//---
delete instance1;
}
}
//---output-----------------------------------------------------------
// creational::singleton::client::output
// instances are the same objects
// setting singleton data
// reading singleton data
// singleton data
//---consequences-----------------------------------------------------
// ---controlled access to sole instance
// ---reduced name space
// better than global variables
// ---operations and representation
// can be changed
// may be subclassed
// ---variable number of instances is ok
// ---static methods
// more flexible than class operations
// but non-virtual and non-polymorphic
//---implementation---------------------------------------------------
// ---ensuring a unique instance
// in c++
// static member _instance
// pointer to its unique instance
// static instance()
// lazy initialization
// create and store on first access
// singleton subclass pointer to _instance can be assigned
// constructor is protected
// ---subclassing the singleton
// singleton is determined in instance()
// possible singletons are hard-wired
// instance() implementation
// in subclass, not in parent class
// choice of singletons at link-time is fixed
// registry of singletons
// register themselves by name in constructor
// static singleton is defined in the implementation file
// all possible singletons must be created
// else they won't get registered
// map between string names and singletons
// instance() asks for the singleton by name
// looks up the singleton
// returns if it exists
//---related patterns-------------------------------------------------
// abstract factory, builder, prototype
// can be implemented using the singleton
//+------------------------------------------------------------------+
#30596LibrariesMetaTrader 5👁 3957 views
Singleton - creational design pattern
D
Author
DMITRII PECHERITSAPublished date
Aug 12, 2020
📚Related Articles
← View other articles →#77531Libraries
CSetupLedger: a per-setup win rate ledger for Expert Advisors
Why I wrote it My scalper trades two setups. A bullish order block and the bearish mirror of it. The Strategy Tester gave me one profit factor for both of them. I spent two weeks t...
Ali AkbarView detail →
#77488Libraries
Position sizing from risk, using the broker's own tick value and volume step
Every lot size calculator that assumes one pip is worth ten dollars is wrong on gold, wrong on indices, wrong on JPY crosses and wrong on any account not denominated in USD which i...
Ralph Ivan SimeonView detail →
#77156Libraries
PropFirm Defense
A prop firm account runs under a second rulebook MetaTrader 5 knows nothing about: a maximum loss measured from a fixed starting balance, a daily limit that resets at an hour the b...
Bruno Nunes Myrrha RibeiroView detail →