The application of design pattern in game--observer mode (10)

Source: Internet
Author: User

The observer pattern may seem strange, but it can be said that the observer pattern is the most used mode in the game, even more frequent than the singleton pattern, and the code that wants to write good games must understand where in the game those places use the observer pattern, and who the subscribers of each observer are.

Most MMORPG players are a while loop that updates the viewer by updating Subscribers with these while loops. For example, we have a player's subscribers, each player is an observer, I just need to update the subscribers to update each observer. We each player has a lot of buff, our players are subscribers, each buff is the observer, so that when the player gets updated, we have each buff can get updates.

Let's look at the observer pattern, the observer pattern (sometimes called the publish-subscribe subscribe> pattern, the model-view view> mode, the source-listener listener> mode, or the slave mode), which is one of the software design patterns. In this mode, a target object manages all the observer objects that are dependent on it and proactively notifies when its own state changes. This is usually done by calling the methods provided by each observer.

Is the flowchart of the Observer pattern:

The code is as follows:

//MVC.cpp: Defines the entry point of the console application. //#include "stdafx.h"#include <iostream>#include <string>#include <sstream>#include <list>using namespace STD;classSubject;classObserver;classobserver{ Public: ~observer () {};Virtual voidUpdate () =0;};classBuff: Publicobserver{ Public: Buff (subject* host,intID): M_host (Host), m_id (ID) {}Virtual voidUpdate () {cout<<m_id<<"Buff Update"<<endl; } subject* M_host;intm_id;};classsubject{ Public: ~subject () {};voidAttach (observer* value) {observerlist.push_back (value); }voidDetach (observer* value) {observerlist.remove (value); }voidNotify () { for( list<Observer*>:: Iterator iter = Observerlist.begin (); Iter! = Observerlist.end ();        ++iter) {(*iter)->update (); }    } list<Observer*>Observerlist;};voidMain () {auto_ptr<Subject> Subject (NewSubject ());auto_ptr<Buff> Buff_one (NewBuff (Subject.get (),1));auto_ptr<Buff> Buff_two (NewBuff (Subject.get (),2));    Subject->attach (Buff_one.get ());    Subject->attach (Buff_two.get ()); Subject->notify ();}

The results are as follows:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The application of design pattern in game--observer mode (10)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.