开发者

Sending a message between 2 objects

开发者 https://www.devze.com 2023-04-13 03:39 出处:网络
I have objects A and B that need to send and receive开发者_运维技巧 messages from each other. For example:

I have objects A and B that need to send and receive开发者_运维技巧 messages from each other.

For example:

I click, this causes A to send a message to B. B does something with it then sends A another message as a reply.

Right now the way I have it is, A has a B* and B has an A* and they call each other's sendMessageTo method to communicate.

Is there a better way to do this sort of thing?

Basically, the server sends a message or the local game sends a message which a polymorphic mediator takes in. The mediator either gets the message from the server or local game. The mediator passes it on to the client which does whatever with it. The idea is that the mediator should not be doing logic for the client hence message passing.


I think you need to implement Listener/Observer pattern. from Wikipedia:

The observer pattern (a subset of the publish/subscribe pattern) is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems.

For more ref:

http://en.wikipedia.org/wiki/Observer_pattern

http://www.codeproject.com/KB/cpp/CppEvents.aspx

http://www.codeproject.com/KB/architecture/observer.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号