dimanche 23 juin 2019

Design question - Multiple classes with same functionality

I have some things to clarify for myself, so please bear with me.

Let's say I want to have an object that will explode after a certain period of time. If the object explodes while the person is still holding it, you get killed.

Let's consider the following implementation:

public interface IKillable
{

    void Kill();

}



   public interface IExplodable : IKillable
{
     float TimeLeft { get; set; }
     void Timer(float timeToExplode);

}


public abstract class Bomb: IExplodable
{
public float TimeLeft { get; set; }

public void Kill()
{
    //Destroy the object
}

public void Timer(float timeLeft)
{
    if (TimeLeft <= 0) {

        Kill();
    }
}

}

What if I also want to add let's say a "Backpack" instead of "Bomb" that will have the exact same functionality or any other item that can explode (and kill)?

  1. Is inheriting a "Backpack from Bomb" reasonable in this case?
  2. Probably copying the code won't follow SOLID principles?

Aucun commentaire:

Enregistrer un commentaire