lundi 24 août 2015

Exchange data between stack of classes

Consider we have a stack of linked classes. Pseudo-code:

template<class CMyLevelHigher, class CMyLevelLower>
class CMyLevel 
{
public:
    CMyLevel(CMyLevelHigher* t_higher, CMyLevelLower* t_lower)
        :   m_higher(t_higher),
            m_lower(t_lower) 
    {
        // ...
    }

    void ProcessInHigher(/* data */) 
    {
        // ...

        if (m_higher != nullptr) {
            m_higher->ProcessInHigher(/* data */);
        }

        // ...
    }

    void ProcessInLower(/* data */) 
    {
        // ...

        if (m_lower != nullptr) {
            m_lower->ProcessInLower(/* data */);
        }

        // ...
    }

private:
    CMyLevelHigher* m_higher{ nullptr };
    CMyLevelLower* m_lower{ nullptr };
}

Purpose of them is to process data and move them to lower or higher direction (class design may be incorrect, its for idea only). For example, for net protocol stack abstraction.

Parameters in /* data */ can be of any type depending on purpose of current level.

Do you know any design patterns or programming best practices to realize this behavior?

Aucun commentaire:

Enregistrer un commentaire