0

Im following a tutorial based on SOLID principles but i fail to understand how to actually implement this method, here is the tutorial Link

I faily understand the concept of the code but i cant figure out how to actually call the AreaCalculator.Area method from my Main

How do i call the method to either calculate the Rectangle Area or the Circle Area? based on the code in the tutorial

Area(Circle with 10 radius);
Area(Rectangle with 10 width and 5 height);

Shape.cs

public abstract class Shape
{
    public abstract double Area();
}

Circle.cs

    public class Circle : Shape
    {
        public double Radius { get; set; }
        public override double Area()
        {
            return Radius * Radius * Math.PI;
        }
    }

Rectangle.cs

    public class Rectangle : Shape
    {
        public double Width { get; set; }
        public double Height { get; set; }
        public override double Area()
        {
            return Width * Height;
        }
    }

AreaCalculator.cs

public static double Area(Shape[] shapes)
        {
            double area = 0;
            foreach (var shape in shapes)
            {
                area += shape.Area();
            }

            return area;
        }

Thank you

3
  • 2
    Side note: interface (say, IShapable) instead of abstract class (Shape) will be more resilient. public static double Area(IEnumerable<IShapable> shapes) - note interface IEnumerable - is also more flexible Commented Apr 13, 2021 at 14:02
  • Talking about flexible, it could also be double Area(params Shape[] shapes) Commented Apr 13, 2021 at 14:18
  • @Cleptus I would prefer IEnumerable because Area is unlikely to be called with a single item, and IEnumerable would allow an existing collection to be used rather than potentially forcing a new array to be allocated. Commented Apr 13, 2021 at 14:21

2 Answers 2

2
var circle = new Circle { Radius = 10 };                  // Create a circle
var rectangle = new Rectangle { Width = 10, Height = 5 }; // Create a rectangle

var shapes = new Shape[] { circle, rectangle };           // Create array of shapes

double area = AreaCalculator.Area(shapes);                // Call Area method with array

If you only need the area of a single shape, the AreaCalculator is unneccesary; simply call the Area method on an individual shape:

double circleArea = circle.Area();
double rectangleArea = rectangle.Area();
Sign up to request clarification or add additional context in comments.

6 Comments

How do i call the method to either calculate the Rectangle Area or the Circle Area? Are you sure author needs the sum of this areas?
@AlexeyRumyantsev I've updated the answer; with a single shape using the AreaCalculator is unnecessary.
Thank you! It made me understand this example better!
@JohnathanBarclay Even AreaCalculator is unnecessary and can be easily replaced by short LINQ statement.
@ScreamoIsDead Did you come closer to understanding not how this example works, but to understanding open/closed principle?
|
1

You need to instantiate instances of each shape and pass those as an array to the AreaCalculator.Area() method. The key take away is the Area method will take any objects that extend Shape.

var circle = new Circle();
circle.Radius = 5;

var rectangle = new Rectangle();
rectangle.Width = 10;
rectangle.Height = 3;

var area = AreaCalculator.Area(new Shape[] {circle, rectangle});

Or more succinctly

AreaCalculator.Area(new Shape[]{
    new Circle() {Radius = 5},
    new Rectangle() {Width = 10, Height = 3}
});

3 Comments

Your second example won't compile, because the array cannot be implicitly typed.
@JohnathanBarclay good call, edited to make it compile. That what happens when you write code in an html textbox
Thank you! it helped me understand this a bit better:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.