Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

Iterator - Behavioral Design Pattern - MetaTrader 5 Library | MT5 EA Download - MetaTrader 5 Resources

author EAcpu | 3 reads | 0 comments |
//+------------------------------------------------------------------+
//| 201021_113117.mq5 |
//| 2019-2020, Dimitri Pecherica|
//| 792112@gmail.com |
//+------------------------------------------------------------------+
// From: Design Patterns: Elements of Reusable Object-Oriented Software
// Author: gof: erich gamma, richard helm, ralph johnson, john vlissides
// Published in 1994
//+------------------------------------------------------------------+
//|Iterator - behavioral design pattern |
//+------------------------------------------------------------------+
// Provide a way to access the elements of the aggregate object
// Proceed sequentially without exposing its underlying representation
//+------------------------------------------------------------------+
//|Applicability |
//+------------------------------------------------------------------+
// Access the contents of an aggregate object without exposing its contents
// internal representation
//Support multiple traversals of aggregate objects
// Provide a unified interface to traverse different aggregates
// Structure (i.e. supports polymorphic iteration)
//+------------------------------------------------------------------+
//|Structure |
//+------------------------------------------------------------------+
//
// | Aggregation|<------|Client|----->| Iterator|
// |-------------| |-------------|
// |CreateIterator()| |First() |
// ^ |Next() |
// | |IsDone() |
// | |Current Item()|
// | ^
// | |
//| Specific |- - - - - - - - ->|Specific|
//| Aggregation |<--------------------------|Iterator|
//|----------------------------------|
//|CreateIterator() |
//|Return new ConcreteIterator(this)|
//
//+------------------------------------------------------------------+
//|Participant |
//+------------------------------------------------------------------+
// iterator
// Define the interface for accessing and traversing elements
//Total
// Define the interface for creating iterator objects
//Specific iterator
// Implement the iterator interface
// Keep track of the current position in the traversal
//Total
// concrete aggregate
// Implement the iterator creation interface to return
// Instance of the correct concrete iterator
//+------------------------------------------------------------------+
//|Cooperation |
//+------------------------------------------------------------------+
// A concrete iterator keeps track of the current object
// Aggregate and can calculate subsequent objects in the traversal |
//---iterator participant
#include  201021 _104101.mqh> //Iterator
#include201021 _104329.mqh> // # of total included  201021 _104505.mqh> //Specific iterator #include  201021 _104755.mqh> //Concrete Aggregate //--- Blank on startup () { total <string>* aggregate= new concrete aggregate; total+= "element a" ; total+= "element b" ; total+= "element c" ; total+= "element d" ; //--- Iterator <string> * iterator=aggregate.CreateIterator(); for (iterator.First(); !iterator.IsDone(); iterator.Next()) { print (iterator.CurrentItem()); } //--- Delete the total; delete the iterator; } //+--------------------------------------------------------------------------------+ //|Output | //+--------------------------------------------------------------------------------+ // The first iteration of the aggregation // element a // The next iteration of the aggregation // Element b // Next iteration of aggregate // Element c // Next iteration of aggregate // Element d //+------------------------------------------------------------------+ //| Consequences | //+------------------------------------------------------------------+ // It supports changes in aggregate traversal // To change the traversal algorithm just replace the iterator // with a different instance // You can also define iterator subclasses to support new traversals // Iterator simplifies the aggregate interface // There can be multiple traversals pending on an aggregate //+--------------------------------------------------------------------------------+ //|Implementation | //+----------------------------------------------------------------------------------+ // Who controls the iteration? // Who defined the traversal algorithm? // How robust are iterators? // Additional iterator operations // Using polymorphic iterators in C++ // Iterators may have privileged access // Composite iterators // Empty iterators //+------------------------------------------------------------------+



Attachment download

📎 201021_113117.mq5 (5.54 KB)

📎 201021_104101.mqh (0.86 KB)

📎 201021_104329.mqh (0.9 KB)

📎 201021_104505.mqh (3.04 KB)

📎 201021_104755.mqh (2.43 KB)

Source: MQL5 #31579

Verification code Refresh