Objective C - 在对象内向对象传递消息
我对已在不同对象中实例化的消息传递对象有疑问。 具体来说,我希望将“游戏状态”对象与“GLview 对象”分开,并能够从另一个对象中调用属于每个对象的方法。 实现这一目标的最佳方法是什么?
提前致谢 ;)
I have a query about messaging objects which have been instantiated within a different object.
Specifically I want to keep my 'gamestate' object separate from my 'GLview Object' and be able to call methods belonging to each object from within the other.
What is the best way of achieving this?
Thanks in advance ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有多种方法可以实现这一目标。
最明显的是,您的超类可以使用引用其属性的属性(子对象,如您所描述的)来定义。例如,[[gamestate subObject] doSomething],其中“subObject”是指向您要发送消息的对象的合成属性。
如果您想要更松散的耦合,请考虑使用委托模式或注册通知。
您的嵌套对象可以定义委托协议,在适当的时候调用它的委托方法。如果另一个对象符合协议并被指定为委托,它将接收消息。
最后,对象可以注册通知。对象A可以发布通知(例如,“发生了一些事情”),而对象B可以注册通知(例如,当您收到“发生了一些事情”的通知时执行方法X)。
There are several ways of achieving this.
The most obvious is that your superclasses may be defined with properties referencing their properties (sub-objects, as you're describing). Eg, [[gamestate subObject] doSomething], where 'subObject' is a synthesized property pointing to the object you want to message.
If you want looser coupling, consider using a pattern of delegation or registering for notifications.
Your nestled objects may define a delegate protocol, calling it's delegate methods at appropriate times. If another object conforms to the protocol and is assigned as the delegate, it will receive the messages.
Finally, objects can register for notifications. Object A can post notifications (e.g., 'something happened'), and Object B can register to for notifications (e.g., perform method X when you're notified 'something happened').