Access to variables shared between classes in an aggregator class

I have a problem in my hand that requires a very modular design for different algorithms. For example, population-based optimization algorithms such as a genetic algorithm, a particle rotation algorithm, etc. There are several variations of these algorithms, so I planned to make smaller building blocks an abstract class and allow an embedded concrete building block.

For example, let's say we have algo1, which can be divided into the following routines

algo1
loop
{
  sub1 ()
  sub2 ()
  sub3 ()
}

To do this, I can create three interfaces, the implementation of which will be redefined in accordance with their implementation. therefore

//Sub1Class, Sub2Class, Sub3Class are interfaces/abstract classes
class algo1
{
  sub1Class *sub1Obj;
  sub2Class *sub2Obj;
  sub3Class *sub3Obj;
}

// constructor or setter method to set the implementation 
algo1 (Sub1Class *myAlgo1Obj, Sub2Class myAlgo1Obj, Sub3Class myAlgo1Obj)
{
  sub1Obj = myAlgo1Obj;
  sub2Obj = myAlgo2Obj;
  sub3Obj = myAlgo3Obj;
}

doAlgo1
{
  loop
  {
    sub1Obj->algo ();
    sub2Obj->algo ();
    sub3Obj->algo ();
  }
}

, algo, , , getter/setter.

, . , . - friend cpp? , .

, , .

. , , locals re-computation, , , .

+4
1

Inverse of Control .

, :

class ISubroutineState {
public:
    ISubroutineState() = default;

    virtual int getVar1() const = 0;
    virtual void setVar1(int v1) = 0;
};

class ISubroutineState1 : public ISubroutineState {
public:

    virtual std::string getVar2() const = 0;
    virtual void setVar2(std::string& v2) = 0;
};

:

class SubState1 : public ISubroutineState1 {
    int var1;
    std::string var2;

public:
    int getVar1() const {
        return var1;
    }

    std::string getVar2() const {
        return var2;
    }

    void setVar1(int v1) { var1 = v1; }
    void setVar2(std::string& v) { var2 = v; }  
};

IoC ( , - ):

class StateBroker
{
    std::map<const char*, ISubroutineState*> *storage;
public:
    StateBroker();

    template <class S>
    void StateBroker::bind(S* state) {
        storage->emplace(typeid(S).name(), state);
    }

    template <class S>
    S* StateBroker::get() const {
        auto found = storage->find(typeid(S).name());
        if (found == storage->end()) return NULL;
        return (S*)found->second;
    }

    ~StateBroker();
};

StateBroker* stateBroker;

:

 class ISubroutine {
 public:
    virtual void Execute() = 0; 
 };

class Sub1Class : public ISubroutine {
public:
    void Execute()
    {
        if (stateBroker == NULL)
        {
            std::cout << "Sub1 called" << std::endl;
        }
        else {
            ISubroutineState1* ss1 = stateBroker->get<ISubroutineState1>();
            std::cout << "Sub1 with state called" << std::endl;
            ss1->setVar1(1);
            ss1->setVar2(std::string("State is changed by Sub1Class"));
            std::cout << *static_cast<SubState1*>(ss1) << std::endl;
        }
    }
};

class Sub2Class : public ISubroutine {
public:
    void Execute()
    {
        if (stateBroker == NULL)
        {
            std::cout << "Sub2 called" << std::endl;
        }
        else {
            ISubroutineState* ss1 = stateBroker->get<ISubroutineState>();
            std::cout << "Sub2 with state called" << std::endl;
            ss1->setVar1(2);
            std::cout << *static_cast<SubState1*>(ss1) << std::endl;
        }
   }
};

class Sub3Class : public ISubroutine {
public:
    void Execute()
    {
        if (stateBroker == NULL)
        {
            std::cout << "Sub3 called" << std::endl;
        }
        else {
            ISubroutineState1* ss1 = stateBroker->get<ISubroutineState1>();
            std::cout << "Sub3 with state called" << std::endl;
            ss1->setVar1(3);
            ss1->setVar2(std::string("State is changed by Sub3Class"));
            std::cout << *static_cast<SubState1*>(ss1) << std::endl;
        }
    }
};

, "Execute() , . (, ).

:

class Algo {
private:
    Sub1Class* sub1;
    Sub2Class* sub2;
    Sub3Class* sub3;

public:
    Algo(Sub1Class* s1, Sub2Class* s2, Sub3Class* s3) : sub1(s1), sub2(s2), sub3(s3){}

    void Execute()
    {
        sub1->Execute();
        sub2->Execute();
        sub3->Execute();
    }
 };

... ( , statefull , StateBroker )

Sub1Class s1;
Sub2Class s2;
Sub3Class s3;
std::cout << "Stateless algorithm" << std::endl;
Algo mainAlgo(&s1, &s2, &s3);
mainAlgo.Execute();

stateBroker = new StateBroker();
SubState1* state = new SubState1();
stateBroker->bind<ISubroutineState>(state);
stateBroker->bind<ISubroutineState1>(state);

std::cout << "Statefull algorithm" << std::endl;
Algo statefulAlgo(&s1, &s2, &s3);
statefulAlgo.Execute();

, Algo , ..; Sub2Class ISubroutineState1; StateBroker .

, https://github.com/ohnefuenfter/cppRestudy (VS2015)

+1

All Articles