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

Visitor - Behavioral Design Patterns - MetaTrader 5 Library | MT5 EA Download - MetaTrader 5 Resources

author EAcpu | 5 reads | 0 comments |
//+------------------------------------------------------------------+
//| visitor.mq5 |
//| 2019-2020, Dimitri Pecherica|
//| 792112@gmail.com |
//+------------------------------------------------------------------+
//
// Visitor - behavioral design pattern
//
// From: Design Patterns: Elements of Reusable Object-Oriented Software
// Author: erich gamma, richard helm, ralph johnson, john vlissides
// Published in 1994
//
// Intent
//
// Represents the operation performed on the elements of the object
//structure. Visitors let you define new actions without changes
//The class of the element it operates on.
//
// Applicability
//
// Use visitor pattern in the following situations
// An object structure contains many objects of different types
//interface and you want to perform operations on these objects
//Depends on their concrete class.
// Many different and unrelated operations need to be performed
// objects in an object structure and you want to avoid "polluting" them
//Class with these operations. Visitors allow you to retain related actions
// Combine them together by defining them in a class. When the object structure is
//Shared by many applications, use visitors to put actions into these applications
//Applications that require them.
// The class that defines the object structure rarely changes, but you
//I often want to define new operations on the structure. Change
//The object structure class needs to redefine the interfaces of all visitors.
//This can be expensive. If the object structure class changes
//Generally, then it's better to define operations in these classes.
//
// structure
//
//|Client|---------->| Guest |
// | |----------------------------------------|
// | |Access ConcreteElementA(ConcreteElementA)|
// | |Access ConcreteElementB(ConcreteElementB)|
// | ^
// | |
// | +----------------+----------------+
// | | |
// | | Specific Visitor 1 | | Specific Visitor 2 |
// | |--------------------------- | |--------------------------|
// | |VisitConcreteElementA( | |VisitConcreteElementA( |
// | |Specific element A) | |Specific element A) |
// | |VisitConcreteElementB( | |VisitConcreteElementB( |
// | |Specific element B) | |Specific element B) |
// |
// |
// +--->|ObjectStructure|--->*| Element |
// |---------------|
// |accept(guest)|
// ^
// |
// +--------------------------------+-----------------+
// | |
// | Specific element A | | Specific element B |
// |------------------------------------------------| |--------------------------------|
// |accept(guest) | |accept(guest) |
// | v.VisitConcreteElementA(this)| | v.VisitConcreteElementB(this)|
// |Operation A() | |Operation B() |
//
//
// participants
lass="comment">// // Visitors // Declare an access operation for each concrete element class // in the object structure. The name and signature of the operation identify // the class that sends the access request to the visitor. This lets //visitor determine the concrete class of the element being visited. //Visitors can then access the element directly through its specific interface. // Specific visitor // Implement each operation declared by the visitor. Each operation // is implemented as a correspondingly defined algorithm fragment // class of object in the structure. Concrete visitors provide context // for the algorithm and store its local state. This state often accumulates // the results during traversal of the structure. // Element // Defines an accept operation that takes the visitor as a parameter. // Specific elements // Implement operations that accept visitors as parameters. // Object structure // Its elements can be enumerated. // A high-level interface can be provided to allow visitors to access // its elements. // Can be a complex or a collection, such as a list or set. // // Cooperation // // Clients using the visitor pattern must create a specific visitor // object and then traverse the object structure to access each element // with the visitor. // When an element is accessed, the corresponding visitor operation // will be called to its class. The element takes itself as a parameter to this operation // and gives the visitor access to its state, if necessary. // The following interaction diagram illustrates the collaboration // between the object structure, the visitor, and the two elements: // // anObject aConcreteElementA aConcreteElementB aConcreteVisitor // Structure | | |<------------------ | -------------------------- | | // | | Accept ( Guest ) | | // + ------------------------------------------------------------------ + #Include #include #include #include #include #include #include blank start () { ObjectStructure structure; struct.add( new concrete element A()); struct.add( new concrete element B()); struct.accept( new concrete visitor1()); struct.accept( new concrete visitor2()); } // Output // // Add element 2097152 to the object structure// Add element 3145728 to the object structure // // The object structure has accepted a new visitor 4194304 // The object structure has 2 elements // The object structure is requesting each element to accept a visitor // Element a has accepted a visitor // Element performs an operation a // Element b has accepted a visitor // Element performs an operation b // // The object structure has accepted a new visitor 5242880 // The object structure has 2 elements // The object structure is requesting each element to accept a visitor // element a has accepted a visitor // element performs operation a // element b has accepted visitor // element performs operation b // // // result // // Some of the advantages and disadvantages of the visitor pattern are // as follows: // Visitors make it easy to add new operations // Visitors collect related operations and separate irrelevant operations // Adding new concrete element classes is difficult // Cross-class hierarchy access // Accumulated state // Breaking encapsulation // // Execution // // Each object structure has an associated visitor class. This // abstract visitor class declares an operation that accesses concrete elements // for each class that defines a concrete element of the object structure. Each access // operation to the visitor declares its parameters as specific concrete parameters // elements, allowing the visitor to access specific interface // direct elements. Concrete visitor classes override each access operation // implementing visitor-specific behavior for the corresponding concrete // element class. // Here are two more implementation issues that arise when you apply // the visitor pattern: // Double dispatch // Who is responsible for traversing the object structure? // // Related Patterns // // Composite: Visitors can be used to apply operations on objects // structures defined by the Composite pattern. // Interpreters: Visitors can request an interpreter. // //+------------------------------------------------------------------+



Attachment download

📎 visitor.mq5 (9.23 KB)

📎 element.mqh (1.32 KB)

📎 concreteelementa.mqh (1.29 KB)

📎 concreteelementb.mqh (1.3 KB)

📎 visitor.mqh (0.6 KB)

📎 concretevisitor1.mqh (1.26 KB)

📎 concretevisitor2.mqh (1.25 KB)

📎 objectstructure.mqh (2.64 KB)

Source: MQL5 #32433

Verification code Refresh