Let's say we have an object that is going to consume some object but, in the process, will be affected by that object or, perhaps, the environmental context of the action.
To bring that closer to home, consider eating. The eater consumes the eatie. The eatie, by being consumed, can cause all sorts of possible affect to the eater: indigestion, increased energy, nourishment, or even death by poison...
So the eater does its eating thing but then the eatie does its decomposing via digestion.
Here's a simple example.
/// define our delegate
public delegate void Digested(Organism Predator);
/// base class Organism.
public abstract class Organism
{
private System.Collections.ArrayList alMeals = new System.Collections.ArrayList();
public bool IsAlive = true;
public virtual void Eat(Organism prey, Digested PreyDigestion)
{
PreyDigestion(
this);
alMeals.Add(PreyDigestion);
}
public virtual void BeDigested(Organism predator)
{
this.Die();
for(int iMeal = 0; iMeal < alMeals.Count; iMeal++)
{
Digested DigMeal = (Digested)alMeals[iMeal];
DigMeal(predator);
}
}
public virtual void Die()
{
this.IsAlive = false;
}
public virtual void Nourish(){}
}
/// class Mammal
public class Mammal:Organism
{
public override void Eat(Organism prey, Digested PreyDigestion)
{
//Bite...
//Chew...
//Swallow...
base.Eat(prey,PreyDigestion);
}
public override void BeDigested(Organism predator)
{
predator.Nourish();
base.BeDigested(predator);
}
public override void Die()
{
//Moan();
//Heart.Stop();
base.Die();
}
}
/// class fish
public class Fish:Organism
{
public override void BeDigested(Organism predator)
{
predator.Nourish();
base.BeDigested(predator);
}
public override void Die()
{
//Heart.Stop();
base.Die();
}
}
/// class Algae
public class PoisonousAlgae:Organism
{
public override void BeDigested(Organism predator)
{
if(predator is Mammal)
{
predator.Die();
}
else
{
predator.Nourish();
}
base.BeDigested(predator);
}
public override void Die()
{
//Photosynthesis.stop();
base.Die();
}
}
Now, If the bear eats the fish, she will be nourashed and happy. If the fish eats the algae, he is nourished. If the Bear eats the Algae, she will die of Ciguatera Toxin Poisoning. If the Fish first eats the Algae before the Bear east the fish, the Bear will die of poisoning just the same.
Using inheritence and delegates passed as parameters, everything an organism consumes becomes part of them and, should they be eaten, their consumer will have to digest their meal as well.
What did we learn? Bears shouldn't eat algae and should avoid large quantities of fish that eat algae. Stick to Salmon and Tuna.
As opposed to the service class having to expose a method or support an interface that the client can call, the client can expect a delegate as a parameter. That parameter might be something completely separate from the service class. Perhaps a transponder was attached to the fish and now the transponder is in the bear...
See and step through the attached example if you want to watch it in action.
DelegateExample.zip (6.99 KB)