Các mẫu thiết kế composite được đề cập bởi sampson-chen là cách phù hợp với một thực hiện tôi đã làm cho một màn hình nhà phát triển nhỏ, cho phép bạn chọn một số phương pháp kiểm tra từ một cấu trúc menu.
Tôi có một lớp cơ sở "Menu Entry" mà từ đó tôi lấy được các menu phụ và các lá (các mục trình đơn). Một chiếc lá chỉ thực hiện một cái gì đó, trong khi một menu phụ sẽ mở ra một cấp menu khác.
Ví dụ các lớp cơ sở có thể thích tương tự như (khi bạn muốn sử dụng 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;
}
};
Trong phương pháp chọn tôi kiểm tra thông qua giá trị trả về nếu tôi có một chiếc lá mà thực hiện điều gì đó, hoặc một menu phụ, mà tôi phải thay đổi con trỏ của mình.
Để thuận tiện, bạn có thể thêm các phương pháp tìm thấy tất cả các mục menu con trong menu phụ để hiển thị hoặc các phương pháp xây dựng cho bạn dòng tiêu đề với đường dẫn menu thực hoặc tương tự ... quét cây menu của bạn để tránh các mục nhập kép asf.
Bạn cần có mối quan hệ đệ quy. –
Sử dụng cây? Mỗi nút có một nhãn và có thể có số không nhiều hoặc nhiều hơn, có thể chung với bất kỳ số lượng menu lồng nhau nào. Nhãn của nút gốc bị bỏ qua, chỉ có con của nó được xem xét. – Wug
@Seth Tôi hiểu bạn bình luận, nhưng tôi không chắc chắn làm thế nào để đưa nó vào mã – dchhetri