Delegation

--Originally published at diegotc2016

Delegation is when a class “delegates” another class to do something.

Delegate is to give the power you have to do something to another person in order to do something.

In OOP delegation is when a class gives one or more functionalities to another class in order to do something.

Here’s an example:

Some parents want to go out. They hire a babysitter and tell her to look after the baby.
But what if the baby gets sick?
There’s a note on the fridge door. “In case of emergency phone Helen 555-3141”

This is a pretty good analogy for delegation. Helen is the
delegate.

//within the parent class
babysitter.delegate = helen
[babysitter lookAfter:baby]

//within the babysitter class
if (baby.isSick)
{
[delegate callAbout:baby]
}

When assigning actions to an object, you can provide that object with a delegate. The delegate will be called when the object needs to inform something or do something beyond the scope of the object.

Delegation

Where I got this information from?

http://es.thefreedictionary.com/delegar

https://www.quora.com/What-is-delegation-in-object-oriented-programming

https://es.wikipedia.org/wiki/Delegaci%C3%B3n_(inform%C3%A1tica)

 


Delegation

Delegation: as interesting as merchants selling apparel in ‘tianguis’

--Originally published at richardctc201

Delegation: as interesting as merchants selling apparel in ‘tianguis’

In preparation for the exam, here is my blog post about mastery topic: delegation. This term may seem as a tough to understand one, but actually is a concept that we as humans practice everyday in almost every situation we have.

I searched in a bunch of web pages trying to understand the concept of delegation; I searched in Wikipedia, in some Community web pages, and plenty of other web pages, and I finally, I got to a final definition that I like a lot: delegation is to ask someone else to help you. In this case (in an POO environment) the definition its transformed to ‘an object that when receives a request, it passes this request to a second object that is more specialized in this task’. As I said it before, humans tend to do this a lot in their daily lives. When someone receives a bunch of tasks, the first thing he/she does is ask for help; he/she delegates part of their tasks to someone else so that the amount of work can be less stressful or less complicated.

I found a number of good examples in code that explain how delegation is performed in code. Something important to mention is that when an object delegates its request to another object, the first object passes its request as well as itself as a parameter to the other object, so the later can complete the task.

The clearer example was published by Mladen Prajdic (cool name!) in a community web page called ‘Stackoverflow’. It’s important to notice the way that a delegation process is declared, and how it is transferred to the object #2 so that this obj#2 inherit the necessary methods to complete the requested task.

delegate void delMyDelegate(object o);
private void MethodToExecute1(object o)
{
    // do something with object
}
private void MethodToExecute2(object o)
{
    // do something else with object
}
private void DoSomethingToList(delMyDelegate methodToRun)
{
    foreach(object o in myList)
        methodToRun.Invoke(o);
}
public void ApplyMethodsToList()
{
    DoSomethingToList(MethodToExecute1);
    DoSomethingToList(MethodToExecute2);

Its important to see how the declaration of a delegation is made,

Delegation: as interesting as merchants selling apparel in ‘tianguis’

Delegation: as interesting as merchants selling apparel in ‘tianguis’

--Originally published at richardctc201

Delegation: as interesting as merchants selling apparel in ‘tianguis’

In preparation for the exam, here is my blog post about mastery topic: delegation. This term may seem as a tough to understand one, but actually is a concept that we as humans practice everyday in almost every situation we have.

I searched in a bunch of web pages trying to understand the concept of delegation; I searched in Wikipedia, in some Community web pages, and plenty of other web pages, and I finally, I got to a final definition that I like a lot: delegation is to ask someone else to help you. In this case (in an POO environment) the definition its transformed to ‘an object that when receives a request, it passes this request to a second object that is more specialized in this task’. As I said it before, humans tend to do this a lot in their daily lives. When someone receives a bunch of tasks, the first thing he/she does is ask for help; he/she delegates part of their tasks to someone else so that the amount of work can be less stressful or less complicated.

I found a number of good examples in code that explain how delegation is performed in code. Something important to mention is that when an object delegates its request to another object, the first object passes its request as well as itself as a parameter to the other object, so the later can complete the task.

The clearer example was published by Mladen Prajdic (cool name!) in a community web page called ‘Stackoverflow’. It’s important to notice the way that a delegation process is declared, and how it is transferred to the object #2 so that this obj#2 inherit the necessary methods to complete the requested task.

delegate void delMyDelegate(object o);
private void MethodToExecute1(object o)
{
    // do something with object
}
private void MethodToExecute2(object o)
{
    // do something else with object
}
private void DoSomethingToList(delMyDelegate methodToRun)
{
    foreach(object o in myList)
        methodToRun.Invoke(o);
}
public void ApplyMethodsToList()
{
    DoSomethingToList(MethodToExecute1);
    DoSomethingToList(MethodToExecute2);

Its important to see how the declaration of a delegation is made,

Delegation: as interesting as merchants selling apparel in ‘tianguis’