Ifs and Object Orientation - C#

Asked

Viewed 185 times

5

I have a question about how to replace IF with polymorphism.

I will give the following example:

Let’s say I have the classes ExecucaoIndividual and ExecucaoGeral.

For this I created an interface to use the standard Strategy that has remained so:

interface IExecutor {
    void Executar();
} 

class ExecucaoIndividual : IExecutor {

    public void Executar() {
       //bla bla bla
    }
}

class ExecucaoGeral : IExecutor {

    public void Executar() {
        //bla bla bla
    }
}

Until then beauty, but in the base class where I will check which class I should call (individual or general), which would be the best approach not to have to be using:

IExecutor execucao;

if(determinadaCondicao)
  execucao = new ExecucaoIndividual();
else
  execucao = new ExecucaoFinal();

execucao.Executar();

I’ve seen some Factories, Maps, etc, but nothing that would take away my doubt. Someone can give me a light?

  • What is the problem with the approach you are currently using?

  • Man, I imagine you have nothing to do, by the example you gave, will have to continue with the conditional, to know which class the object should be instance...

  • I wonder if there’s any better approach to doing this :)

  • 1

    I don’t think so. It depends a lot on the code. The answers, I think, would be opinionated.

  • BTW, not always ifs are bad: http://answall.com/a/4745/1745

  • If you have many Executioner and are used in several classes, you can write something like a Factory. You pass the condition and receive the Executor. You will have the if’s same but only in one place.

Show 1 more comment

3 answers

3


Polymorphism does not apply here.

Polymorphism happens in variables, in instances.

For example: I have the class gato, and the class cachorro, both derived from animal. The animal class has a property cor and a method alimentar().

Like gato and cachorro are animals, I can call cor or alimentar() in a cat.

Now, when I create a new instance, I cannot create without knowing the type. Nor can I create a cachorro as if it were a animal.

In your case, if is on the other "tip" of logic. It is it that determines the type of class. And this cannot be done polymorphically.

  • When creating a new instance, you need to define the type.

  • When using the object, you can use it as if it were another type, thanks to polymorphism.

Factories and others will serve to centralize this decision of which type instantiate, but in the end, there will always be a if or equivalent structure.

  • Thank you, all your answers were helpful. I was looking for a way to abolish ifs rsrs, but there is no way.

  • It doesn’t exist. But what is done, and an important item of architecture, is to centralize the creation of these instances. Look for Ioc (invertion of Concern) or DI (dependency Injection), I think you will be interested.

2

You can use a Factory thus:

class ExecucaoFactory {

    public void IExecutor Criar(boolean condicao) {
        return condicao ? new ExecucaoIndividual() : new ExecucaoFinal();
    }
}
ExecucaoFactory factory = new ExecucaoFactory();
factory.Criar(algumaCondicao).Executar();

Deep down you’re putting the if (or the equivalent ternary operator) within the Factory, where its complexity will be encapsulated and concentrated in only one place, simplifying possible future changes that may be needed.

2

In this case use a Factory. A class with a static method that returns a Yexecutor. Maybe it’s something like

public static IExecutor GetBestExecutor(Condicao _condicao)
{
   switch(_condicao) 
   {
       case Condicao.Individual:
         return new ExecucaoIndividual();
         break;
       default
         return new ExecucaoGeral();
         break; 
   }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.