1

I am trying to implement cocoa's delegation pattern in c++.

I have tried to emulate the cocoa delegation pattern in imagepicker example as given below. However, I am not sure this is the correct way of implementing in C++, and wonder if somebody has an idea for something better. I also notice that this implementation I came out is nothing to do with traditional c++ delegate(?) ( see here ) . I am not even sure delegate in the previous link is actually delegate pattern (I think it is nothing but function pointer).

class ImagePickerControllerDelegate {          // defined as protocol in swift

    public:

    virtual void ImagePickerFinished() = 0;

};


class ImagePickerController {

public:

    ImagePickerControllerDelegate * delegate;

private:

    void findImageDirectory()   {}

    void checkUserPermission()  {}

    void loadImage(char* image_name)    {}

    void andDoOtherThings() {}

public:

    void Run()  {

    // doing long stuff here ..
    findImageDirectory();

    checkUserPermission();


    loadImage("lena");

    andDoOtherThings();

    // done, notify

    delegate->ImagePickerFinished();

    // other clean-up etc.
    }

};



class MainViewController: ImagePickerControllerDelegate {


    ImagePickerController ImagePicker = ImagePickerController();

public:

    MainViewController()    {

    this->ImagePicker.delegate = this;

    }

    void UserClicked()  {

    ImagePicker.Run();

    }

    virtual void ImagePickerFinished()  {
    std::cout << "image picker finished..";
    }


};

In short I need good suggestions for simple and beautiful implementation of cocoa delegate pattern in C++. I really do not like ugly STL template stuff, so please do not give suggestions/examples that uses it.

mag_zbc
  • 6,801
  • 14
  • 40
  • 62
eral
  • 123
  • 1
  • 16
  • 1
    I suppose when implementing delegate interface you should do `class MainViewController: public ImagePickerControllerDelegate`, not `class MainViewController: ImagePickerControllerDelegate` – olha Feb 12 '19 at 09:13
  • The best way to implement a delegate is using the pure virtual functions, look at https://stackoverflow.com/questions/3130588/c-question-feature-similar-to-obj-c-protocols – Sachin Vas Feb 12 '19 at 09:13
  • You could use `std::function` in conjunction with `std::bind` (or capturing lambda) to get rid of abstract classes and the need of inheritance and still get an elegant solution. However, `std::function` comes from STL and does use templates. – Krzysiek Karbowiak Feb 12 '19 at 09:28

1 Answers1

0

I think that delegates in the Cocoa SDK implement two patterns: Observer pattern and Delegate pattern.

In your implementation you implemented part of Observer pattern, where MainViewController notifies the delegate about event.

Example of Delegate pattern in the cocoa are table view delegate and data source. Table view gives creation of cells and views to the delegate and dataSource.

Your implementation of the Listener pattern is ok.

Andrew Romanov
  • 4,774
  • 3
  • 25
  • 40
  • Actually MainViewController is the one which is being notified. With the user click, it starts image picking operation, and it gets notified when picking is done. – eral Feb 12 '19 at 10:54
  • Most part APIs in the cocoa has one listener (delegate) in a public api. – Andrew Romanov Feb 12 '19 at 11:06