Skip to main content

I am working on making a Pong clone as a practice project. I have a PongStatePongState class with members like "Ball"Ball, "Paddle"Paddle, "Board"Board and "Score"Score. I want to structure my code so that each object doesn't have to know how to render itself, but a rendererPaddleRenderer class will do that. So I also want to create a PongStateRenderer class. The PongStateRendererPongStateRenderer class, which may use other classes like "PaddleRenderer"PaddleRenderer, as well.

My question is howHow should the PongStateRendererI let PongStateRenderer know about the internals of PongStatePongState so that it can render it? The "Ball"Ball object in PongStatePongState know's it's own position and size, does that mean "Ball"Ball has to be a public member variable so that PongStateRendererPongStateRenderer can also know the position? Or, or is there a better way to do this without totally breaking encapsulation? Or isPerhaps my architecture is totally wrong, and I should use a different method?

I'm working in C# as it says in the tag but a general answer that applies to many languages could also be helpful.

I am working on making a Pong clone as a practice project. I have a PongState class with members like "Ball", "Paddle", "Board" and "Score". I want to structure my code so that each object doesn't have to know how to render itself, but a renderer class will do that. So I also want to create a PongStateRenderer class. The PongStateRenderer class may use other classes like "PaddleRenderer" as well.

My question is how should the PongStateRenderer know about the internals of PongState so that it can render it? The "Ball" object in PongState know's it's own position and size, does that mean "Ball" has to be a public member variable so that PongStateRenderer can also know the position? Or is there a better way to do this without totally breaking encapsulation? Or is my architecture totally wrong and I should use a different method?

I'm working in C# as it says in the tag but a general answer that applies to many languages could also be helpful.

I am working on making a Pong clone as a practice project. I have a PongState class with members like Ball, Paddle, Board and Score. I want to structure my code so that each object doesn't have to know how to render itself, but a PaddleRenderer class will. I also want to create a PongStateRenderer class, which may use other classes like PaddleRenderer, as well.

How should I let PongStateRenderer know about the internals of PongState so that it can render it? The Ball object in PongState know's it's own position and size, does that mean Ball has to be a public member variable so that PongStateRenderer can also know the position, or is there a better way to do this without totally breaking encapsulation? Perhaps my architecture is totally wrong, and I should use a different method?

I'm working in C# as it says in the tag but a general answer that applies to many languages could also be helpful.

Source Link

How should renderer class relate to the class it renders?

I am working on making a Pong clone as a practice project. I have a PongState class with members like "Ball", "Paddle", "Board" and "Score". I want to structure my code so that each object doesn't have to know how to render itself, but a renderer class will do that. So I also want to create a PongStateRenderer class. The PongStateRenderer class may use other classes like "PaddleRenderer" as well.

My question is how should the PongStateRenderer know about the internals of PongState so that it can render it? The "Ball" object in PongState know's it's own position and size, does that mean "Ball" has to be a public member variable so that PongStateRenderer can also know the position? Or is there a better way to do this without totally breaking encapsulation? Or is my architecture totally wrong and I should use a different method?

I'm working in C# as it says in the tag but a general answer that applies to many languages could also be helpful.