Ad

How about fix Open/Close principle violation?

Code
Diff
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public enum Quality { SD, HD, UHD }
    
    public class TV
    {
        private class Contract
        {
            public string Type;
            public Func<int, Quality, bool> Condition;
    
            public static readonly IList<Contract> Types =
                new List<Contract>
                {
                    new Contract { Type = "PREMIUM", Condition = (numDevices, quality) => numDevices == 4 && quality == Quality.UHD },
                    new Contract { Type = "STANDARD", Condition = (numDevices, quality) => numDevices == 2 && quality == Quality.HD },
                    new Contract { Type = "ESSENTIAL", Condition = (numDevices, quality) => numDevices == 1 && quality == Quality.SD },
                    new Contract { Type = "INVALID", Condition = (numDevices, quality) => true }         
                };
        }
    
        public static string GetContract(int numDevices, Quality quality)
        {
            return Contract.Types.First(contract => contract.Condition(numDevices, quality)).Type;
        }
    }
    • using System;
    • using System.Collections.Generic;
    • using System.Linq;
    • public enum Quality { SD, HD, UHD }
    • public class TV
    • {
    • public static string GetContract( int numDevices, Quality quality)
    • {
    • string result = "INVALID";
    • if (numDevices == 4 && quality == Quality.UHD)
    • result = "PREMIUM";
    • else if (numDevices == 2 && quality == Quality.HD)
    • result = "STANDARD";
    • else if (numDevices == 1 && quality == Quality.SD)
    • result = "ESSENTIAL";
    • return result;
    • }
    • }
    • private class Contract
    • {
    • public string Type;
    • public Func<int, Quality, bool> Condition;
    • public static readonly IList<Contract> Types =
    • new List<Contract>
    • {
    • new Contract { Type = "PREMIUM", Condition = (numDevices, quality) => numDevices == 4 && quality == Quality.UHD },
    • new Contract { Type = "STANDARD", Condition = (numDevices, quality) => numDevices == 2 && quality == Quality.HD },
    • new Contract { Type = "ESSENTIAL", Condition = (numDevices, quality) => numDevices == 1 && quality == Quality.SD },
    • new Contract { Type = "INVALID", Condition = (numDevices, quality) => true }
    • };
    • }
    • public static string GetContract(int numDevices, Quality quality)
    • {
    • return Contract.Types.First(contract => contract.Condition(numDevices, quality)).Type;
    • }
    • }