거래 로봇을 무료로 다운로드 하는 법을 시청해보세요
당사를 Facebook에서 찾아주십시오!
당사 팬 페이지에 가입하십시오
스크립트가 흥미로우신가요?
그렇다면 링크 to it -
하셔서 다른 이들이 평가할 수 있도록 해보세요
스크립트가 마음에 드시나요? MetaTrader 5 터미널에서 시도해보십시오
라이브러리

Singleton - creational design pattern - MetaTrader 5용 라이브러리

조회수:
2357
평가:
(13)
게시됨:
2020.08.12 08:43
Pattern.mq5 (0.28 KB) 조회
Patterns.mqh (2.69 KB) 조회
Singleton.mqh (4.95 KB) 조회
이 코드를 기반으로 한 로봇이나 지표가 필요하신가요? 프리랜스로 주문하세요 프리랜스로 이동
//---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
//+------------------------------------------------------------------+
   
    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