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

Design Pattern - Factory Method - MetaTrader 5 Library | MT5 EA Download - MetaTrader 5 Resources

author EAcpu | 4 reads | 0 comments |
//FACTORY METHOD -----------------------------------------------------------------------------------------------
// |Group
// | |Created
// |Intention
// | |Interface for creating objects
// | | | Defer instantiation to subclasses
// | |The subclass decides which class to instantiate
// |Benefits
// | |Aspects of code may vary
// | | | Instantiate a subclass of the object
// | |Refactoring
// | | |Question
// | | | |Create an object by explicitly specifying the class
// | | |Solution
// | | | |Create objects indirectly
// | | | | |Also
// | | | | | |Abstract Factory
// | | | | | | prototype
// |Applicability
// | |A class
// | | | cannot predict the class of object it must create
// | | | wants its subclasses to specify the objects it creates
// | | | Delegate responsibility to one of several auxiliary subclasses
// | | | | Localize the knowledge of which auxiliary subclass is the delegate
// |structure
// | |
// | Creator |
// |------------------------|
// |FactoryMethod() |
// |AnOperation() |
// | ... |
// |Product=FactoryMethod()|
// |Product| | ... |
// ^ ^
// | |
// |Concrete Product|<- - - - - - - -| Concrete Creator |
// |----------------------------|
// |FactoryMethod() |
// |Back to new concrete products|
// | |
#include namespace factory method { // | |
// |Participants-------------------------------------------------------------------------------------------------------------
// | | products
// | | |Interface of the object created by the factory method
// | | |
interface product {
  }; // | | |
// | |Concrete products
// | | | Implement product interface
// | | |
Class specific product: people product { people :
                    specific product();
  };
Concrete Products::Concrete Products ( blank )
  {
print ( "Hash:" ,& this );
  } // | | |
// | |Creator
// | | |Declare factory method
// | | | | Returns an object of type Product
// | | | | default implementation of the factory method
// | | | | | Returns the default ConcreteProduct object
// | | |You can call the factory method to create a Product object
// | | |
class creator { public :
Virtual Product* FactoryMethod()= 0 ;
blank an operation();
                    ~Creator(); protected :
  product* product;
  };
Creator::~Creator( blank )
  {
Delete products;
  } Blank Creator::AnOperation(lass="keyword">Blank) { Delete product; print ( "Creator operation: Create products using a virtual factory." ) ; product = FactoryMethod(); } // | ConcreteCreator :: FactoryMethod( blank ) { print ( "Concrete Creator Factory Method: Return new product" ); return the new concrete product ; } // | | Depends on its subclasses // | | |Defines factory method//| - - - - - - - - - - - - - - - - - - - - - - - // | | Create::Factory Method :: Client ::Output // | |Specific creator factory method : return new product // | | Hash value: 138412032 // | |Creator operation: Create a product using a virtual factory. // | | Concrete Creator Factory Method: Returns new product // | | Hash: 139460608 // | | // | Result - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // | | The code only handles the Product interface // | | | Can be used with any user-defined ConcreteProduct class // | | | but the client may have to create a Creator Classes are subclassed // | | | Create specific ConcreteProduct objects // | | Provide hooks for subclasses // | | | Creating objects within a class is // | | | | More flexible than creating objects directly // | | | Factory methods are // | | | | Not abstract // | | | Provide sensible default implementations // | | When connecting parallel class hierarchies // | | | Classes delegate some of their responsibilities to separate classes // | ; | Instances of manipulator subclasses suitable for them // |Execution - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // | | Author // | | | Abstract // | | | Concrete // | | | Default implementation of factory methods // | | | | Class of objects // | | | | | Parent instantiation // | | | | | Can be changed // | |Parameterized factory methods // | | |New identifiers for new kinds of products // | | |Associate existing identifiers with different products// | |Language-specific variations and issues // | | | Factory methods in C++ // | |Clear the use of factory methods // ----------------------------------------------------------------------------------------------------------------



Attachment download

📎 pattern.mq5 (0.35 KB)

📎 patterns.mqh (2.91 KB)

📎 factorymethod.mqh (7.03 KB)

Source: MQL5 #30371

Verification code Refresh