Multi-player game observer Mode Analysis

Source: Internet
Author: User

 

I. Observer Mode

The observer mode is one of the most frequently used design modes. It is used to establish a dependency between an object and an object. When an object changes, other objects are automatically notified, other Objects will respond accordingly. In the observer mode, the changed object is called the observation object, and the notified object is called the observer. One observation object can correspond to multiple observers, and there can be no mutual relationship between these observers, you can add and delete observers as needed to make the system more scalable.

The observer pattern is defined as follows: Observer pattern: defines a one-to-many dependency between objects so that whenever the state of an object changes, related dependent objects are notified and automatically updated. The alias of observer mode includes publish/subscribe mode, model/view mode, source/listener mode, and dependents) mode. The observer mode is an object behavior mode.

 

2. multiplayer Combat Games

Sunny software company wants to develop a multiplayer online Combat Game (similar to World of Warcraft and Starcraft). In this game, multiple players can join the same team to form a consortium, when a member of a team is attacked by an enemy, a notification will be sent to all other allies. After receiving the notification, the Ally will respond.

Sunny software company developers need to provide a design scheme to achieve the association between team members. By analyzing the functional requirements of the System, developers of Sunny software company found that the interaction process between team members in the system can be described as follows: members of the Alliance are attacked --> send notifications to allies --> respond to allies.

If the system is designed according to the above ideas, the Alliance members must notify each of their allies when they are attacked, so each Alliance member must hold information of all other allies, this results in high system overhead. Therefore, sunny developers decided to introduce a new role, "Team control center", to maintain and manage the information of all members of each team. When a consortium member is attacked, a request for help message will be sent to the corresponding team control center. The team control center will then notify each ally one by one, and the Allies will respond again.

The structure is as follows:

 

Iii. Code Implementation

Target class: allycontrolcenter

Https://github.com/Aro710/Game_mul/blob/master/observer/demo/AllyControlCenter.java

Public abstract class allycontrolcenter {/*** team name */protected string allyname; /*** defines a set for storing team members */protected list <observer> players = new arraylist <observer> (); Public String getallyname () {return allyname ;} public void setallyname (string allyname) {This. allyname = allyname;}/*** Registration Method * @ Param player */Public void join (Observer player) {system. out. println (messageformat. format ("{0} join {1} Team", player. getname (), this. allyname); players. add (player);}/*** logout Method * @ Param player */Public void quit (Observer player) {system. out. println (); system. out. println (messageformat. format ("{0} quit {1} Team", player. getname (), this. allyname); players. remove (player);}/*** declare abstract Notification Method */public abstract void policyobserver (string name );}

Target class: concreteallycontrolcenter

Https://github.com/Aro710/Game_mul/blob/master/observer/demo/ConcreteAllyControlCenter.java

Public class concreteallycontrolcenter extends allycontrolcenter {public concreteallycontrolcenter (string allyname) {system. Out. println (messageformat. Format ("{0} Team component succeeded! ", Allyname); system. out. println (); system. out. println ("alert issued by the Control Center"); system. out. println ("---------------------------------"); this. allyname = allyname;} @ override public void yyobserver (string name) {system. out. println (); system. out. println (messageformat. format ("{0} Team emergency notice: Ally {1} is under attack. Please support it quickly! ", This. allyname, name); system. out. println (); // traverses the observer set and calls the support method of every ally (except yourself) for (Observer PLAYER: This. players) {If (! Player. getname (). inclusignorecase (name) {player. Help ();}}}}

 

Abstract observer: Observer

Https://github.com/Aro710/Game_mul/blob/master/observer/demo/Observer.java

Public interface observer {/*** get the observer name * @ return */string getname (); /*** set the observer name * @ Param name */void setname (string name);/*** declare support for Ally Methods */void help (); /*** declare attack method ** @ Param ACC */void beattacted (allycontrolcenter ACC );}

Specific observer: Player

Https://github.com/Aro710/Game_mul/blob/master/observer/demo/Player.java

Public class player implements observer {private string name; Public player (string name) {This. name = Name;} Public String getname () {return this. name;} public void setname (string name) {This. name = Name;}/*** support implementation of Ally Methods */Public void help () {system. out. println (messageformat. format ("stick to it, {0} to save you! ", This. name);}/*** The Implementation of the attack method. When the attack happens, the notifyobserver () Notification Method of the control center class of the team will be called () to notify the ally * @ Param ACC */Public void beattacted (allycontrolcenter ACC) {system. out. println (); system. out. println (messageformat. format ("{0} attacked", this. name); ACC. notifyobserver (this. name );}}

 

 

The observer mode has the following advantages:

(1) The observer mode separates the presentation layer from the data logic layer, defines a stable message update transfer mechanism, and abstracts the update interface, this allows different presentation layers to act as specific observer roles.

(2) The observer mode establishes an abstract coupling between the observer and the observer. To observe a target, you only need to maintain a set of abstract observers without understanding the specific observer. Because the observation targets are not closely coupled with the observer, they can belong to different abstract layers.

(3) The observer mode supports broadcast communication. The observer object will send notifications to all registered observer objects, simplifying the design of one or more systems.

(4) The observer mode meets the requirements of the "Open and closed principle". adding a new observer does not need to modify the original system code. When there is no association between the observer and the observed object, it is also convenient to add new observation targets.

 

Multi-player game observer Mode Analysis

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.