How to resolve the algorithm Delegates step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Delegates step by step in the C programming language

Table of Contents

Problem Statement

A delegate is a helper object used by another object. The delegator may send the delegate certain messages, and provide a default implementation when there is no delegate or the delegate does not respond to a message. This pattern is heavily used in Cocoa framework on Mac OS X. See also wp:Delegation pattern. Objects responsibilities: Delegator: Delegate: Show how objects are created and used. First, without a delegate, then with a delegate that does not implement "thing", and last with a delegate that implements "thing".

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Delegates step by step in the C programming language

The code implements the delegation pattern that consists in the ability of an object to delegate the execution of a method to another object. It has the following classes:

  • Delegate: stores a function pointer to a Responder function and the method thing that executes the function.
  • Delegator: stores a Delegate object, an int parameter, a phrase, and the method Operation that executes the thing method on the Delegate object.

The main function creates two Delegate objects, one with the thing1 function and another one with a NULL function, and a Delegator object with the delegate set to the defaultDelegate object.

The Operation method first tries to execute the thing method on the Delegate object passed as an argument, if it is NULL it tries to execute the thing method on the defaultDelegate object.

The output of the program is:

We're in thing1 with value 3
default implementation

We're in thing1 with value 3
delegate implementation

We're in thing1 with value 3
default implementation

Source code in the c programming language

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef const char * (*Responder)( int p1);

typedef struct sDelegate {
    Responder operation;
} *Delegate;

/* Delegate class constructor */
Delegate NewDelegate( Responder rspndr )
{
    Delegate dl = malloc(sizeof(struct sDelegate));
    dl->operation = rspndr;
    return dl;
}

/* Thing method of Delegate */
const char *DelegateThing(Delegate dl, int p1)
{
    return  (dl->operation)? (*dl->operation)(p1) : NULL;
}

/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
typedef struct sDelegator {
    int     param;
    char    *phrase;
    Delegate delegate;
} *Delegator;

const char * defaultResponse( int p1)
{
    return "default implementation";
}

static struct sDelegate defaultDel = { &defaultResponse };

/* Delegator class constructor */
Delegator NewDelegator( int p, char *phrase)
{
    Delegator d  = malloc(sizeof(struct sDelegator));
    d->param = p;
    d->phrase = phrase;
    d->delegate = &defaultDel;	/* default delegate */
    return d;
}

/* Operation method of Delegator */
const char *Delegator_Operation( Delegator theDelegator, int p1, Delegate delroy)
{
    const char *rtn;
    if (delroy) {
        rtn = DelegateThing(delroy, p1);
        if (!rtn) {			/* delegate didn't handle 'thing' */
            rtn = DelegateThing(theDelegator->delegate, p1);
        }
    }
    else 		/* no delegate */
        rtn = DelegateThing(theDelegator->delegate, p1);

    printf("%s\n", theDelegator->phrase );
    return rtn;
}

/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
const char *thing1( int p1)
{
    printf("We're in thing1 with value %d\n" , p1);
    return "delegate implementation";
}

int main()
{
    Delegate del1 = NewDelegate(&thing1);
    Delegate del2 = NewDelegate(NULL);
    Delegator theDelegator = NewDelegator( 14, "A stellar vista, Baby.");

    printf("Delegator returns %s\n\n", 
            Delegator_Operation( theDelegator, 3, NULL));
    printf("Delegator returns %s\n\n", 
            Delegator_Operation( theDelegator, 3, del1));
    printf("Delegator returns %s\n\n",
            Delegator_Operation( theDelegator, 3, del2));
    return 0;
}


  

You may also check:How to resolve the algorithm Totient function step by step in the Delphi programming language
You may also check:How to resolve the algorithm Comments step by step in the Neko programming language
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the 11l programming language
You may also check:How to resolve the algorithm Search a list of records step by step in the Ruby programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the MontiLang programming language