¿ìËÙÒµÎñͨµÀ

Éè¼Æģʽ֮¹Û²ìÕß(Observer)ģʽÓëÆäC++ͨÓÃʵÏÖ£¨Ï£©

×÷Õß ØýÃû¼¼Êõ À´Ô´ ³ÌÐòÉè¼Æ ä¯ÀÀ ·¢²¼Ê±¼ä 2012-06-29

ÎÒÃÇÔÚ¡¶Éè¼Æģʽ֮¹Û²ìÕß(Observer)ģʽÓëÆäC++ͨÓÃʵÏÖ£¨ÖУ©¡·Ò»ÎÄÖиø³öÁËÒ»¸öÒÔC++ÓïÑÔʵÏÖµÄͨÓù۲ìÕßģʽ·½°¸¹Ç¼Ü¡£È»¶ø£¬Êµ¼ÊµÄ¹¤³ÌÏîÄ¿ÐèÇóÍùÍùÒª±ÈÀíÏë״̬¸´Ôӵö࣬´Ëƪ±ãÊÇÓë¶ÁÕßÒ»Æð̽ÌÖÔÚÏÖʵÊÀ½çÖпÉÄÜÓöµ½µÄ¸÷ÖÖ¼¬ÊÖÎÊÌâ¼°½â¾ö·½°¸¡£

ÎÒ°ÑĿǰΪֹÎÒËùÓöµ½µÄÎÊÌâÂÞÁÐÈçÏ£º

¸´ºÏÖ÷Ìâ

¶àÏß³Ì

¸üз½·¨Ð޸Ĺ۲ìÕßÁ´±í

½ÓÏÂÀ´ÎÒÃÇÒ»Ò»¸øÓèÌÖÂÛ¡£

£¨Ò»£©¸´ºÏÖ÷Ìâ

¿¼ÂÇGUIµÄ×é¼þÉè¼Æ£¬ÎÒÏ°¹ßÓÃWidgetÀà´ú±íÖ®£¬ËüÐèÒª´¦ÀíÐí¶àÓû§½»»¥ÒÔ¼°ÏµÍ³Ê¼þ£¬ÆäÖÐ×î³£¼ûµÄÓû§½»»¥Ê¼þÓÐÊó±ê¼°¼üÅÌʼþ¡£ÌÈÈô¼Ü¹¹Ê¦¾ö¶¨ÒÔʼþ¼àÌý·½Ê½Éè¼ÆÕû¸öUI¿ò¼Ü£¬ÄÇôWidget±ã¾ßÓÐÖ÷ÌâµÄ½ÇÉ«£¬ÏàÓ¦µÄ£¬Êó±ê¼°¼üÅÌʼþ±ãÊǹ۲ìÕß½ÇÉ«¡£Êµ¼ÊÉÏ£¬Ò»¸öÖ÷Ìâ¶ÔÓ¦¶àÖÖ£¨²»ÊǶà¸ö£©¹Û²ìÕßµÄÏÖÏóºÜÆձ顣

ÎÒÃǽèÖúÖÐƪËù¸øµÄ¹Û²ìÕßģʽ¹Ç¼ÜʵÏÖÕâÀàÓ¦Óá£

½èÖú¶à¼Ì³Ð»úÖÆ£¬ºÜÈÝÒ×°ìµ½£º

01.struct MouseListener {
02. void mouseMoved(int x, int y) {}
03.};
04.
05.struct KeyListener {
06. void keyPressed(int keyCode) {}
07.};
08.
09.class Widget : public BasicSubject<MouseListener>, public BasicSubject<KeyListener>{...};

Ìí¼Óʼþ¼àÌýÆ÷µÄα´úÂë´óÖ**çÏ£º

01.MouseListener mel;
02.KeyListener kel;
03.Widget w;
04.w.addObserver(mel);
05.w.addObserver(kel);

ΪÁËʹWidgetÌí¼Ó/ÒƳýʼþ******µÄ·½·¨¸ü¼ÓÓѺã¬ÎÒÃÇ¿ÉÒÔΪWidgetÌṩaddXXXListener/removeXXXListener ·½·¨£¬ÕâЩ·½·¨»á°Ñµ÷ÓÃת¸ø»ùÀà¡£ÓÐÁËÕâЩÏà¶Ô½ÏÓѺõĽӿں󣬻ùÀàµÄaddObserver/removeObserver½Ó¿Ú¶ÔÓû§ÒѾ­Ã»ÓÐÓÃÁË£¬ËùÒÔÎÒÃǿɸÄÓÃprotected¼Ì³Ð¡£×ÛºÏÆðÀ´£¬´úÂë¿´ÆðÀ´´óÖÂÏñÕâÑù£º

01.class Widget : protected BasicSubject<MouseListener>,protected BasicSubject<KeyListener>{
02. typedef BasicSubject<MouseListener> MouseSubject;
03. typedef BasicSubject<KeyListener> KeySubject;
04.public:
05. inline void addMouseListener(MouseListener &mel) {
06. MouseSubject::addObserver(mel);
07. }
08.
09. inline void removeMouseListener(MouseListener &mel) {
10. MouseSubject::removeObserver(mel);
11. }
12.
13. inline void addKeyListener(KeyListener &kel) {
14. KeySubject::addObserver(kel);
15. }
16.
17. inline void removeKeyListener(KeyListener &kel) {
18. KeySubject::removeObserver(kel);
19. }
20.
21. void handleMsg(int msg) {
22. if (msg == 0) {
23. MouseSubject::notifyAll(&MouseListener::mouseMoved, 1, 1);
24. } else if (msg == 1) {
25. KeySubject::notifyAll(&KeyListener::keyPressed, 100);
26. }
27. }
28.};

µ±È»£¬ÄãÒ²¿ÉÒÔ²»Ê¹Óü̳иĶøʹÓÃ×éºÏ¼¼ÊõʵÏÖ£¬ÕâÍêÈ«È¡¾öÓÚÄãµÄ°®ºÃ¡£×éºÏ°æ±¾µÄʵÏÖ´óÖÂÊÇÏñÕâÑùµÄ£º

01.class Widget {
02.public:
03. inline void addMouseListener(MouseListener &mel) {
04. ms_.addObserver(mel);
05. }
06.
07. inline void removeMouseListener(MouseListener &mel) {
08. ms_.removeObserver(mel);
09. }
10. ...
11.private:
12. BasicSubject<MouseListener> ms_;
13. BasicSubject<KeyListener> ks_;
14.};

£¨¶þ£©¶àÏß³Ì

ÌÈÈôÎÒÃǵÄÓ¦ÓóÌÐòÔËÐÐÔÚ¶àÏ̻߳·¾³ÖУ¬ÄÇÄã¿ÉÒª½÷É÷ÁË¡£ÊÔÏëÏß³ÌAÕýÔÚÌí¼Ó¹Û²ìÕßµÄͬʱÁíÒ»Ïß³ÌBÒ²ÊÔͼÌí¼Ó¹Û²ìÕß°É¡£ÎÒÃÇĬÈÏʹÓõÄÈÝÆ÷std::listÊÇÏ̷߳ǰ²È«µÄ£¬ËùÒÔÎÒÃǵÄBasicSubjcetÒ²»áÊÇÏ̷߳ǰ²È«µÄ¡£Òª½â¾ö´Ë

ÁèÖڿƼ¼×¨ÒµÌṩ·þÎñÆ÷×âÓᢷþÎñÆ÷Íйܡ¢ÆóÒµÓʾ֡¢ÐéÄâÖ÷»úµÈ·þÎñ£¬¹«Ë¾ÍøÕ¾£ºhttp://www.lingzhong.cn ΪÁ˸ø¹ã´ó¿Í»§Á˽â¸ü¶àµÄ¼¼ÊõÐÅÏ¢£¬±¾¼¼ÊõÎÄÕÂÊÕ¼¯À´Ô´ÓÚÍøÂç,ÁèÖڿƼ¼×ðÖØÎÄÕÂ×÷ÕߵİæȨ£¬Èç¹ûÓÐÉæ¼°ÄãµÄ°æȨÓбØҪɾ³ýÄãµÄÎÄÕ£¬ÇëºÍÎÒÃÇÁªÏµ¡£ÒÔÉÏÐÅÏ¢ÓëÎÄÕÂÕýÎÄÊDz»¿É·Ö¸îµÄÒ»²¿·Ö,Èç¹ûÄúҪתÔر¾ÎÄÕÂ,Çë±£ÁôÒÔÉÏÐÅÏ¢£¬Ð»Ð»!

·ÖÏíµ½£º ¸ü¶à

Copyright ©1999-2011 ÏÃÃÅÁèÖڿƼ¼ÓÐÏÞ¹«Ë¾ ÏÃÃÅÓÅͨ»¥Áª¿Æ¼¼¿ª·¢ÓÐÏÞ¹«Ë¾ All rights reserved

µØÖ·(ADD)£ºÏÃÃÅÈí¼þÔ°¶þÆÚÍûº£Â·63ºÅ701E£¨¶«ÄÏÈÚͨÅÔ£© Óʱà(ZIP)£º361008

µç»°£º0592-5908028 ´«Õ棺0592-5908039 ×ÉѯÐÅÏ䣺web@lingzhong.cn ×ÉѯOICQ£º173723134

¡¶ÖлªÈËÃñ¹²ºÍ¹úÔöÖµµçÐÅÒµÎñ¾­ÓªÐí¿ÉÖ¤¡·ÃöB2-20100024  ICP±¸°¸:ÃöICP±¸05037997ºÅ