0 Views
0 Downloads
0 Favorites
Composite
ÿþ#include <SRC\Patterns\Structural\Composite.mqh> //your path...

/****************************************************************

Example of Composite pattern usage. Client

/****************************************************************/

void OnStart()

  {

   /*root*/

   Component* root=new Composite("ROOT"); //dynamic // = Composite root("ROOT"); //auto

   /*parts*/

   Component* branch1=new Composite("  Branch 1");

   Component* branch2=new Composite("  Branch 2");

   Component* leaf1=new Leaf("    Leaf 1");

   Component* leaf2=new Leaf("    Leaf 2");

   /*build tree*/

   root.Add(NULL);                        //by mistake...

   root.Add(branch1);

   root.Add(branch2);

   branch1.Add(leaf1);

   branch1.Add(leaf2);

   branch2.Add(leaf2);

   branch2.Add(new Leaf("    Leaf 3"));   //add part by reference (non-var)

   root.Operation();                      //check

     {string s; for(int i=0; i<13; i++) {s+="-";} Print(s);}

   /*remove whole branch*/

   root.Remove(branch1);

   root.Operation();                      //check

   /*delete root*/

   delete root;

  }

/****************************************************************

Output:

/**

   ROOT

     Branch 1

       Leaf 1

       Leaf 2

     Branch 2

       Leaf 2

       Leaf 3

   -------------

   ROOT

     Branch 2

       Leaf 2

       Leaf 3

/**/

Comments