Blmz
New Coder
I've found lots of similar posts about related questions, this reply by taylonr came close but didn't quite answer my scenario. Need some help with the Solid Principles. https://adevait.com/software/solid-design-principles-the-guide-to-becoming-better-developers
Assume I have an interface:
I then create 3 classes using this interface
I now want to add a int GetNumberOfSides() function (or some other function which is only relevant to some of my IShape's). Obviously this isn't relevant to the Circle class. If I then had a List<IShape> objects and wanted to iterate over call this function on relevant IShapes, how would you using object oriented design principles go about addressing this?
I could add a boolean to my IShape interface bool HasSides { get; set; } and run any logic of this, however I would need to cast to a specific class in order to access a GetNumberOfSides() function. I know this isn't right as it doesn't feel right, but I'm not sure how to do this.
I've thought about having another interface ISidedShape which inherited from IShape, but then back to iterating of my list how would I know which of the shapes had this specific method?
Any help would be much appreciated.
Thanks
Assume I have an interface:
Code:
public interface IShape
{
decimal GetArea();
}
I then create 3 classes using this interface
Code:
public class Rectangle : IShape
{
public decimal GetArea()
{
...
}
}
public class Triangle : IShape
{
public decimal GetArea()
{
...
}
}
public class Circle : IShape
{
public decimal GetArea()
{
...
}
}
I now want to add a int GetNumberOfSides() function (or some other function which is only relevant to some of my IShape's). Obviously this isn't relevant to the Circle class. If I then had a List<IShape> objects and wanted to iterate over call this function on relevant IShapes, how would you using object oriented design principles go about addressing this?
I could add a boolean to my IShape interface bool HasSides { get; set; } and run any logic of this, however I would need to cast to a specific class in order to access a GetNumberOfSides() function. I know this isn't right as it doesn't feel right, but I'm not sure how to do this.
I've thought about having another interface ISidedShape which inherited from IShape, but then back to iterating of my list how would I know which of the shapes had this specific method?
Any help would be much appreciated.
Thanks