© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
39 replies
eysidi

Dependency Inversion

Hello all, I had asked this before and was answered I need to implement dependency inversion

I have
Shape
Shape
abstract class and and interface which is responsible for drawing the shape.

public interface IShapeDrawer
{
    void Draw(Vector2 position);
}
public abstract class Shape : IShapeDrawer
{
    public abstract string Name; // Name to display

    public abstract void Draw(Vector2 position); // Interface to abstract
}
public interface IShapeDrawer
{
    void Draw(Vector2 position);
}
public abstract class Shape : IShapeDrawer
{
    public abstract string Name; // Name to display

    public abstract void Draw(Vector2 position); // Interface to abstract
}

The problem is: each shape uses
position
position
to calculate coordinates, for circle it would be single
Vector2
Vector2
for square, rectangle etc it would be 2 and for triangle it would be 3.

So I need an interface, right
public interface ICoordinateCalculator
{
    Vector2 CalculateCoordinate(Vector2 position);
}

public interface IMultipleCoodinateCalculator
{
    Vector2[] CalculateCoordinates(Vector2 position);
}

public CircleCoordinateCalculator : ICoordinateCalculator
{
    Vector2 CalculateCoordinate(Vector2 position) => position;
}
public interface ICoordinateCalculator
{
    Vector2 CalculateCoordinate(Vector2 position);
}

public interface IMultipleCoodinateCalculator
{
    Vector2[] CalculateCoordinates(Vector2 position);
}

public CircleCoordinateCalculator : ICoordinateCalculator
{
    Vector2 CalculateCoordinate(Vector2 position) => position;
}


Now my problem is, how do I implement this in my
Shape
Shape
class
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Dependency Inversion Principle, Dependency Injection
C#CC# / help
3y ago
❔ Help with Dependency Inversion Principle
C#CC# / help
3y ago
❔ Can someone help me understand this statement around dependency inversion with an example?
C#CC# / help
3y ago
Dependency injection
C#CC# / help
6mo ago