设计模式定义

发布于 2024-10-03 05:10:45 字数 773 浏览 0 评论 0原文

我正在尝试制作一个简单的游戏。我有一个用于屏幕缓冲区的类、一个用于图像的类和一个用于精灵的类。我的问题是这样的:
由于每个精灵都需要“属于”某个图像(精灵只保存某个图像内的坐标以及对图像的指针/引用),我应该将图像类传递给精灵构造函数,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℡Ms空城旧梦 2024-10-10 05:10:45

似乎您在这里有两个不同的问题:在哪里放置基于图像创建精灵的逻辑,以及是否使用工厂方法。

我建议不要使用imageInstance.createSprite,除非图像知道存在某个名为 Sprite 的类确实有意义。如果可能的话,您希望保持单向可见性。因此,除非图像依赖于精灵,否则不要让它知道类 Sprite 的存在。

我可能会建议,

Sprite s = Sprite.forImage(image, rect);

如果不了解有关 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 class Sprite exists.

I might suggest

Sprite s = Sprite.forImage(image, rect);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文