代码之家  ›  专栏  ›  技术社区  ›  Tony The Lion

类包装设计问题

  •  0
  • Tony The Lion  · 技术社区  · 14 年前

    我想在我的项目中的一些自定义类中包装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的功能吗?

    2 回复  |  直到 14 年前
        1
  •  1
  •   Steve Townsend    14 年前

    你已经把听筒包好了 document -现在,您只需要在需要的地方委托它,只公开它提供的函数的一个子集。

    替换这个

    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);
    }
    

    用这个

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

    这是完全合法的,因为你不想暴露 TiXmlDocument 给你们班的客户。想想怎么 std::queue std::stack 充当底层容器上的适配器。

        2
  •  0
  •   Vinay    14 年前

    有两种方法可以解决这个问题。

    1. 从xmldocument派生
    2. 具有xmldocument对象并将调用转发给它。