SRP – violation

  • Each software module should have one and only one reason to change.
  • Module refers to class or even a single function.
  • When classes do too many things it  often ends up coupling together things that should not be related. It makes the overall class harder to use.
  • Responsibilities change at different times for different reasons. 
  • What is tight coupling ? —  It binds two or more details together in a way thats difficult to change.
  • Loose Coupling:- It offers a modular way to choose which details are involved in a particular operation.
  • Separation of concerns:- Programs should be separated into distinct sections each addressing a separate concern, or set of information that affects the program.
  • High level code should not know about low level implementations. It should not be tightly coupled to the specific details.
  • Cohesion:- Classes that have many responsibilities  will tend to have less cohesion than that have a single responsibility.
  • Dont go ahead and apply the SRP at the start. Analyse the pain and then refactor accordingly. Practice pain driven development.
  • Strive for high cohesion and loose coupling.
  • Keep your classes small, focussed and testable.

Too much of Responsibility

  • Logging
    • Console.WriteLine(..)
  • Source – JSON or XML , deserializing based on that
    • DeserializeToObject..
    • File.ReadAllText(…)
				
					// See https://aka.ms/new-console-template for more information

using Newtonsoft.Json;
using System.Xml.Serialization;

internal class CricketSpecialistInAFormat
{
    public int OverAllRating { get; set; }

    internal void OverAllRate(Source source)
    {

        List<CricketRating> ratingObject = new();
        if(Source.JSON == source)
        {
            var ratingJson = File.ReadAllText("C:\\Users\\611184113\\source\\repos\\SRP\\SRP\\cricket.json");
            ratingObject = JsonConvert.DeserializeObject<List<CricketRating>>(ratingJson);

        }
        else if (Source.XML == source)
        {
            string path = Path.Combine(Environment.CurrentDirectory, "cricket.xml");
            ratingObject =DeserializeToObject<List<CricketRating>>(path);

        }
        
        foreach (var item in ratingObject)
        {
            Console.WriteLine("-----------------------------");
            Enum.TryParse(item.Format, out CricketFormat format);
            switch (format)
            {
                case CricketFormat.T20:
                    Console.WriteLine("T20 Format");
                    if(item.StrikeRate > 150 && item.Average > 40)
                    {
                        Console.WriteLine($"star player:: {item.Name}");
                        Console.WriteLine("Applicable for special bonus");
                    }
                    else
                    {
                        Console.WriteLine($"Average player:: {item.Name}");
                        Console.WriteLine("player will be given next 10 chances to prove");
                        Console.WriteLine("Not applicable for bonus");
                    }
                    break;
                case CricketFormat.ODI:
                    Console.WriteLine("ODI Format");
                    if (item.StrikeRate > 95)
                    {
                        Console.WriteLine($"star player:: {item.Name}");
                        Console.WriteLine("Applicable for special bonus");
                    }
                    else
                    {
                        Console.WriteLine($"Average player:: {item.Name}");
                        Console.WriteLine("Not applicable for bonus");
                    }
                    break;
                default:
                    Console.WriteLine("unknown format!!");
                    break;
            } 
        }
    }

    public T DeserializeToObject<T>(string filepath) where T : class
    {
        XmlSerializer ser = new XmlSerializer(typeof(T));

        using (StreamReader sr = new StreamReader(filepath))
        {
            return (T)ser.Deserialize(sr);
        }
    }

    public T SerializeXml<T>(string filepath, T result) where T : class
    {
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

        using (FileStream stream = File.Create(filepath))
        {
            xmlSerializer.Serialize(stream, result);
        }
        return result;
    }
}
				
			
				
					

public class CricketRating
{
    public string Name { get; set; }
    public string Format { get; set; }
    public decimal Average { get; set; }
    public decimal StrikeRate { get; set; }

}

public enum CricketFormat
{
    T20,
    ODI
}

public enum Source
{
    JSON,
    XML
}
				
			
				
					// See https://aka.ms/new-console-template for more information


Console.WriteLine("Cricket Specialist in a format");

var player = new CricketSpecialistInAFormat();

player.OverAllRate(Source.XML);

				
			
				
					[
  {
    "Name": "SKY",
    "Format": "T20",
    "Average": "46.4",
    "StrikeRate": "184.86"
  },
  {
    "Name": "KL Rahul",
    "Format": "T20",
    "Average": "37.75",
    "StrikeRate": "140"
  },
  {
    "Name": "SKY",
    "Format": "ODI",
    "Average": "38.50",
    "StrikeRate": "100.52"
  }
]
				
			
				
					<ArrayOfCricketRating xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<CricketRating>
		<Name>sky</Name>
		<Format>T20</Format>
		<Average>40.2</Average>
		<StrikeRate>131.2</StrikeRate>
	</CricketRating>
	<CricketRating>
		<Name>sky1</Name>
		<Format>T20</Format>
		<Average>40.5</Average>
		<StrikeRate>131.2</StrikeRate>
	</CricketRating>
</ArrayOfCricketRating>
				
			

Leave a Comment