Jump to content
  • Advertisement
Sign in to follow this  
CoffeeMug

Object communication pattern

This topic is 5466 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Consider two classes, Window and Graphics. Generally, in order to create Graphics class, I need to pass it a pointer to the Window class. I can probably dig up dozens of examples where one class requires a pointer to another in order to properly operate, but the actual passing of the pointer seems to be a very dull task to the user. In these cases I become tempted to use a singleton class that contains a map of global variables that classes can just access from anywhere within the application at any time they wish. Assuming objects are created in a correct order, they will simply access necessary data without the user having to explicitly pass it around. Are there any other patterns that deal with this issue? How do you handle it in your work?

Share this post


Link to post
Share on other sites
Advertisement
The problem here is association. A Graphics object is associated with a specific Window instance; how do you specify this without explicitly passing the object? How does mere order of execution ensure proper association? Do we now begin to rely on hidden dependencies such as the new Graphics object being associated with the last created Window object?

I think there's a reason why there's no major pattern to solve this "problem."

Share this post


Link to post
Share on other sites
To be perfectly honest I'd say even making Graphics a singleton is abusing the pattern. Can you think of any good reason why the user shouldn't be able to make a second Graphics instance with a second window to have a second window rendering graphics in the same application? Consider using your engine for your level editor as an example.

Share this post


Link to post
Share on other sites
Quote:
Original post by CoffeeMug
... Generally, in order to create Graphics class, I need to pass it a pointer ... but the actual passing of the pointer seems to be a very dull task to the user. In these cases I become tempted to use a singleton class that contains a map of global variables that classes can just access from anywhere ... at any time ... Assuming objects are created in a correct order, ...


I thought I would point out some words to consider.

Share this post


Link to post
Share on other sites
Quote:
Original post by CoffeeMug
Consider two classes, Window and Graphics. Generally, in order to create Graphics class, I need to pass it a pointer to the Window class. I can probably dig up dozens of examples where one class requires a pointer to another in order to properly operate, but the actual passing of the pointer seems to be a very dull task to the user. In these cases I become tempted to use a singleton class that contains a map of global variables that classes can just access from anywhere within the application at any time they wish. Assuming objects are created in a correct order, they will simply access necessary data without the user having to explicitly pass it around.

Are there any other patterns that deal with this issue? How do you handle it in your work?
That's a mix between a facade and a factory.

Share this post


Link to post
Share on other sites
Well, there is no real reason to make Window and Graphics singletons but I only intended to use them as an example.

Consider the way OpenGL API is designed. It's effectively a state machine and once people get used to it, they find it extremely user friendly and natural. Instead of passing Graphics object everywhere, you simply set some global variable/singleton to its reference and assume every call after SetCurrentGraphics() refers to *that* graphics object. I don't think I've ever seen this done in object oriented code and I'm considering what effects it would have on application/library design.

Share this post


Link to post
Share on other sites
Quote:
Original post by CoffeeMug
Well, there is no real reason to make Window and Graphics singletons but I only intended to use them as an example.

Consider the way OpenGL API is designed. It's effectively a state machine and once people get used to it, they find it extremely user friendly and natural. Instead of passing Graphics object everywhere, you simply set some global variable/singleton to its reference and assume every call after SetCurrentGraphics() refers to *that* graphics object. I don't think I've ever seen this done in object oriented code and I'm considering what effects it would have on application/library design.
Because it destroys encapsulation, increases coupling, and functions are no longer bound to data.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!