Currently I have a series of three classes related to a game. I've previously made games using Unity where you access components such as the camera using functions accessible throughout all code. My current setup, however, relies on instances of each class being sent across the other classes. See the following outtake:
class World{
};
class Game{
Camera* camera;
World* world;
};
class Camera{
Game* game;
World* world;
};
Is this a result of bad design or are there times when a setup like this is justified?
Should I rather use static classes over my current setup since it would clarify the code and no more than one instance of each class is ever used?
What are the possible downsides of using static classes over instances?
Edit: Why they need to access each other.
Why Camera needs World: The camera needs access to the world since the world need to be rendered from the perspective of the camera. The camera triggers a render of the world depending on what it sees. Rendering is triggered from various methods in the camera such as when it moves.
When the camera is drawn it draws what it sees, such as the world. To draw the world the camera needs access to the world.
Why Camera needs Game: Game has values such as FPS which Camera use to display an overlay of debugging information.
In general, in Objective-C, a reference to an instance is a pointer and the name of the data type of what's at the far end of that pointer is the name of the instance's class.
Address of static pointer is constant throughout the execution of program. But where it points-to can be modified. A static pointer can be used to implement a function that always returns the same buffer to the program. This can be helpful in serial communication.
Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static. Another reason for making functions static can be reuse of the same function name in other files.
A class can have static members and a static initialization block, which is very similar to the instantiated objects with their members and constructors. The difference is, that there is only one static class and the static members only exist once, whereas, there may be any number of instantiated objects.
Three classes which are that tightly coupled does suggest some questionable design choices. Try to change your code so that, for example, the Camera
gets a pointer or reference to World
passed in only the methods where it actually needs to deal with World
. Also consider whether Camera
and World
actually need a pointer to Game
. Conceptually it would make more sense if Game
has a World
and has a Camera
, instead of all three objects being owned by someone else (who)?
The relationship between Game
and Camera
still only suggest that you should pass Game
, or even better, relevant data FROM Game
as method arguments to the Camera
draw method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With