Is it possible to implement a singleton pattern in MQL4.

 

I have structures that I need a single instance of. Naturally, it's highly discouraged to create several objects in different classes of these structures. So I came to the conclusion that, in this case, the most reasonable option would be to use thesingleton pattern. Right?

Here's an example of a structure:

struct Symbol_Properties
{
   datetime    gdt_Quote;           // Время поступления последней котировки
   double      gda_Price [2];       // Текущие рыночные цены (0 - Bid, 1- Ask)
   double      gd_Spread;           // Размер спреда в пунктах
   double      gd_Swap;             // Своп
   double      gd_Comission;        // Комиссия
   double      gd_Pt;               // Величина одного пункта
   int         gi_Digits;           // Количество знаков в цене после запятой
   int         gi_StopLevel;        // Минимально-допустимый уровень стоп-лосса/тейк-профита в пунктах
   int         gi_FreezLevel;       // Уровень заморозки ордеров в пунктах
};

How do I turn it intoa singleton? Is there such a possibility in MQL4?

Who is using this pattern in their developments?

 
hoz:

I have structures that I need a single instance of. Naturally, it's highly discouraged to create several objects in different classes of these structures. So I came to the conclusion that, in this case, the most reasonable option would be to use thesingleton pattern. Right?

Here's an example of a structure:

How do I turn it intoa singleton? Is there such a possibility in MQL4?

Who uses this pattern in their developments?


"... Can you at least draw a quadratic quotient...?" - "... No, I can't even imagine. ..."

Any pattern in MQL4 can be implemented.

 
tara:


"... Can you at least picture the quadratic divalent...?" - "... No, I can't even imagine. ..."

Any pattern can be implemented in MQL4.

Alexey, show me an example. I read the articlehttp://cpp-reference.ru/patterns/creational-patterns/singleton/

Actually I haven't understood how to apply it to my structure. That's why I'm asking.

 
hoz:

Alexei, show me an example. I read the articlehttp://cpp-reference.ru/patterns/creational-patterns/singleton/

Didn't really understand how to apply it to my structure. That's why I'm asking.


Victor, I'm not trying to explain how to apply the article I haven't read to your structure, and I don't understand the relationship between the latter and unknown classes.

My point is that any pattern, correctly described verbally, can be implemented in MQL4.

 
announce it once in the inite and forget it.
 
FAQ:
announce it once in the inite and forget it.
And that's the right thing to do! For, Victor doesn't know why a singleton is needed :-)
 
Victor, as usual, is burying himself in the details, going from the complicated to the contrary...
 
hoz:

I have structures that I need a single instance of. Naturally, it's highly discouraged to create several objects in different classes of these structures. So I came to the conclusion that, in this case, the most reasonable option would be to use thesingleton pattern. Right?

Here's an example of a structure:

How do I turn it intoa singleton? Is there such a possibility in MQL4?

Who is using this pattern in their developments?


There was such a theme here, and someone even drew this singleton, if you look around you may find it. But I don't need it here.
 
tara:


Victor, I don't mean how to apply the article I haven't read to your structure, and I don't understand relations of the latter with unknown classes.

My point is that any pattern, correctly described verbally, can be implemented in MQL4.

Alexey, you're not the first programmer... The members-elements of the structure, such as: symbol, point, stop loss, etc.. They are used quite often. And they are needed in many classes. So there is no question about the relationship between classes that use these structures and the corresponding structures as such!

FAQ:
declare it once in inite and forget it.

What does the inite have to do with it? Which inite do you mean? It's not like I'm describing them in the EA itself.

Zhunko:
And rightly so! For, Victor doesn't know why a singleton is needed :-)

Yeah, well... The main thing is thatVadim knows :)))))

I'm aware of that!

FAQ:
Victor, as usual, is burying himself in the details, going from the complicated, while the opposite should be the case...
Hmm... Well, how? Make all the methods in the structure static with the object created immediately after the structure, and inlude this structure in each class and in the Expert Advisor itself?
 
yes
 

I've done it this way:

struct Symbol_Properties
{
   static datetime    gdt_Quote;           // Время поступления последней котировки
   static double      gda_Price [2];       // Текущие рыночные цены (0 - Bid, 1- Ask)
   static double      gd_Spread;           // Размер спреда в пунктах
   static double      gd_Swap;             // Своп
   static double      gd_Comission;        // Комиссия
   static double      gd_Pt;               // Величина одного пункта
   static int         gi_Digits;           // Количество знаков в цене после запятой
   static int         gi_StopLevel;        // Минимально-допустимый уровень стоп-лосса/тейк-профита в пунктах
   static int         gi_FreezLevel;       // Уровень заморозки ордеров в пунктах
};
//---- Создадим объект структуре Symbol_Properties
Symbol_Properties SSym;
datetime   SSym.gdt_Quote = 0;
double     SSym.gda_Price [2] = {0.0, 0.0};
double     SSym.gd_Spread = 0.0;
double     SSym.gd_Swap = 0.0;
double     SSym.gd_Comission = 0.0;
double     SSym.gd_Pt = 0.0;
int        SSym.gi_Digits = 0;
int        SSym.gi_StopLevel = 0;
int        SSym.gi_FreezLevel = 0;

But for some reason there are a lot of errors when compiling. What's wrong?

Reason: