Introduction In March, I received an iPhone game project development. For this game, I learned the observer mode and used it in my game background in my game, the gamescene I designed serves as the upper-layer class of the game level.
Background
In my games, the gamescene I designed serves as the upper level of the game level. It inherits observable and uses gamerole to practice game roles, such as player and NPC. Gamerole implements the observer and uses the observer mode to update the role in the level.
CodeAs follows:
1. Class observable;
2. Class observer: Public nsobject
3 .{
4. Private:
5. // --- add private member ---//
6. Public:
7. // --- add public member ---//
8. Observer ();
9. Virtual ~ Observer ();
10. Virtual void Update () = 0;
11 .};
12. typedef set <Observer *> set;
13. Class observable: Public nsobject
14 .{
15. Private:
16. // --- add private member ---//
17. Set m_observers;
18. Public:
19. // --- add public member ---//
20. observable ();
21. Virtual ~ Observable ();
22. Void registerobserver (Observer * O );
23. Void removeobserver (Observer * O );
24. Void policyobservers ();
25 .};
26. Void observable: registerobserver (Observer * O)
27 .{
28. m_observers.insert (O );
29 .}
30. Void observable: removeobserver (Observer * O)
31 .{
32. m_observers.erase (O );
33 .}
34. Void observable: policyobservers ()
35 .{
36. Set: reverse_iterator index;
37. Set: reverse_iterator end = m_observers.rend ();
38. For (Index = m_observers.rbegin (); index! = End; index ++)
39 .{
40. (Observer *) (* index)-> Update ();
41 .}
42 .}