All Creative Design Patterns - MetaTrader 5 Library | MT5 EA Download - MetaTrader 5 Resources
//+------------------------------------------------------------------+ //| PatternOrganizer.mqh | //| 2019-2020, Dimitri Pecherica| //| 792112@gmail.com | //+------------------------------------------------------------------+ //| 23 Design Patterns | //+------------------------------------------------------------------+ // Design patterns: elements of reusable object-oriented software // gof > Erich Gamma, Richard Helm, Ralph Johnson, John Freesides // Published in 1994 //+------------------------------------------------------------------+ //|Classification | //+------------------------------------------------------------------+ // Purpose > Create > 5 //Abstract factory, builder, factory method, prototype, singleton // Purpose > Structure > 7 // Adapter, bridge, compositer, decorator, facade, flyweight, proxy // Purpose > Behavior > 11 //Chain of responsibility, command, interpreter, iterator, mediator // memo, observer, state, strategy, template method, visitor // Scope > Class > 3 > Factory method, interpreter, template method // scope > objects > 20 > all others //+------------------------------------------------------------------+ //|Relationship | //+------------------------------------------------------------------+ // // +------------->|Memo| |Agent| // | ^ |Adapter| // save state | |bridge| // iteration avoid // | Hysteresis // |Builder| |Iterator|<---+ | | Chain| Chain // ^ ^ | |Command| |Responsibility| // | | | | | // Create enumeration | Composition definition // composite children | using chain // | | | | | // +---------|Composite|<---|--------+----------------+ // | | | | // Add | Definition------------+ Definition // Responsibilities | Traversal | Syntax // Object | | | // | +--------Add------>|Visitor| | // v | Operations | // |Decorator| Share | | // ^ composite +--------|interpreter|---+ // | | | // | v share | //Change skin |flyweight|<-----terminal----+ // with guts ^ symbol |observer| // | Share | | // | Strategy---+---Share | // v | status complex // |Strategy|------+ | |Mediation|<----Dependency // ^ |Status| Management // | // share //Algorithm----------|template methodMethod|----Commonly used---+ // Steps | // | // |Prototype|<--+ | // | v // Configuration factory Implementation---->|Factory method| // Dynamic use // | | // |Abstract factory|---+--------+ // | // Singleton // |singleton|<----------Instance------------|Appearance| // //+--------------------------------------------------------------------------------+ //|Code refactoring | //+--------------------------------------------------------------------------------+ // Create objects <explicit> Indirect creation // Abstract factories, factory methods, prototypes // Dependencies > Specific operations > Avoid > Hardcoded requests // Chain of responsibility, commands // Dependencies > Hardware and software > Limitations // Abstract factories, bridges // Dependencies > Object representation/implementation // Hidden from clients > Prevent cascading changes // Abstract factories, bridges, memos, proxies // Algorithm dependencies > Isolation > Algorithms that may change // Builders, iterators, strategies, template methods, visitors // Tight coupling > Decoupling < Abstract coupling and layering // Abstract factories, bridges, chain of responsibility, // Commands, appearance, mediation, observers // Extended functions < Subclassing // Composition/delegate > Composition behavior // Bridges, chain of responsibility, composition, decorators, // Observers, strategies // Cannot change classes conveniently > Adapters, decorators, visitors // +--------------------------------------------------------------------------------+ //|Creation> Abstract Factory | //+----------------------------------------------------------------+ // Interface > Related/Dependent Object Series // No specific class specified // Variable aspect > Product Object Series // // | AbstractFactory|<----------------------------------------------------------------|Client| // |----------------| | // |CreateProductA()| | // |CreateProductA()| |Abstract ProductA|<--------+ // ^ ^ | // | | // +-----------+----------+ +-----+-----+ | // | | | | | //|ConcreteFactory1|- + |ConcreteFactory2|- + ->|ProductA2| |ProductA1|<- + | //|----------------| | |----------------| | | | //|Create product A()| |Create product A()| | //|Create product B()| | |Create product B()| | | | // |AbstractProductB|<----+--+ // | | ^ | // | // | | +-----+-----+ | // | | // | + ->|Product B2| |Product B1|<- +span class="comment">// | // + - - - - - - - - - - - - - - - - - - - - - - - + // //+----------------------------------------------------------------------------------+ //|Creator > Builder | //+--------------------------------------------------------------------------------+ // For complex objects > Separate construction from presentation // The same construction process can create different representations // Variable aspects > Ways to create composite objects // // Builder // | Director |o--------->| Builder| // |----------------------------------------| |------------| // |Construct() | | BuildPart()| // |For all objects in the structure| ^ // | builder.BuildPart() | | // |ConcreteBuilder | - - > | Products | // |--------------------------| // |BuildPart() | // |GetResult() | // //+--------------------------------------------------------------------------------+ //|Create > Factory Method | //+ -------------------------------------------------------------------------------- + // Interface for creating objects > Defer instantiation to subclasses // Subclasses decide which class to instantiate // Variable aspects > Subclasses of instantiated objects // // | Creator | // |------------------------| // |FactoryMethod() | // |AnOperation() | // | ... | // |Product=FactoryMethod()| // |Product| | ... | // ^ ^ // | | // |Concrete product | < - - - - - | Concrete creator | // p = Prototype.Clone ( ) | ^ // | // +-----------+------------+ // | | // |Specific prototype 1 | |Specific prototype 2 | // |--------------------| |--------------------| // |Clone()| |Clone() | // |Return a copy of self| |Return a copy of self| // //+------------------------------------------------------------------+ //|Create > Singleton | //+------------------------------------------------------------------+ // Ensure > an instance of the class with a global access point // Variable aspect > the only instance of the class // // | Singleton | // |------------------------------------------------| // |StaticInstance() | // |Return a unique instance| // | SingletonOperation () | // |GetSingletonData() | // |---------------------------------------- | // |static uniqueInstance | // |singletonData | // //+------------------------------------------------------------------+ //|Mode Interface | //+------------------------------------------------------------------ + Interface Client Example //Mode Client { String Output( Blank ); // Return Header Blank Run( Blank ); // Execute Mode Client}; // +--------------------------------------------------------------------------------+ //|Mode Interface | //+------------------------------------------------------------------+ Blank Run(Example Client) //Start a schema { Print function ( "---\n%s" , client.Output()); //Print schema title client.Run(); //Perform client collaboration to delete clients; //Export } //+------------------------------------------------------------------+ //|Include > Pattern | //+------------------------------------------------------------------+ Namespace Creative { #Include "Creational\AbstractFactory.mqh" #Include " Creational\Builder.mqh" #Include "Create\FactoryMethod.mqh" #Include "Create\Prototype.mqh" #Include "Create\Singleton.mqh" } //+----------------------------------------------------------------------------------+
//+------------------------------------------------------------------+ //| mode.mq5 | //| 2019-2020, Dimitri Pecherica| //| 792112@gmail.com | //+------------------------------------------------------------------+ #include//+------------------------------------------------------------------+ //|Run | //+------------------------------------------------------------------+ blank on startup () //Start mode clients one by one and check the results { //---create run( new create::AbstractFactory::client); run( new create::builder::client); run( new Create::FactoryMethod::Client); run( new creation::prototype::client); run( new create::singleton::client); } //+------------------------------------------------------------------+ //|Output> Abstract Factory | //+------------------------------------------------------------------+ // Creational::AbstractFactory::Client::Output //The client requests to create factory 1 //The client requests to create a factory client //The client is requesting the factory client to manage factory 1 // Factory 1: 3145728 has been built //The factory client creates and receives abstract factory 3145728 // Factory client request to accept/switch factory // Factory client is accepting new factory 3145728 //The factory client saves the new factory //The factory client is requesting its new factory to create product a // Factory 1 is creating and returning product a1 // Construct product a1 //The factory client is requesting its new factory to create product b // Factory 1 is creating and returning product b1 // Construct product b1 // The client is requesting the factory client to operate //The factory client is running abstract product b // Product b1: 5242880 is interacting with product a: 4194304 //The client requests to create a new factory 2 and asks the factory client to switch factories // Factory 2: 6291456 has been built //The factory client is switching the old factory 3145728 to the new factory 6291456 //The factory client saves the new factory //The factory client is requesting its new factory to create product a // Factory 2 is creating and returning product a2 // Build product a2 //The factory client is requesting its new factory to create product b // Factory 2 is creating and returning product b2 // Build product b2 // The client is requesting the factory client to run again //The factory client is running abstract product b // Product b2: 8388608 is interacting with product a: 7340032 //+------------------------------------------------------------------+ //|Output>Generator //+------------------------------------------------------------------+ // Creational::Builder::Client::Output // The client requests the creation of a new concrete builder // The client requests to create a supervisor and give him the builder // The supervisor created and received builder 10485760 // The customer requests the supervisor to carry out construction // The supervisor has started construction // The supervisor is requesting its builder to build the product part // The builder requests the product to add part a to itself // product adds part a to itself // The builder has made part A and added it to the product // The builder requests the product to add part b to itself // The product adds part b to itself // The builder has made part b and added it to the product // The builder requests the product to add the c part to itself // The product adds the c part to itself // The builder has made part c and added it to the product // Director's builder builds from partsProduct // The client is requesting the builder to return the resulting product // The builder is returning the product // The client is requesting a product that describes itself // The product displays all the parts it consists of // Part a // Part b // Part c //+------------------------------------------------------------------+ //|Output > Factory Method | //+------------------------------------------------------------------+ // Creational::FactoryMethod::Client::Output // Request to create the creator // Requesting the creator to run its factory method to return a product // The creator is running the factory method // The concrete creator is creating and returning a new concrete product // Specific product: Created 15728640 // Requesting the creator to run its action // The creator is running its action // The creator is running the factory method // The specific creator is creating and returning a new specific product // Specific product: Created 16777216 // The creator has saved the product received from the virtual factory method //+------------------------------------------------------------------+ //|Output > Prototype | //+-------------------------------------------------------------------+ // Creational::Prototype::Client::Output // Requesting the creation of a concrete prototype 1 with id 1 // Prototype 18874368 , id: 1 Create // concrete prototype 1: 18874368, id : 1 Request prototype 18874368 to create its clone // Clone specific prototype 1: 18874368, id: 1 // Prototype 19922944, id: 1 Created // Specific prototype 1: 19922944, id: 1 Create // Request to create a specific prototype 2 with id 2 // Prototype 20971520, id: 2 Create // Concrete prototype 2: 20971520, id: 2 Create // Request prototype 20971520 to create its clone // Clone Concrete prototype 2: 20971520, id: 2 // Prototype 22020096, id: 2 Created // Concrete prototype 2 : 22020096 , id: 2 Create //+--------------------------------------------------------------------------------+ // |Output> Singleton | //+------------------------------------------------------------------+ // Creational::Singleton::Client::Output // Request singleton instance 1 // Singleton instance method is running // The unique instance of the singleton is empty // Singleton 24117248 Created // Singleton assigned to the unique instance // The unique instance contains the singleton: 24117248 // Return the unique instance of the singleton: 24117248 // Request singleton instance 2 // Singleton instance method is running // The only instance containing singleton: 24117248 // Return the only instance of singleton 24117248 // Instance 1 and instance 2 are the same object // Request singleton operation on instance 1 // Run singleton operation > Set singleton data // Request singleton data through singleton instance 2 // Read and return singleton data // Singleton data //+------------------------------------------------------------------+
Attachment download
📎 pattern.mq5 (7.04 KB)
📎 patternorganizer.mqh (12.4 KB)
📎 abstractfactory.mqh (37.03 KB)
📎 builder.mqh (14.65 KB)
📎 factorymethod.mqh (9.85 KB)
📎 prototype.mqh (22.08 KB)
📎 singleton.mqh (8.94 KB)
Source: MQL5 #30597
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •