Principle of Delegation, what is it?

Asked

Viewed 3,477 times

8

What Means Delegation Principle in Object-Oriented Programming?

After some research, I found a definition about this principle:

Principle of Delegation: way to make the composition as powerful for reuse purposes as the heritage.

I found an example of code in Java:

public abstract class Pato {

    public Padrao_Voaveis m_Padrao_Voaveis;

    public Pato(){

    }

    public void finalize() throws Throwable {

    }

    public void comportamento_pato(){

        m_Padrao_Voaveis.voar ();
        //(delega para a classe de comportamento)
    }

    public abstract void mostrar();

    public void nadar(){

    }
}

However, I could not understand this principle. Someone has a better way to explain to me?

  • Polymorphism this?

4 answers

7

It’s another mechanism for code reuse. It is usually used in languages other than use the concepts of classes (Actor and Self). In these languages the objects are called prototypes (prototypes) and each of them implements a specific behavior.

If object A does not implement a particular message it delegates (passes) the message to object B. If object B implements that message then he executes it with A’s data, otherwise he delegates it to his "delegatees".

In class-based languages this mechanism can also be obtained. This is done through pass the message to the other object. For this is the delegating object of the service contains a reference to the object responsible by execution.

It is important to indicate that the delegation differs from the inheritance in two important ways:

  1. With inheritance, you have only one instance of the object. There is only one indivisible object, for what is inherited becomes an intrinsic part of the new class.

  2. The delegation usually provides the user with only what is in the public interface. Normal inheritance gives more access to the inner details of the inherited class.

De:

Para:

If you look carefully at the previous figure, you will notice that Robovulcao still has 3 sensor-related methods; these are methods that just call the corresponding sensor methods. That’s exactly what delegation is, just pass on functionality to the contained parts.

Delegation comes along with composition to offer flexible and elegant solutions like the one we saw earlier, and also respects the principle "separate mutable code from static code", but also charges a price for it: the need for methods that "wrap" calls impose extra processing time because of calls to these methods.

5

The term can be used in different ways in programming. In OOP it is basically a form of aggregation more specific. You leave certain aspects for another class - the delegate - to accomplish what this class - the delegator - needs.

Delegation occurs by having a reference in some member to the other class. It allows code reuse manually.

The delegation does not fail to occur automatically with inheritance and polymorphism, but in this case the term is not usually used.

Only inheritance can be problematic in some cases. And I’m talking about multiple inheritance which is even more problematic and many languages don’t even implement it.

Inheritance is often abused and the reuse of the code is considered without properly conceptualizing the relationship between the more general and more specific type. Often reuse can be obtained without inheritance. Of course this will have to be done more manually. On the other hand there is more control and flexibility over reuse.

In fact modern languages preferred not to have an inheritance. This may require a little more code in some cases by requiring manual delegation, which is what we’re talking about here, but they make language simpler and avoid certain kinds of problems.

Your example

In your example shows what I I commented on a previous question. For this class it does not matter how the flight takes place. It is not her responsibility (remembering that the classes must have single responsibility, being cohesive). It takes care of the duck in a general way. The flight-specific mechanism is defined in another class. Its class only calls this mechanism.

If it wasn’t for this delegation, in addition to the class being doing more than it needs, it would still have to reproduce a code, which you may not even have access to, in your method. To access the method that actually implements the mechanism you need to have a reference to this class and this is obtained with the variable m_Padrao_Voaveis.

An interesting detail is that the type specific of this variable was not defined in this code and therefore you can use a sub-type of Padrao_Voaveis giving some flexibility to what will actually be executed, depending on how this variable is initialized, which may occur within the class itself in configurable or not, or externally, i.e. dependency injection.

This can be especially useful when using interfaces. Let’s say the method comportamento_pato() is part of an interface that you must implement in this class. It takes work, you take risks writing all the code of this method. But you can have the specific implementation in another class. Then you fulfill the contract by defining the method required by the interface but delegate the implementation of its code to what the other class already knows how to do.

Some languages have facilities that automates a little this delegation.

3

In Object-Oriented Programming, according to Angun Croll, engineer and author of the blog Javascript, defines the Delegation as being a technique promoting the re-use of the code by allowing the function to be invoked in the context of a specific instance - irrespective of the hierarchical line of the instance and function. - source

Follow an example:

private static function curlExec($link, $data) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $link);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data, '', '&'));

    $response = curl_exec($ch);

    echo $response;
}

And being called in another function...

public function login($email, $senha) {  

    $data = array(
        'nome' => $email,
        'senha' => $senha
    );

    $link = 'http://teste.local/login';
    $response = self::curlExec($link, $data);

    ....      

1

Delegation in the Object Orientation view is when you develop an Object that delegates tasks to other objects, it is as if you have an intermediate object between the Superclass and the Class that has processing.

In this article more details can be found on this subject;

  • Hello, Giovanni. If you can explore a little more the subject would be great.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.