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

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

author EAcpu | 5 reads | 0 comments |
//+------------------------------------------------------------------+
//| status.mq5 |
//| 2019-2020, Dimitri Pecherica|
//| 792112@gmail.com |
//+------------------------------------------------------------------+
//
// State-behavior design pattern
//
// From: Design Patterns: Elements of Reusable Object-Oriented Software
// Author: erich gamma, richard helm, ralph johnson, john vlissides
// Published in 1994
//
// Intent
//
// Allows an object to change its behavior when its internal state changes.
//The object will appear changing its class
//
// Applicability
//
// The behavior of an object depends on its state, and it must change its state
//Runtime behavior depends on this state
// Operation has a large, multi-part conditional statement that depends on
//About the state of the object. This state is usually represented by one or more
//Enum constants. Often, multiple operations will contain the same content
//Conditional structure. The state pattern converts each branch of the condition into
//In a separate class. This allows you to view the state of an object as
// By itself, it can change independently of other objects
//
// structure
//
// state
// |Context |o------------------>|Status |
// |---------------| |--------|
// |Request() | |handle()|
// |Status.Handle()| |
// +--------------------+--- - - - -
// || |
// |ConcreteStateA| |Concrete state B|
// |--------------| |--------------|
// |Handle() | |handle() |
//
// participants
//
// context
//Define interfaces that customers are interested in
// Maintain an instance of a specific state subclass that defines
//Current status
// state
//Define an interface to encapsulate related behaviors
//Specific state with context
//Concrete state subclass
//Each subclass implements state-related behavior
//context
//
//  cooperate
//
// The context delegates state-specific requests to the current concrete object
//status object
// The context can pass itself as a parameter to the state object for processing
//ask. This allows the state object to access the context when necessary
// context is the main interface of the client. Customer can configure
//Context with state object. Once a context is configured, its client
//No need to deal with the state object directly
// The context or concrete state subclass can determine which
//State inherits another state and under what circumstances

//+------------------------------------------------------------------+
//|Client |
//+------------------------------------------------------------------+
#include #include #include #include Blank on startup ( blank )
  {
  context context( new concrete state A);
  context.Request();
  context.Request();
  } // output
//
// The context is requesting its status 2097152, it will handle this context// Concrete state a (2097152) is processing the context and changing the context state // The context state is changed to: 3145728 // The context is requesting its state 3145728, which will handle this context // Concrete state b (3145728) is processing the context and changing the context state // The context state is changed to: 4194304 // // Result // // It localizes state-specific behavior and partitions behavior // across states // It makes state transitions explicit // State objects can be shared // // Execution // // Who defines state transitions? // Table-based alternative // ​​Creating and destroying state objects // Using dynamic inheritance // // Related patterns // // The Flyweight pattern explains when and how to share state objects // State objects are usually singletons // + ----------------------------------------------------------------------------------+



Attachment download

📎 state.mq5 (4.36 KB)

📎 state.mqh (0.5 KB)

📎 context.mqh (2.04 KB)

📎 concretestatea.mqh (1.01 KB)

📎 concretestateb.mqh (1.01 KB)

Source: MQL5 #32121

Verification code Refresh