OCP – 2

				
					using SRP;
internal class CricketSpecialistInAFormat
{
    public Logger Logger { get; set; } = new Logger();
    public CricketRatingSource sourceObj = new ();
    public CricketDeserializer cricketDeserializerObj = new();
    public int OverAllRating { get; set; }
    public void OverAllRate(Source source)
    {
        List<CricketRating> ratingObject = new();
        var ratingStr = new RatingSource(source, sourceObj, cricketDeserializerObj);
        ratingObject= ratingStr.cricketRating();

        foreach (var item in ratingObject)
        {
            Logger.Log("-----------------------------");
            var factory = new SRP.Factory.CricketRatingFactory(Logger);
            var rating =factory.Create(item);
            rating.Rate(item);
        }
    }
}
				
			
				
					public class RatingSource
{
    private readonly Source _source;
    private readonly SRP.CricketRatingSource _sourceObj;
    private readonly CricketDeserializer _cricketDeserializerObj;
    public RatingSource(Source source, SRP.CricketRatingSource sourceObj, CricketDeserializer cricketDeserializerObj)
    {
        _source = source;
        _sourceObj = sourceObj;
        _cricketDeserializerObj = cricketDeserializerObj;
    }

    public List<CricketRating> cricketRating()
    {
        if (Source.JSON == _source)
        {
            var ratingJsonStr = _sourceObj.CricketRatingFromJson();
            return _cricketDeserializerObj.cricketRatingDeserializeJson(ratingJsonStr);
        }
        else if (Source.XML == _source)
        {
            var ratingXMLStr = _sourceObj.CricketRatingFromXML();
            return _cricketDeserializerObj.cricketRatingDeserializeXml(ratingXMLStr);
        }
        return new List<CricketRating>();
    }
}
				
			
				
					namespace SRP.Factory
{
    public class CricketRatingFactory
    {
        private readonly Logger _logger;
        public CricketRatingFactory(Logger logger)
        {
            _logger = logger;
        }

        public IRating Create(CricketRating item)
        {
            Enum.TryParse(item.Format, out CricketFormat format);
            switch (format)
            {
                case CricketFormat.T20:
                    return new T20RatingPlayer(_logger);
                case CricketFormat.ODI:
                    return new ODIRatingPlayer(_logger);
                default:
                    return new UnknownRatingPlayer(_logger);
            }
        }
    }
}

				
			
				
					namespace SRP.Factory
{
    public class CricketRatingFactory
    {
        private readonly Logger _logger;
        public CricketRatingFactory(Logger logger)
        {
            _logger = logger;
        }

        public IRating Create(CricketRating item)
        {
            Enum.TryParse(item.Format, out CricketFormat format);
            switch (format)
            {
                case CricketFormat.T20:
                    return new T20RatingPlayer(_logger);
                case CricketFormat.ODI:
                    return new ODIRatingPlayer(_logger);
                default:
                    return new UnknownRatingPlayer(_logger);
            }
        }
    }
}

				
			

Leave a Comment