//+------------------------------------------------------------------+
//| 201104_114239.mq5 |
//| 2019-2020, dimitri pecheritsa |
//| 792112@gmail.com |
//+------------------------------------------------------------------+
// from: design patterns: elements of reusable object-oriented software
// by gof: erich gamma, richard helm, ralph johnson, john vlissides
// published in 1994
//+------------------------------------------------------------------+
//| memento - behavioral design pattern |
//+------------------------------------------------------------------+
// without violating encapsulation, capture and externalize an object's
//internal state so that the object can be restored to this state later
//+------------------------------------------------------------------+
//| applicability |
//+------------------------------------------------------------------+
// a snapshot of (some portion of) an object's state must be saved so
//that it can be restored to that state later, and
// a direct interface to obtaining the state would expose implementation
//details and break the object's encapsulation
//+------------------------------------------------------------------+
//| structure |
//+------------------------------------------------------------------+
// memento
// | Originator |- - ->| Memento |<-------o|Caretaker|
// |--------------------------| |----------|
// |SetMemento(Memento m) | |GetState()|
// | state=m.GetState() | |SetState()|
// |CreateMemento() | |----------|
// | return new Memento(state)| |state |
// |--------------------------|
// |state |
//
//+------------------------------------------------------------------+
//| participants |
//+------------------------------------------------------------------+
// memento
// stores internal state of the originator object. the memento may
//store as much or as little of the originator's internal state as necessary
//at its originator's discretion
// protects against access by objects other than the originator.
//mementos have effectively two interfaces. caretaker sees a narrow interface
//to the memento — it can only pass the memento to other objects. originator,
//in contrast, sees a wide interface, one that lets it access all the data
//necessary to restore itself to its previous state. ideally, only the
//originator that produced the memento would be permitted to access the
//memento's internal state
// originator
// creates a memento containing a snapshot of its current internal state
// uses the memento to restore its internal state
// caretaker
// is responsible for the memento's safekeeping
// never operates on or examines the contents of a memento
//+------------------------------------------------------------------+
//| collaborations |
//+------------------------------------------------------------------+
#include <Mqh\201104_114823.mqh> //memento
#include <Mqh\201104_115143.mqh> //originator
#include <Mqh\201104_115621.mqh> //caretaker
// a caretaker requests a memento from an originator, holds it for a
//time, and passes it back to the originator:
//
// aCaretaker anOriginator aMemento
// | | |
// | |CreateMemento() |
// | |--------------------->| |new Memento |
// | | | |- - - - - - - - - - ->| |
// | | | |SetState() |
// | | | |--------------------->| |
// | | |
// / / /
// / / /
// | | |
// | |SetMemento() | |
// | |--------------------->| |GetState() |
// | | | |--------------------->| |
// | | |
//
// sometimes the caretaker won't pass the memento back to the originator,
//because the originator might never need to revert to an earlier state
// mementos are passive. only the originator that created a memento will
//assign or retrieve its state
//---
void OnStart()
{
Originator originator;
Caretaker caretaker;
//---
originator.State("on");
caretaker.memento=originator.CreateMemento();
originator.State("off");
originator.SetMemento(caretaker.memento);
}
//+------------------------------------------------------------------+
//| output |
//+------------------------------------------------------------------+
// changing originator state to on
// creating memento with current state on
// changing originator state to off
// restoring originator state from memento to on
//+------------------------------------------------------------------+
//| consequences |
//+------------------------------------------------------------------+
// preserving encapsulation boundaries
// it simplifies originator
// using mementos might be expensive
// defining narrow and wide interfaces
// hidden costs in caring for mementos
//+------------------------------------------------------------------+
//| implementation |
//+------------------------------------------------------------------+
// language support
// storing incremental changes
//+------------------------------------------------------------------+
//| related patterns |
//+------------------------------------------------------------------+
// command: commands can use mementos to maintain state for undoable operations
// iterator: mementos can be used for iteration as described earlier
//+------------------------------------------------------------------+
#31766LibrariesMetaTrader 5👁 3941 views
Memento - behavioral design pattern
D
Author
DMITRII PECHERITSAPublished date
Nov 4, 2020
📚Related Articles
← View other articles →#77531Libraries
CSetupLedger: a per-setup win rate ledger for Expert Advisors
Why I wrote it My scalper trades two setups. A bullish order block and the bearish mirror of it. The Strategy Tester gave me one profit factor for both of them. I spent two weeks t...
Ali AkbarView detail →
#77488Libraries
Position sizing from risk, using the broker's own tick value and volume step
Every lot size calculator that assumes one pip is worth ten dollars is wrong on gold, wrong on indices, wrong on JPY crosses and wrong on any account not denominated in USD which i...
Ralph Ivan SimeonView detail →
#77156Libraries
PropFirm Defense
A prop firm account runs under a second rulebook MetaTrader 5 knows nothing about: a maximum loss measured from a fixed starting balance, a daily limit that resets at an hour the b...
Bruno Nunes Myrrha RibeiroView detail →