Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

PHP How can I design this better using OO and SOLID principles?

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:

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
 
Honestly, the simplest thing to do in my opinion would be to just handle your return types.
A circle does have an integer of # of straight sides. That number is 0. A circle has 0 straight sides. This means that your number of sides function *can* actually be used for the circle, but the answer is 0. You could check the return for === 0 for 0 sides or perhaps return false if a shape (like a circle) is not "eligible" for counting straight sides. It's up to you... as long as your code knows the returns, I see no reason why you couldn't just return 0 for "how many sides?" on a circle.

Layman's terms:
-> function counts how many fries are left on your plate
-> (triangle) pass a plate with 3 fries to your function, it returns 3
-> (square) pass a plate with 4 fries, it returns 4
-> (circle) pass an empty plate to your function, it returns === 0 (0 fries/sides)
-> (empty) pass nothing to your function, it returns === false (ineligible)
 
Back
Top Bottom