How does inheritance replace the switch body?

I am using C # Switch case, how can I replace using inheritance. the case is like 1,2,3,4, so how can I implement it.

eg:

    public Blocks(int code)
    {
         bool[,] shp1;

        switch (code)
        {
            case 1:
                this._Width = 4;
                this._Height = 1;
                this._Top = 0;
                this._Left = 4;

                shp1 = new bool[_Width, _Height];
                shp1[0, 0] = true;
                shp1[1, 0] = true;
                shp1[2, 0] = true;
                shp1[3, 0] = true;
                this.Shape = shp1;
                break;

            case 2:
                this._Width = 2;
                this._Height = 2;
                this._Top = 0;
                this._Left = 4;

                shp1 = new bool[_Width, _Height];
                shp1[0, 0] = true;
                shp1[0, 1] = true;
                shp1[1, 0] = true;
                shp1[1, 1] = true;
                this.Shape = shp1;
                break;


            default:
                throw new ArgumentException("Invalid Block Code");
        }
    }

      

+2


source to share


3 answers


The basic idea is that instead of having a method that decides what to do based on the state, you have different types of objects that have different implementations of the same method that do the right thing for that type of object.

Let's say you now have a Car class and the values ​​(1, 2, 3, ...) refer to different brake configurations. In your current Brake () method, you have code that looks like this:

public class Car
{
    public void Brake()
    {
          switch (this.BrakeType)
          {
              case 1:  // antilock brakes
                ....
              case 2:  // 4-wheel disc brakes, no antilock
                ....
              case 3:  // rear-drum, front-disc brakes
                ....

          }
     }
}

      



What you would really like to do is the different classes that implement the Brake method. In this case, I will have a BrakeStrategy for each type of brake. Assign the correct BrakeStrategy to the vehicle object for its configuration, and then simply call the strategy method from the Brake method.

public class Car
{
     public BrakeStrategy BrakeStrategy { get; set; }

     public void Brake()
     {
         this.BrakeStrategy.Brake();
     }
}

public class ABSBrakes : BrakeStrategy
{
     public void Brake()
     {
        ... do antilock braking...
     }
}

var car = new Car { BrakeStrategy = new ABSBrakes(), ... };
car.Brake();  // does ABS braking

      

+8


source


You can use the Strategy pattern .
But you have to make sure that you are not trying to kill the fly with a gun, if you know what I mean. Make sure you are not introducing more complexity than you are trying to remove. In some cases, replacing your switch statement with a strategy is a good solution, in other cases it is not.



As I see your code now, I think I would pick some kind of factory pattern ...

+4


source


If you are using inheritance, you can still change the implementation dynamically, but without using a switch statement.

Taking the above BrakeType example:

Dictionary<BrakeType, BrakeStrategy> strategyList = new Dictionary<BrakeType, BrakeStrategy>();

strategyList.Add(BrakeType.ABS, new ABSBrakes());
strategyList.Add(BrakeType.Disc, new DiscBrakes());
strategyList.Add(BrakeType.RearDrum, new RearDrumBrakes());

public class Car
{
    public BrakeType TypeOfBrake {get; set;}

    public void Brake()
    {
        strategyList[TypeOfBrake].Brake();
    }
}

      

So, instead of a big switch statement cluttering up the class logic, this is a simple dictionary lookup.

+1


source







All Articles