如何用各种类型的对象填充堆栈
使用 NSMutableArray ivar,我计划编写一个类似堆栈的类,并且应该能够从中检索各种类型的对象。
每当该堆栈由于几乎所有对象都已被检索而威胁要耗尽对象时,它应该通过从某种尽可能通用的“对象发射器”中获取新对象来自动将新对象推送到自身上。例如,应该有请求类返回 NSImages 或 NSString 或任何其他可以想象的类型的实例,这些类型应该放在堆栈上,但每个类都必须事先以单独的方式进行处理。
将这些对象发射类“插入”到我的堆栈类中最简单的模式是什么?堆栈类不需要知道它必须处理的对象类型,这是一些“帮助器类”可能设置它的地方 - 但我不知道它们的位置在哪里。我尝试使用 NSClassFromString 进行动态创建,但感觉不太对劲。
Using an NSMutableArray ivar, I plan to write a class that acts like a stack and which objects of various types should be able to be retrieved from.
Whenever that stack threatens to run out of objects because almost all have yet been retrieved, it should automatically push new objects onto itself by fetching them from some sort of as-generic-as-possible "object emitter". For example, there should be supplicant classes which return instances of NSImages or of NSString or any other imaginable types which should be put on the stack but each have to be treated in a separate way beforehand.
What would be the easiest pattern of "plugging in" those object emitting classes into my stack class? The stack class should not need to be aware of the object types it has to deal with, this is where some "helper classes" might set it - but I have no clue where their place would be. I tried Dynamic Creation using NSClassFromString but that just doesn't feel quite right.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSMutableArray 能够存储任何对象。与 Java 中的数据结构存储特定类的实例的泛型集合不同,Cocoa 允许您向 NSArray 添加任何内容。因此,您的 pop 方法可以简单地删除数组的最后一个对象,检查大小是否小于某个阈值,然后从供应商类请求新对象。
例如:
NSMutableArray is capable of storing any object. Unlike generic collections in Java, where data structures store instances of specific classes, Cocoa allows you to add anything to an NSArray. So, your pop method could simply remove the last object of the array, check to see if the size is smaller than some threshold, and then request new objects from supplier classes.
For example: