Observer (Push) - Behavioral Design Patterns - MetaTrader 5 Library | MT5 EA Download - MetaTrader 5 Resources
//+------------------------------------------------------------------+ //| 201111_091814.mq5 | //| 2019-2020, Dimitri Pecherica| //| 792112@gmail.com | //+------------------------------------------------------------------+ // // Observer (push) - 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 // // Define one-to-many dependencies between objects so that when an object //Change the state and all its dependencies will be notified and updated automatically // // Applicability // // When an abstraction has two aspects, one dependent on the other. //Encapsulating these aspects in separate objects allows you to change and reuse //They are independent // When changes to one object require changes to other objects and you don't //Know how many objects need to be changed // When an object should be able to notify other objects without //Assumptions about who these objects are. In other words, you don't want //These objects are tightly coupled // // structure // // Observer // | Topic |--------------------->*|Observer| // |----------------------| |--------| // |Attach(Observer) | |Update()| // | detach(observer) | ^ // |Notify() | | // |All o in observer| | // | o.Update() | | // ^ | // | | Concrete observer | // | Topic |--------------------| // |Specific topic |<----------------|Update() | // |---------------------| |Observer status = | // |GetState() | | Topic.GetState()| // |Return subject.State()| |--------------------| // |SetState() | |observer_state | // |------------------------| // |subject_state | // // participants // // theme // Understand its observers. Any number of observer objects can observe //a topic // Provides an interface for attaching and detaching observer objects // Observer // Define the update interface of the object that should be notified //Theme changes // specific topic //Storage the state that the specific observer object is interested in //Send notification to observer when state changes // specific observer // Maintain a reference to the specific theme object // Store state that should be consistent with the theme // Implement the observer update interface to keep its state consistent //with the theme // // cooperate // // Whenever a change occurs, the concrete subject notifies its observers //This may make its observer's state inconsistent with its own state. // After learning that the specific topic has changed, the specific //The observer object can query the subject for information. concrete observer //Use this information to coordinate its state with the body's state. // // aConcrete aConcrete another // themeObserver ConcreteObserver // | | | // | Set status () | | // | |< -------------------------- | | | // | |Notification() | | | // | |---------------------+ | | // | | | | | -------------------- | -------------->| | // | | | GetStatus()| | // | | Notifications are // not always called by the subject. It can be called by an observer or // implemented by another object entirely. // #include201111 _084634.mqh> // push observer #include 201111 _084512.mqh> //Push topic #include 201111 _091110.mqh> //Push specific topics #include 201111 _090802.mqh> //Push specific observers //+------------------------------------------------------------------+ //|Client | //+------------------------------------------------------------------+ Blank On startup ( blank ) { Concrete topic; topic.Attachment( new concrete observer(topic)); topic.Attachment( new concrete observer(topic)); topic.status( "new push status" ); topic.Notify(); } // // Output // // Topic status is set to: new push status // Push status to observer 2097152... // Observer 2097152 status is updated to: new push status // Push status to observer 3145728... // Observer 3145728 status is updated to: new push status // // Result // // The Observer pattern allows you to change topics and observers independently. //You can reuse topics without reusing their observers, and vice versa. //It allows you to add observers without modifying the subject or other observers. // Further advantages and disadvantages of the Observer pattern include // the following: // Abstract coupling between subjects and observers // Support for broadcast communication // Unexpected updates // // Execution // // Mapping subjects to observers // Observing multiple objects // Who triggers updates? // Dangling references to deleted topics // Ensure topic state is self-consistent before notifying // Avoid observer-specific update protocols: push and pull models // Explicitly specify modifications of interest // Encapsulate complex update semantics // Combine principal and observer classes // // Related patterns // // Mediators // Singletons // +--------------------------------------------------------------------------------+
Attachment download
📎 201111_091814.mq5 (6.6 KB)
📎 201111_084634.mqh (0.7 KB)
📎 201111_084512.mqh (3.91 KB)
📎 201111_091110.mqh (0.73 KB)
📎 201111_090802.mqh (1.61 KB)
Source: MQL5 #31796
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •