2010-10-20 91 views
0

我想在我的項目中的一些自定義類中包裝TinyXML庫的一部分,因爲我只需要它的某些功能,我不想公開所有內容。類包裝設計問題

我有一個問題,我的XMLDocument::AddNode(...)函數基本上和我的XMLNode類所做的相同。我想知道是否有人可以給我一些關於如何包裝TinyXML庫的設計技巧,這樣我就不會破壞封裝,並且封裝類不會將TinyXML庫暴露給我的代碼的其餘部分。

class XMLNode 
    { 
    public: 
     XMLNode::XMLNode(); 
     XMLNode::XMLNode(const std::string& element); // Creates a TiXMLElement object 
     XMLNode::XMLNode(XMLNode& nodycopy); 
     XMLNode::~XMLNode(); 

     XMLNode GetFirstChild(); 
     XMLNode GetNextSibling(); 

     std::string GetNodeValue(); 
     std::string GetNodeName(); 

     void AddNodeText(const std::string& text); 
     void AddNodeAttribute(); 

    private: 
     std::string value; 
     std::string nodename; 

     TiXmlElement* thisnode; 
    }; 

//These functions do the same as my AddNode does from XMLDocument, it seems rather silly... 
    XMLNode::XMLNode(const std::string& str_element) 
{ 
    thisnode = new TiXmlElement(str_element.c_str()); 
    nodename = str_element; 
} 

void XMLNode::AddNodeText(const std::string& nodetext) 
{ 
    TiXmlText* text = new TiXmlText(nodetext.c_str()); 
    thisnode->LinkEndChild(text); 
    value = nodetext; 
} 


class XMLDocument 
    { 
    public: 
     XMLDocument::XMLDocument(const std::string& documentname); 
     XMLDocument::~XMLDocument(); 

     void SaveToFile(std::string filename); 
     std::string basicXML(std::string rootnode); 

     void AddNode(XMLNode& node); 
     XMLNode GetXPathNode(const std::string& node); 
     void AppendCloneNodeAsChild(XMLNode& nodetoappend); 

     void SetRoot(XMLNode& rootnode); 
    private: 
     TiXmlDocument document; 
     TiXmlElement root; 
     TiXmlElement currentelement; 

    }; 

void XMLDocument::AddNode(XMLNode& node) // This function does over what XMLNode class is actually for... I just want to add the node to the XMLDocument 
{ 
    std::string val = node.GetNodeName(); 
    TiXmlElement* el = new TiXmlElement(val.c_str()); 
    TiXmlText * txt = new TiXmlText(node.GetNodeValue().c_str()); 
    el->LinkEndChild(txt); 
    document.LinkEndChild(el); 
} 

任何人都可以給我一些建議,關於如何正確包裝,只公開我想要的TinyXML的功能?

回答

1

您已經封裝了TinyXml document - 現在您只需將其委託給它,只展示它提供的函數的子集。

與此

void XMLDocument::AddNode(XMLNode& node) 
{ 
    document.AddNode(node); 
} 

這是完全合法的,因爲你不希望暴露充分TiXmlDocument到類的客戶端替換此

void XMLDocument::AddNode(XMLNode& node) // This function does over what XMLNode class is actually for... I just want to add the node to the XMLDocument 
{ 
    std::string val = node.GetNodeName(); 
    TiXmlElement* el = new TiXmlElement(val.c_str()); 
    TiXmlText * txt = new TiXmlText(node.GetNodeValue().c_str()); 
    el->LinkEndChild(txt); 
    document.LinkEndChild(el); 
} 

。想想std::queuestd::stack如何充當底層容器的適配器。

+0

但是我的XMLNode與document.AddNode使用的TiXmlElement不兼容 – 2010-10-20 16:45:23

+0

@Tony - 我看到了,但是由於'XmlNode'封裝了'TiXmlElement',你不能直接調用'document.AddNode(* node .thisnode);'? – 2010-10-20 16:50:24

+0

所以你會暴露TiXmlElement?我想這裏沒有太多的選項,儘管我認爲它暴露了一種包裝的想法... – 2010-10-20 19:51:47

0

有兩種方法可以解決這個問題。

  1. 從的XMLDocument
  2. 派生它擁有的XMLDocument對象和呼叫轉發給它。