Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal


Singleton - creational design pattern - library for MetaTrader 5


votes: 6
2020.08.12 08:43
//   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
class Singleton
//   instance method
//      lets clients access its unique instance
//      class operation
//         static member function in c++
//   own unique instance may be created
   static Singleton* Instance();
   void              SingletonOperation();
   string            GetSingletonData();
   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
      uniqueInstance=new Singleton;
   return uniqueInstance;
class Client:public ClientExample
   string            Output();
   void              Run();
string Client::Output() {return __FUNCTION__;}
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";
   string singletonData=instance2.GetSingletonData();
   delete instance1;
//   creational::singleton::client::output
//   instances are the same objects
//   setting singleton data
//   reading singleton data
//   singleton data
//   ---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
//   ---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
    Periodic Range Breakout (Martingale) Periodic Range Breakout (Martingale)

    Trend Follower Range Breakout System with Martingale.

    Price momentum oscillator Price momentum oscillator

    Price momentum oscillator

    All creational design patterns All creational design patterns

    A collection of classic creational GoF design patterns

    VR Watch List and Linker Lite MT5 VR Watch List and Linker Lite MT5

    Synchronous change of a trading instrument in all charts