C# how delegates and interfaces can play a role in this example

319 views Asked by At

I would like to understand if/when to use delegates and interfaces in my code. The structure is pretty simple. The main class initialize a windows form:

class MainClass
{
    public static void Main()
    {
        InputForm InputForm1 = new InputForm();
        InputForm1.ShowDialog(); // show interface to prompt user
    }
}

And the class for the form has a button and few more methods:

public partial class InputForm : Form
{
    public InputForm()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e) 
    {   
        // do some calculation and then create a dictionary of items

        for (int n = 1; n <= dict.Count; n++) // loop through items
        {

            dict[n].calculatedLength = theLength.calcLength(arg1, arg2, dict[n].speed); 

        }
    }
}

When the button is clicked, the program does some calculation (calling methods in the same class InputForm) and save results into a dictionary (dict). Each element is an animal and I have some properties that I store in the dictionary (e.g. under the key "Dog" I have an average weight of dogs, an average speed, etc.). Using the speed and two default arguments (arg1 that is the number of hours and arg2 that is the number of minutes) I have to call the method of the class LengthClass in order to get the estimated length that is covered by the specific animal in arg1 hours and arg2 minutes. The LengthClass is like this:

class LengthClass
{
    static double calcLength(double arg1, double arg2, double speed)
    {
        // do some calculation
        return x;
    }
}

Now the question: is there space in this example to use delegates and interfaces? Can you please show me how to best do it and what are the advantages/disadvantages in doing so instead of calling the calcLength method directly as I'm doing?

1

There are 1 answers

2
MVCDS On

EDITED: I included the Microsoft guideline


Microsoft Guideline

Both delegates and interfaces enable a class designer to separate type declarations and implementation. A given interface can be inherited and implemented by any class or struct. A delegate can be created for a method on any class, as long as the method fits the method signature for the delegate. An interface reference or a delegate can be used by an object that has no knowledge of the class that implements the interface or delegate method. Given these similarities, when should a class designer use a delegate and when should it use an interface?

Use a delegate in the following circumstances:

  • An eventing design pattern is used.
  • It is desirable to encapsulate a static method.
  • The caller has no need to access other properties, methods, or interfaces on the object implementing the method.
  • Easy composition is desired.
  • A class may need more than one implementation of the method.

Use an interface in the following circumstances:

  • There is a group of related methods that may be called.
  • A class only needs one implementation of the method.
  • The class using the interface will want to cast that interface to other interface or class types.
  • The method being implemented is linked to the type or identity of the class: for example, comparison methods.

The original answer (What I think)

Interfaces and delegates were made to make things easier when two classes have the same proprieties/behaviours.

I think that if you had something like that

 private void Button(object sender, EventArgs e)
 {
     List<IAnimal> animals = Farm.GetAnimals();
     foreach(IAnimal animal in animals)
     {
         dict[animal.name] = LeghtClass.CalculateLenght(animal);
         //dict[animal.name] = animal.CalculateOwnLenght(); //using a method in the interface
     }
 }

You wouldn't have to worry about passing the method its parameters (wichh would help you when refactoring).

 interface IAnimal 
 {
   double Hours;
   double Minutes;
   double GrowSpeed;
   //void CalculateOwnLenght();
 }

And your lenght class would calculate it like that:

class LengthClass
{
    static double calcLength(IAnimal animal)
    {
        return (animal.Hours + animal.Minutes / 60) * animal.GrowSpeed;
    }
}

I can't think how delegates would help you unless if you do something like that:

foreach(var animal in animals)
{
    delegate = animal.CalculateOwnLeght;
    dict[animal.name] = delegate();
}

Ok, I have shown some code without saying anything.

I think interfaces are useful if you use them in more than one place, otherwise it can be messy - you just repeat youself more when using interfaces.

I personally don't think your way is wrong but I've read people saying that static is bad practise without proving why in a way I could understand, the one thing I remember is that it gets confusing easily and uses more resources.

I'd use delegates when I have to point to some method but I don't know the object of untill the code is running (they don't have mush use for this case, I think).

delegate void Cry();

...

cat = new Cat(); dog = new Dog();

...

foreach(var animal in animals)
{
    if (animal is Cat)
    {
        Cry = cat.meow;
    }
    else if (animal is Dog)
    {
        Cry = dog.bark;
    }
    else {...}

    if (animal.isInPain)
    {
        Cry();
    }
}