State design pattern in MQL4?

Back to topics list To post a new topic, please log in or register
avatar
53
amir_avatar 2015.07.05 22:59
 

Hello,

The OO state design pattern takes a context class which has a reference to a state base class, which is subclassed by the state classes that implement the state behaviour of the context class.
At the same time the state subclasses have a refference to the context class which they get as parameter to their methods in order to call the SetState method of the context class.

Does this mean that this pattern can't be implemented in MQL4 (because of the 2 objects that have references one for another)?

 

Here's a brief on the state design pattern:
http://www.tutorialspoint.com/design_pattern/state_pattern.htm 

avatar
313
JD4 2015.07.05 23:36 #
 
MQL4 (recently) was reworked to be more like MQL5.  I do not know if it had the OO properties added to it that MQL5 supposedly has from being similar to C++.  Me personally, from what I remember of my Java course, you want to keep interdependency between objects down to a minimum, unless they are in a parent/child relationship, and even then, you only want the object itself to determine what methods are public, so it has ultimate control over it's own state.  As far as changing the behavior based on it's state, I do not remember if that is supposed to be happening in a properly built (follows the OO implementation) object, or if it does, it is very controlled by the object that is changing state.  I would think that minor changes would be allowable.  Like the simple example of a car, if someone steps on the gas pedal, the car goes faster.
avatar
53
amir_avatar 2015.07.05 23:44 #
 

I think I was misunderstood. 

Maybe this C# example will make things more clear.

 

http://www.dofactory.com/net/state-design-pattern 

avatar
313
JD4 2015.07.05 23:52 #
 
I understood your first example. As I said, I took Java.  My problem sometimes is in clearly explaining what my mind is seeing and making my hands output it properly.  My point was I am not sure about what of the OO capabilities that were put in MQL5 (because of it being based on C++) are possible, if any, in MQL4.
avatar
53
amir_avatar 2015.07.05 23:56 #
 
I think they are the same in that manner, MQL4 and MQL5. I tried both and seems that you cant make 2 objects have reference to each other.
In terms of OO capabilities they are almost the same thing.
avatar
313
JD4 2015.07.06 00:34 #
 
Oh, I think I get it now.  One object can call another object's methods, and possibly have that called object change it's state, and you can have more than one reference refer to a single object.  But you can't have a single reference refer to more than one object.  Again, hopefully my fingers are explaining correctly what my mind is thinking here, but does that look closer to what you were asking?
avatar
484
ydrol 2015.07.06 00:57 #
 

@OP Do you mean forward references to a sub class ?

class Context;

class Base {

    void doSomething(Context *refContext ) {
        refContext.setState();
    }

};

class Context : Base {

    Base *refBase;
    public:
    void setState() {
    }

};

avatar
53
amir_avatar 2015.07.06 01:38 #
 

ydrol, I don't know of forward reference in MQL, maybe it can be used (BTW, there's MQL doc on this?).

What I mean is suppose you have an object, Context - which has many states. Lets say some of them are Open,Close,etc... And you want to manage the transitions between the states.
So there is a ready made OO solution for this kind of problem, called State pattern which says the following: 

class Context {
   State           *m_state;            // keeps the current object state - base class of state
   StateOpen *m_state_open;   // subclass of state - a specific state
   StateClose *m_state_close;  // subclass of state - a specific state
   void Context() {
        m_state=new StateOpen(); // initial state is open
        m_state_open=new StateOpen();  // used to transition Context to this state from specific Close Subclass
        m_state_close=new StateClose(); // used to transition Context to this state from specific Open Subclass
   }
   SetState (State *state){       // used to transition the state of the object from within each state subclass
     m_state=state;
   } 
   void Close(){
      m_state.Close(this);       // pass context to the specific state object in order for it to change state to Close.
   }
   void Open(){
      m_state.Open(this);       // pass context to the specific state object in order for it to change state to Open
   }

} 

Class State{  // state of object - base class - abstract class
   virtual void Close(Context *context);
   virtual void Open(Context *context);
}

Class StateOpen : State {   // class to deal operations of Context when Context is in Open mode
  StateOpen() {Print("Object in open mode created");}
  void Close(Context *context){
     Print("moving Context to Close state");
     context.SetState(context.GetCloseState());  // GetClosedState is a getter of m_closed_state in Context
  }
  void Open(Context *context){
    Print("context is already open, cant open");
  }
}

Class StateClose : State {   // class to deal operations of Context when Context is in Close mode
  StateClose() {Print("Object is in Close mode now");}
  void Open(Context *context){
     Print("moving Context to Open state");
     context.SetState(context.GetOpenState());  // GetOpenState is a getter of m_open_state in Context
  }
  void Close(Context *context){
    Print("context is already closed, cant close");
  }
}
avatar
53
amir_avatar 2015.07.06 01:40 #
 

This model can add as many states as you want though, but I cant make it in MQL because StateOpen has reference to Context and Context has reference to StateOpen (and StateClose as well).

as you see, each state of Context is dealt in a different subclass of State. And each state subclass deal differently with the same methods, depending on the state.
When a state subclass needs to change the state of object, it simply uses the Context object to set the new state - like in StateOpen subclass for Close() method - it changes the state to Close.
avatar
53
amir_avatar 2015.07.07 13:13 #
 
Ydrol, it worked with forward reference, thx
Back to topics list  

To add comments, please log in or register