设计模式定义
我正在尝试制作一个简单的游戏。我有一个用于屏幕缓冲区的类、一个用于图像的类和一个用于精灵的类。我的问题是这样的:
由于每个精灵都需要“属于”某个图像(精灵只保存某个图像内的坐标以及对图像的指针/引用),我应该将图像类传递给精灵构造函数,如下所示:
Sprite spriteInstance(imageInstance, rect, ...);
或者让精灵由图像类的方法返回
Sprite spriteInstance = imageInstance.createSprite(rect, ...);
还是个人喜好问题。 (两者显然都有效,我只是想知道是否被认为是“正确”或“专业”方式。)
图像/屏幕关系也是如此:表面是由屏幕返回,还是图像通过屏幕。
我还没有尝试实现的第三个选项是拥有一个“Game”类,并执行以下操作:
Game gameInstance;
Image imageInstance = gameInstance.loadImage(path);
Sprite spriteInstance = gameInstance.newSprite(imageInstance, rect, ...);
如果推荐上面的第二/第三种方法,是否应该将精灵类构造函数设为私有,并将图像类作为“朋友”,还是公开?这种类型的方法是否有一个名称可供我查找以获取进一步的见解?这就是所谓的“工厂模式”吗?
谢谢
I'm trying to make a simple game. I have a class for the screen buffer, a class for the images, and a class for the sprites. My question is this:
Since every sprite needs to "belong" to some image (the sprite only holds the coordinates within some image, and a pointer/reference to an image), should I pass the image class to the sprite constructor, like so:
Sprite spriteInstance(imageInstance, rect, ...);
or have the sprites returned by a method of the image class
Sprite spriteInstance = imageInstance.createSprite(rect, ...);
or does it a matter of personal preference. (both obviously work, I'm just wondering if one is considered the "right" or "professional" way to do it.)
The same goes for the image/screen relationship: Should the surface be returned by the screen, or the image passed the screen.
A third option which I haven't tried to implement yet would be to have a "Game" class, and do this:
Game gameInstance;
Image imageInstance = gameInstance.loadImage(path);
Sprite spriteInstance = gameInstance.newSprite(imageInstance, rect, ...);
If the second/third methods above are recommended, should the sprite class constructor be made private with the image class as a "friend", or left as public? And does this type of method have a name that I can look up for further insight? Is this what is referred to as the "Factory pattern"?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎您在这里有两个不同的问题:在哪里放置基于图像创建精灵的逻辑,以及是否使用工厂方法。
我建议不要使用
imageInstance.createSprite
,除非图像知道存在某个名为 Sprite 的类确实有意义。如果可能的话,您希望保持单向可见性。因此,除非图像依赖于精灵,否则不要让它知道类Sprite
的存在。我可能会建议,
如果不了解有关 Game、Image 和 Sprite 的类(和职责)的更多信息,我就不能说太多了。
至于工厂方法,是的,疯狂吧。 《Effective Java》中有一篇很好的章节介绍了它们的优点。
It seems like you've got two separate questions here: where to put the logic for creating a sprite based on an image, and whether to use a factory method or not.
I would advise against
imageInstance.createSprite
unless it actually makes sense for an Image to know that there exists some class named Sprite. You want to keep visibility uni-directional if at all possible. Therefore unless an image depends on the sprite, don't let it even know the classSprite
exists.I might suggest
I can't say much more without knowing more about the classes (and responsibilities of) Game, Image, and Sprite.
As for factory methods, yeah go nuts. Effective Java has a good chapter on their merits.