Dependency Inversion
Hello all, I had asked this before and was answered I need to implement dependency inversion
I have
Shape
abstract class and and interface which is responsible for drawing the shape.
The problem is: each shape uses position
to calculate coordinates, for circle it would be single Vector2
for square, rectangle etc it would be 2 and for triangle it would be 3.
So I need an interface, right
Now my problem is, how do I implement this in my Shape
class21 Replies
What attributes does a shape have? I can imagine you would
then you can draw them using an X and Y
the
Draw
method for each shape, uses its attributesI'm using IMGui
so, for circle I need single Vector2
for square I need
pMin
pMax
2 Vector2
s
for triangle I need 3 Vector2
sI wouldn't make a "coordinate calculator". It's hard to model that into the real world too
I definetely can do the calculation under
Draw
implementation of each shape
however I want to apply dependency inversion and be able to test my code
because Draw
returns nothing and I can't create a unit test for itwhat do you want to unit test exactly?
calculation of the coordinate for each shape
sometimes it helps to first write an unit test (which will not compile because the methods or classes don't exist yet), so you get a feel for what you want your structure to look like
that would be top-down programming
I understand, have already thought of the logic and had asked this question. Was responded I need dependency inversion which made/makes sense. I'm trying to practice SOLID so want to implement things correctly.
SOLID is some nice concepts, but for all of these principles it holds that they are more guidelines than rules. So apply them where you see use for this
sure I can just implement my calculation under
Draw
implementation but as I said I won't be able to testIf I were to overengineer this, I would have a method on a shape
CreateDrawRequests(Vector2 position)
which returns an object like
so the solution is abstracting the Calculator
and the thing that draws, can take in these "draw requests" and execute them
I'd expect something like
this is not the case though,
you see each shape has different position requirements,
because what is the exact calculation you'll be doing?
going e.g. from a midpoint x and y to a top-left x and y
so I have
I pass
300x500
for example, find the center, calculate Vectors for the shapeso for a rectangle, it would be e.g.
`
(not sure if those values are correct)
let's consider this is an interview question
You get
Vector2 position
as argument, create classes/interfaces for Shape where each shape takes different number of position arguments
Circle -> 1
Square -> 2
Triangle -> 3
MyWeirdShape -> N
What classes/interfaces would you create that applies SOLID
I already can just write the code and keep on going, as I said my goal is to apply SOLID principles 🙂hmm it's tricky doing something for the sake of doing it. Decoupling the calculation of a shape, and the call of the drawing would be something that seems SOLID, but is probably unproductive in the end
so what classes/interfaces are you already thinking of?
I will try again and see what I come up with
I think I will just create static methods for each shape
Not sure how else to deal with varying number of Vector2 s returned
Would generics help?
you could over engineer and have a
something like this
but this does not seem very maintainable or productive 😁 but it's fun
this links the shape to the class that holds the attributes
so you cannot make a
it wouldn't compile