哪种设计模式适合可数对象? [英] Which design pattern is appropriate for countable objects?

查看:23
本文介绍了哪种设计模式适合可数对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个无法再次解决的问题.作为最近熟悉设计模式但尚未处理它们的初级开发人员,我需要您的帮助以找到合适的.

问题是:

<块引用>

  1. 我想借助抽象类或接口来概括对象.

  2. 我想计算派生类的每个实例,因为我需要限制它们的数量.

例如在海战"中我使用的游戏类:SeaObject ---->我的,船,Kreyser.水雷、船只等数量有限

我尝试在基类中使用静态字段count",但我不明白如何在派生中以正确的方式使用它.因为我可以再次复制和粘贴静态字段.我怀疑它不好:[.

我同意我使用抽象类 SeaObject 的决定不太正确.如果有更好的选择,我会使用它.

请不要对我严格,我是这里的新人.感谢您的帮助.

解决方案

检查对象池模式.它基本上是一个工厂,对它所创造的东西进行计数.

I have a problem that I can't solve again. As junior developer that have recently got acquainted with design patterns and doesn't deal with them yet, I need your help to find appropriate.

The issue is:

  1. I want to generalize objects with help of an abstract class or interface.

  2. I want to count every instance of derived class, because I need to limit their number.

For example in "Sea Battle" game I use classes: SeaObject ----> Mine, Boat, Kreyser. The quantity of mines,boats,etc are limited

I have tried to use static field 'count' in base class and I don't understand how to use it in the right way in derived. Because all that I could just copy and paste the static fields again. I suspect that it is not good :[.

I agree that my decision to use abstract class SeaObject is not quite right. If the better choice exists I would use it.

Please, don't be strict to me, I am a newcomer here. Thank you for any help.

解决方案

Check out object pool pattern. It's basically a factory which keeps count of the things it creates.

这篇关于哪种设计模式适合可数对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆