繁体   English   中英

C ++的obj-c委托模式是什么?

[英]What's the C++ equivalent of the obj-c delegate pattern?

我对obj-c相当熟悉,现在我试图在C ++中进行更深入的研究。

我正在寻找与obj-c的委派模式相对应的C ++。

您不必继承协议,而只是继承类(协议)。 一个小例子:

class Delegate 
{
public:
// Some pure virtual method here.
virtual void method() = 0; 
};

class A : Delegate
{
   void method() { // Do something here... };
};

class B
{
   Delegate your_delegate;
   // Somewhere in your code you might need to call the method() using: your_delegate.method();
};

实际上并没有1:1的等效项。 目标C是一种动态类型化的语言。 protocol中的OBJ-C是类似的功能(“选择”)与某些签名会在运行时存在一个承诺。 不必来实现他们,如果你不想,但你应该,如果你不想要的东西在运行时崩溃,除非他们打上@optional

相比之下,C ++是100%静态类型的。 如果您说某些功能应该存在,那么它必须存在,否则程序将无法编译。 如果超类将函数声明为抽象,则子类必须继承它。 多重继承(如果您想让一个类实现多个解耦协议,则需要继承)是一个雷区。

静态类型(C ++)的好处是,你可以在编译时知道你的程序是否完整功能(是否有代码,每一个地方应该有代码)。 然而,这样做的代价是,有时你必须想出其他的解决方案,以填补在的OBJ-C的协议和授权。

术语“委托”仍然可以很好地应用。 没有语法模式,但是您可以使用常用工具通过框架对其进行建模。

这里有很多代码,但是界面很干净。 用户要做的只是在底部添加一些代码。 还是有相关的构造一些样板,但这将消失在将来与C ++ 11“继承构造”语法。

/* Class to be used as a member of the delegation host.
   Keeps a list of event clients, which you can iterate over.
   As a C++11 convenience, function call operator() is overloaded to call
   all clients with the given argument list.

   This is templated over the base type for the clients. The base type
   should define virtual function(s) for handling the events.
   Templating is necessary because we can't anticipate the number or types
   of these virtual functions. */

template< typename delegate >
struct delegator {
    typedef std::list< delegate * > list; // host interface
    list delegates;

    typedef typename list::iterator delegate_id; // client interface

    delegate_id add( delegate *d )
        { return delegates.insert( delegates.end(), d ); }

    void remove( delegate_id d )
        { delegates.erase( d ); }

#if __cplusplus >= 201103L // C++11-only convenient host interface
    template< typename ... args >
    void operator() ( args && ... a ) {
        for ( auto d : delegates ) ( *d )( std::forward< args >( a ) ... );
    }
#endif
};

/* Abstract base class for all delegate bases. Registers and unregisters
   from the delegator, but doesn't define any event handler. */
template< typename derived >
struct delegate {
    typedef ::delegator< derived > delegator;

    delegator &source;
    typename delegator::delegate_id id;

    delegate( delegator &in_source )
        : source( in_source ),
        id( source.add( static_cast< derived * >( this ) ) ) {}

    virtual ~delegate() { source.remove( id ); }
};

/* Example delegate base. Defines an event handler which clients must implement.
   Other types of events might declare other bases. */
struct my_delegate_base : delegate< my_delegate_base > {
    typedef delegate< my_delegate_base > base;
    typedef base::delegator delegator;
    my_delegate_base( delegator &d ) : base( d ) {}

    virtual void operator() ( int ) = 0;
};

/* Example client class defines how to handle an event. */
struct my_delegate_impl : my_delegate_base {
    my_delegate_impl( delegator &d ) : my_delegate_base( d ) {}

    virtual void operator() ( int i ) {
        std::cout << i << '\n';
    }
};

看到它运行: https : //ideone.com/IRp5rJ

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM