kompozytowy wzorzec projektowy wspomniane przez Sampson-chen była właściwa droga dla realizacji zrobiłem dla małego monitora deweloperskim, pozwalając wybrać kilka metod badawczych ze struktury menu.
Mam klasę podstawową "Wpis w menu", z której czerpię podmenu i liście (pozycje menu). Liść właśnie wykonuje coś, a podmenu otwiera inny poziom menu.
Na przykład klasa bazowa mogła podoba podobny do tego (jeśli chcesz używać shared_pointers):
/*************************************************************//*!
* @brief The base class for all menu entry types (sub menus and sub menu entries/items)
******************************************************************/
class MenuEntry : public boost::enable_shared_from_this<MenuEntry>
{
public:
virtual ~MenuEntry(){}
/**************************************************************//*!
* @brief Default implementation to add menu entries; has to be re implemented
* @param[in] newSubMenuEntry - the new menu entry (another sub menu or leaf)
**************************************************************/
virtual void add(boost::shared_ptr<MenuEntry> newSubMenuEntry)=0;
/*****************************************************************//*!
* @brief Default implementation for call to menu entries; always returns false
* @return false - the function has not been re implemented
****************************************************************/
virtual bool call(void)
{
// the member function has not been re-implemented
return false;
}
/*****************************************************************************//*!
* @brief Default implementation, has to be reimplemented
* @return emptyVector - an empty vector
*********************************************************************************/
virtual std::vector<boost::shared_ptr<MenuEntry> > getChildren(void)
{
std::vector<boost::shared_ptr<MenuEntry> > emptyVector;
return emptyVector;
}
/*******************************************************************************//*!
* @brief Gives a pointer to the parent of the actual menu entry
* @return m_parent - pointer to the parent
******************************************************************************/
boost::shared_ptr<MenuEntry> parent(void)
{
return m_parent;
}
/***************************************************************************//*!
* @brief Default implementation for selecting a menu entry
* @param[in] desiredMenuEntry - the desired menu entry which shall be selected
* @return notExisting - a pointer to <b>this</b>
**********************************************************************************/
virtual boost::shared_ptr<MenuEntry> select(boost::shared_ptr<MenuEntry> desiredMenuEntry)=0;
/**************************************************************************//*!
* <B>Criticality: C0 \n\n</B>
* @brief Sets a pointer to the parent of new menu entry
* @param[in] pointerToParent - pointer to the parent
****************************************************************************/
void setParent(boost::shared_ptr<MenuEntry> pointerToParent)
{
m_parent = pointerToParent;
}
/***************************************************************************//*!
* @brief Default implementation for destroying children
*****************************************************************************/
virtual void destroy(void)=0;
protected:
/************************************************************************//*!
* @brief Constructor for a menu entry (sub menu or leaf)
* @param[in] menuEntryName - the name for the sub menu or leaf
*************************************************************************/
MenuEntry(std::string menuEntryName)
{
m_menuEntryName = menuEntryName;
}
};
W select sprawdzasz poprzez wartości zwracanej metoda, jeśli mam liścia, która wykonuje coś, lub podmenu, dla którego muszę zmienić swoje wskaźniki.
Dla wygody można dodać metody, które znajdują wszystkie pozycje menu podrzędnego w podmenu do wyświetlania, lub metody, które tworzą linię tytułu z rzeczywistą ścieżką menu lub podobną ... Innym pomysłem byłyby metody skanowanie twojego drzewa menu w celu uniknięcia podwójnych pozycji menu asf.
Potrzebujesz relacji rekursywnej. –
Użyj drzewa? Każdy węzeł ma etykietę i może mieć zero lub więcej dzieci, generalizowanych do dowolnej liczby zagnieżdżonych menu. Etykieta węzła głównego jest ignorowana, brane są pod uwagę tylko jej dzieci. – Wug
@Seth Rozumiem, że skomentowałeś, ale nie jestem pewien, jak umieścić to w kodzie – dchhetri