jeudi 22 septembre 2016

What is the (Name for the) Pattern That Couples a Non-Generic Method with a Generic One?

I have been noticing a pattern in my development, and have seen it for a while, but it struck me that I do not know its technical name, or if it has one. The reason I am asking this question is that I am building some classes around this behavior and I would like to be technically correct in how I go about naming the components and/or the namespace in which the components will be defined.

Here is a simple example of it in action:

public interface ICommand
{
    void Execute( object parameter );
}

public interface ICommand<in T>
{
    void Execute( T parameter );
}

public class Command : ICommand<int>, ICommand
{
    public void Execute( object parameter )
    {
        // What is the technical name 
        // of this behavior and/or relationship 
        // with its generics-based counterpart?
        if ( parameter is int )
        {
            Execute( (int)parameter );
        }
    }

    public void Execute( int parameter )
    {
        // We are now in strongly-typed parameter land...
    }
}

Essentially, the pattern is this:

  1. Take an object that is passed into a method that accepts a System.Object parameter. The best name I can come up with for this at the moment is "generalized object."
  2. Determine the type of the generalized object in previous step. In my example I simply check the object's type. However, other scenarios could call for value conversion/coercion.
  3. Pass the now-strongly-typed object to a strongly-typed counterpart of the generalized method described in the first step.

Is there a name for this pattern? I am also open to feedback if I am doing something horrendously wrong here and that there is a better/preferred way of going about what I am doing.

Aucun commentaire:

Enregistrer un commentaire