Announcement

Collapse
No announcement yet.

Difference between Delegates, static functions and inheritance?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    Difference between Delegates, static functions and inheritance?

    Hello everyone,

    I just read all the information about Delegates that i found in the UE4 documentation page, and as far as I unerstood, the delegate is a way to call functions from one class to other without knowing each other.

    At this point I just got confused about why just simply implementa interface with the actions you expect to do?
    Or in case you don't want to implement the action for every actor, just make some inheritance or static function in some class?

    If anyone can clarify the purpose of delegates I would appreciate so much.

    Thanks

    #2
    Delegates can be used to subscribe without a concrete type, because you subscribe with a function with the signature of the delegate instead of with an object reference.
    You still need to know references for the delegate to subscribe to though.

    An interface can be used to achieve the same thing, but you lose the flexibility of subscribing ANY function. With Delegates you can subscribe with two different functions of the same object.
    They are even more flexible in C++ where you subscribe with Lambdas and Raw pointers

    Comment


      #3
      Delegates are a subscription model. "My system would like to know when the world is created." Your system doesn't know how the world is created, at what point, etc, but it knows it will get notified when the world is created.

      Interfaces are more for forcing any future sub classes into a paradigm.

      Let's say I have a Door class. All doors I make will need an OpenDoor and CloseDoor method. I can make an interface (IDoorInterface) which contains those methods. Now I can create classes that inherit from the interface (TrapDoor, LockedDoor, etc) which I can guarantee will have an OpenDoor/CloseDoor method.

      Code:
      if (IDoorInterface* MyDoor = Cast<IDoorInterface>(MyPotentialDoorObject))
      {
          // It's a door, try to open it.
          MyDoor->OpenDoor();
      }
      Able Ability System - A high performance, robust ability system for UE4. Now Available!

      Comment


        #4
        I see, In some way delegates are implementing observer pattern, so when something happens everyone get notified without caring what class or what interface are implementing. Meanwhile interfaces allows you to safely call a method from any class that imlpement the interface.

        Comment


          #5
          You can also look at the direction of communication.

          With Delegates Receiver is listening for an Event on the Sender. Sender only care about calling the delegate, and Receiver needs to know about the Sender.
          With Interfaces Sender is messaging the Receiver Interface if it implements it. Sender needs a list of the Receivers, and Receivers simply Implement the Interface and don't care about who sent the Message.

          So Delegates is the reverse communication of Interfaces which is often very useful and is (as you mention) known as the observer pattern.

          Comment

          Working...
          X