.NET Delegates: A C# Bedtime Story

Tight Coupling & Interfaces

This article was reprinted with permission by Chris Sells, who holds the copyright. Its official home is here.

Tight Coupling

Once upon a time, in a strange land south of here, there was a worker named Peter. He was a diligent worker who would readily accept requests from his boss. However, his boss was a mean, untrusting man who insisted on steady progress reports. Since Peter did not want his boss standing in his office looking over his shoulder, Peter promised to notify his boss whenever his work progressed. Peter implemented this promise by periodically calling his boss back via a typed reference like so:

class Worker {
    public void Advise(Boss boss) { _boss = boss; }
    public void DoWork() {
        Console.WriteLine("Worker: work started");
        if( _boss != null ) _boss.WorkStarted();
        Console.WriteLine("Worker: work progressing");
        if( _boss != null ) _boss.WorkProgressing();
        Console.WriteLine("Worker: work completed");
        if( _boss != null ) {
            int grade = _boss.WorkCompleted();
            Console.WriteLine("Worker grade= " + grade);
        }
    }
    private Boss _boss;
}
class Boss {
    public void WorkStarted() { /* boss doesn't care. */ }
    public void WorkProgressing() { /* boss doesn't care. */ }
    public int WorkCompleted() {
        Console.WriteLine("It's about time!");
        return 2; /* out of 10 */
    }
}
class Universe {
    static void Main() {
        Worker  peter = new Worker();
        Boss        boss = new Boss();
        peter.Advise(boss);
        peter.DoWork();
        Console.WriteLine("Main: worker completed work");
        Console.ReadLine();
    }
}

Interfaces

Now Peter was a special person. Not only was he able to put up with his mean-spirited boss, but he also had a deep connection with the universe around him. So much so that he felt that the universe was interested in his progress. Unfortunately, there was no way for Peter to advise the Universe of his progress unless he added a special Advise method and special callbacks just for the Universe, in addition to keeping his boss informed. What Peter really wanted to do was to separate the list of potential notifications from the implementation of those notification methods. And so he decided to split the methods into an interface:

interface IWorkerEvents {
    void WorkStarted();
    void WorkProgressing();
    int WorkCompleted();
}
class Worker {
    public void Advise(IWorkerEvents events) { _events = events; }
    public void DoWork() {
        Console.WriteLine("Worker: work started");
        if( _events != null ) _events.WorkStarted();
        Console.WriteLine("Worker: work progressing");
        if(_events != null ) _events.WorkProgressing();
        Console.WriteLine("Worker: work completed");
        if(_events != null ) {
            int grade = _events.WorkCompleted();
            Console.WriteLine("Worker grade= " + grade);
        }
    }
    private IWorkerEvents _events;
}
class Boss : IWorkerEvents {
    public void WorkStarted() { /* boss doesn't care. */ }
    public void WorkProgressing() { /* boss doesn't care. */ }
    public int WorkCompleted() {
        Console.WriteLine("It's about time!");
        return 3; /* out of 10 */
    }
}

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why.”