THE SINGLE RESPONSIBLE PRINCIPLE

Problem

```/// <summary>
/// Class calculates the area and can also draw it on a windows form object.
/// </summary>

public class RectangleShape
{
public int Height{ get; set; }
public int Width { get; set; }

public int Area()
{
return Width * Height;
}

public void Draw(Form form)
{
SolidBrush myBrush = new SolidBrush(System.Drawing.Color.Red);
Graphics formGraphics = form.CreateGraphics();
formGraphics.FillRectangle(myBrush, new Rectangle(0, 0, Width, Height);
}
}```

Typically, the above class is used by consuming client classes like these:

```/// <summary>
/// Consumes the RectangleShape */
/// </summary>
public class GeometricsCalculator
{
public void CalculateArea(RectangleShape rectangleShape)
{
int area = rectangleShape.Area();
}
}

/// <summary>
//// Consumes the RectangleShape */
/// </summary>
public class GraphicsManager
{
public Form form {get;set;}

public void DrawOnScreen(RectangleShape rectangleShape)
{
rectangleShape.Draw(form);
}
}```

Solution

The next classes show how to separate the different responsibilities. Basic coding is used not taking other SOLID principles into account. It only just shows how to deal with the SRP principle. The `RectangleDraw `class consumes now a `RectangleShape `instance and a `Form `object.

```/// <summary>
/// Class calculates the rectangle's area.
/// </summary>
public class RectangleShape
{
public int Height { get; set; }
public int Width { get; set; }

public int Area()
{
return Width * Height;
}
}

/// <summary>
/// Class draws a rectangle on a windows form object.
/// </summary>
public class RectangleDraw
{
public void Draw(Form form, RectangleShape rectangleShape)
{
SolidBrush myBrush = new SolidBrush(System.Drawing.Color.Red);
Graphics formGraphics = form.CreateGraphics();
formGraphics.FillRectangle(myBrush,
new Rectangle(0, 0, rectangleShape.Width,rectangleShape.Height));
}
}```

The following code shows how to consume both classes:

```/// <summary>
/// Consumes the RectangleShape */
/// </summary>
public class GeometricsCalculator
{
public void CalculateArea(RectangleShape rectangleShape)
{
int area = rectangleShape.Area();
}
}

/// <summary>
/// Consumes the RectangleDraw and RectangleShape */
/// </summary>
public class GraphicsManager
{
public Form form { get; set; }

public void DrawOnScreen(RectangleDraw rectangleDraw, RectangleShape rectangleShape)
{
rectangleDraw.Draw(form, rectangleShape);
}
}```