如何防止使用对象池进行构造
我想为我的库使用对象池设计模式,这样用户就无法创建超过预定义数量的对象。
如何强制用户使用 Pool 类来获取实例,而不是资源的构造函数?
我无法将资源构造函数声明为私有,因为这样池类也将无法创建实例...
谢谢
I want to use the Object Pool design pattern for my library, so that the user cannot create more than a predefined number of objects.
How can I force the user to use the Pool class for acquiring an instance, instead of constructor of the resource?
I can't declare the resource constructor private because then the pool class won't be able to create the instance either...
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
池不需要与类本身分开,或者类可以组合在池中,等等。
实例将从对象的(独立的)池中检索,就像单例一样,而是一个任意数量的伊顿公学。
您也可以玩具有可见性的游戏,但如何做到这一点取决于语言,并且在我看来,可能不值得付出努力 - 使用类似单例的模式,但使用 n 个对象,而不是一个。
The pool doesn't need to be separate from the class itself, or the class could composite in a pool, etc.
Instances would be retrieved from the object's (self-contained) pool, like a singleton, but a how-ever-many-you-want-eton.
You could also play games with visibility, but how to do so depends on the language, and IMO, probably not worth the effort--use a singleton-like pattern, but with n objects, instead of one.
使用组合和保存集合的类。 IOW,您为它们提供了一个围绕集合的包装类型,用于调整可见性并处理对象创建。
如果您确实想进一步限制资源创建,您可以使用这种方法在没有友谊的情况下进行绑定(已看到 c++/friend 评论):
use composition and a class which holds the collection. IOW, you give them a wrapper type around the collection which adjusts the visibility and handles object creation.
If you really want to restrict resource creation even further, you can bind without friendship using this approach (having seen the c++/friend comments):