Search code examples
c#solid-principlesopen-closed-principle

How to calculate area using Open closed principle C#


I am working with Open Closed principle of SOLID in C#. I have abstract class Shape which i want to used to calculate the area of different shapes. How to call areaCalculator class and how to pass different shapes. Here is my code.

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

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

public class AreaCalculator
{
    public double TotalArea(Shape[] shapes)
    {
        double area = 0;
        foreach (var objShapes in shapes)
        {
            area += objShapes.Area();
        }
        return area;
    }
}

I want to call areaCalculator class to calculate the area.

AreaCalculator _obj = new AreaCalculator();
            Shape[] _shapes = new Shape[2];
            var _result = _obj.TotalArea(_shapes);
            Console.WriteLine(_result);
            Console.ReadLine();

Solution

  • You need to create the rectangle objects and set their height and width for the calculation. If not the _shapes list is empty. Find below a sample of working code.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ShapesStacjOverflow {
    
    
        public abstract class Shape {
            public abstract double Area();
        }
    
        public class Rectangle : Shape {
            public double Height { get; set; }
            public double Width { get; set; }
            public override double Area() {
                return Height * Width;
            }
        }
    
        public class AreaCalculator {
            public double TotalArea(Shape[] shapes) {
                double area = 0;
                foreach (var objShapes in shapes) {
                    area += objShapes.Area();
                }
                return area;
            }
        }
        class Program {
            static void Main(string[] args) {
                AreaCalculator _obj = new AreaCalculator();
                Shape[] _shapes = new Shape[2];
                Rectangle rectangle1 = new Rectangle {
                    Width = 2,
                    Height = 3
                };
                Rectangle rectangle2 = new Rectangle {
                    Width = 1,
                    Height = 1
                };
                _shapes[0] = rectangle1;
                _shapes[1] = rectangle2;
    
                var _result = _obj.TotalArea(_shapes);
                Console.WriteLine(_result);
                Console.ReadLine();
            }
        }
    }
    

    Returning 7 as a result. If you want to create other child shapes, those should override the Area() method, so for each of the objects created in the list, the corresponding Area() method would be applied. Hope that helps.