【问题标题】:How can I hide my implementation with interfaces and still build upon classes with inheritance?如何使用接口隐藏我的实现并仍然基于具有继承的类?
【发布时间】:2013-09-11 13:51:50
【问题描述】:

我喜欢使用接口来隐藏我的实现细节的想法。我也非常喜欢使用继承来构建我之前创建的类。我怎样才能让这两种好处共存?这是我的问题的一个例子:

object.h

class object {
    protected:
        //stuff that should be available to derived classes
        virtual void    derivedHelper    () = 0;

    public:
        //stuff that should be available to the client
        virtual object* create           () = 0;
        virtual void    mainTask         () = 0;
}

object.cpp

class objectPriv : public object {
    private:
        //implementation specific details and members
    protected:
        void derivedHelper () {
             //some stuff
        }
    public:
        objectPriv() { }

        object* create () {
            return(new objectPriv());
        }
        void mainTask () {
            //more stuff
        }
}

superObject.h

class superObject : public object {             //problem #1
    public:
        //stuff that should be available to the client
        virtual superObject* create  () = 0;
}

superObject.cpp

class superObjectPriv : public superObject {    //problem #2
    private:
        //the magic behind super object

    public:
        superObjectPriv() { }

        superObject* create () {
            return(new superObjectPriv());
        }

        void mainTask () {
            object::mainTask();                 //problem #3
            object::derivedHelper();            //problem #4
            //super extra stuff
        }
}

所以你可以在这里看到这不会编译。

我可以为 superObject 实现对象的纯虚拟,但这违背了从对象派生的目的。我不想重复实现,我想在它的基础上进行构建。

我可以将 superObject 更改为从 objectPriv 派生,但是我会暴露我的实现细节。我想对所有人隐藏有关 objectPriv 的所有具体信息。

我想不出任何方法来实现这一点。我有一种不好的预感,这可能是不可能的,但我祈祷你们会有一些聪明的把戏给我:)

谢谢 莱斯

【问题讨论】:

  • 搜索“Pimpl idiom”。
  • @ThomasMatthews 感谢您的建议。 Pimpl 似乎有它自己的缺点。像堆分配和额外的间接层。我不知道我是否愿意做出这样的牺牲。我意识到接口也增加了一层间接性,但这也是我仍在努力接受的东西。我目前没有使用界面设计;我改用嵌套类。这使所有内容保持私有,我仍然可以内联方法,但它会创建一些非常混乱的公共标头:(

标签: c++ inheritance interface


【解决方案1】:

你考虑过 mixin 模式吗?这是一种将共享实现添加到多个类的方法。一个定义了一个从它的参数派生的模板类。您可以使用 ObjectPriv 和 SuperObjectPriv 的常见行为来做到这一点:

template <typename ParentT>
class CommonFunctionalityMixin
: public ParentT   // this is the magic line that makes it a mixin
{
    public:
        typedef ParentT parent_type;

        virtual void mainTask()      { /* implementation */ }
        virtual void derivedHelper() { /* implementation */ }
};

class ObjectPriv
: public CommonFunctionalityMixin<object> // which derives from object
{
    public:
        typedef CommonFunctionalityMixin<object> parent_type;

        virtual object* create()     { return new ObjectPriv; }
        // virtual void mainTask() defined in CommonFunctionalityMixin
        // virtual void derivedHelper() defined in CommonFunctionalityMixin
};

class SuperObjectPriv
: public CommonFunctionalityMixin<superObject> // which derives from superObject
{
    public:
        typedef CommonFunctionalityMixin<object> parent_type;

        virtual object* create()     { return new SuperObjectPriv; }

        // now we override CommonFunctionalityMixin's mainTask() with super's
        virtual void mainTask()
        {
            parent_type::mainTask();
            parent_type::derivedHelper();
        }
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    • 2011-03-22
    相关资源
    最近更新 更多