Is this a free communication mechanism in Objective-C + Cocoa as C # delegates or C ++ Qt signals + slots?

For large programs, the standard way to complicate this is to split the program code into small objects. Most real programming languages ​​offer this functionality through classes, which is why Objective-C. But after the source code is divided into a small object, the second task is to somehow connect them with each. The standard approaches supported by most languages ​​are compositon (one object is a member field of another), inheritance, generics, and callbacks. More critical methods include method-level delagates (C #) and signals + slots (C ++ Qt). I like the idea of ​​delegates / signals, because when connecting two objects, I can connect individual methods with each, without objects that do not know anything about each of them. For C #, it will look like this:

var object1 = new CObject1(); var object2 = new CObject2(); object1.SomethingHappened += object2.HandleSomething; 

In this code, object1 calls it SomethingHappened delegate (like a regular method call), the HandleSomething object2 method will be called.

For C ++ Qt, it will look like this:

 var object1 = new CObject1(); var object2 = new CObject2(); connect( object1, SIGNAL(SomethingHappened()), object2, SLOT(HandleSomething()) ); 

The result will be the same. This method has some advantages and disadvantages, but as a rule, I like it more than interfaces, because if the base of the program code grows, I can change connections and add new ones without creating tons of interfaces. After learning Objective-C, I havn't found any way to use this technique, I like it :( It seems that Objective-C supports messaging perfectly, but it requires object1 have a pointer to object2 in order to pass the message to it. If some object needs to be connected to many other objects, in Objective-C I will be forced to point to it with pointers to each of the objects to which it should be connected.

So the question is :). This is some kind of approach to programming Objective-C, which will resemble the connection types of the delegate / signal + slot, and not "give the first object a full pointer to the second object so that it can pass a message to it." Method level associations are a little preferable for me than object level joins ^ _ ^.

+4
source share
1 answer

I will give you a couple of research pointers:

  • NSNotification
  • Monitoring Key Values
  • NSInvocation
  • Category

The first two are closest to what you ask for, but the last two have a role in this common area.

+4
source

Source: https://habr.com/ru/post/1310373/


All Articles