vendredi 25 janvier 2019

Add new methods to interface or cast the object?

We have an interface like this?

public interface ILicense
{
   bool IsTrial { get; }
   bool IsTrialLicense { get; }
}

public class StandardLicense : ILicense
{
  public bool IsTrial { get { return true; } }
  public bool IsTrialLicense { get { return true; } }
}

But now new requirement came for new License (the name is ZLicense) which has additional methods.

  • GenerateActivationKey()
  • GenerateDeactivationKey()

So I have two options:

  • Add two new methods to interface (and make an abstract class)

  • Cast the interface and use new two methods

Usage can be used like this:

if(ILicense is ZLicense)
{
  ZLicense license = ILicense as ZLicense
  .. can use new methods lile license.GetActivationKey()
}

What's best practise approach here?

Aucun commentaire:

Enregistrer un commentaire