I apologize for the potentially misleading and ambiguous title; I've tried to generalize it as best as I can.
I am currently working on an OpenGL project in C++. I wanted to wrap a couple of OpenGL functions into higher level objects. I ran into (what I thought was a unique) problem. I couldn't decide between having a Renderer
object be in charge of drawing a Drawable
object, or having a Drawable
object be in charge of drawing itself with Renderer
as a target.
renderer.render(Drawable) vs Drawable.draw(Renderer)
The more I thought about this problem the more I found scenarios where this problem arises.
Some more examples:
writer.write(File) vs file.write(Writer)
video.play(Screen) vs screen.play(Video)
object.toString() vs toString(Object)
Obviously, for the last one, Java uses object.toString()
It got me thinking though. Not every object overrides the toString
method, so doesn't that violate the interface segregation principle?
I realized this is a ubiquitous problem. I tried doing some research, but couldn't find the right words to actually search for this problem. That is why I am asking this here.
Is the solution purely subjective, based on design preference? Is the solution different depending on the context like the file
and video
problems? Please help me understand this more.