Interaction between objects-Observer mode (3)

Source: Internet
Author: User
23.3 complete solution

To achieve interaction between objects,SunnyThe software company Developers decided to use the Observer Model to design Multiplayer Online Combat Games. Its basic structure22-4As shown in:

Figure22-4
Multiplayer Online Combat Game structure

In the Graph22-4Medium,AllycontrolcenterAct as the target class,ConcreteallycontrolcenterAct as a specific target class,ObserverAct as abstract observer,PlayerActs as a specific observer. CompleteCodeAs follows:

Import Java. util. *; // abstract observation class interface observer {Public String getname (); Public void setname (string name); Public void help (); // declare to support the ally method public void beattacked (allycontrolcenter ACC); // declare the method under attack} // team member class: Specific observer class player implements observer {private string name; public player (string name) {This. name = Name;} public void setname (string name) {This. name = Name;} Public String getname () {return this. name ;} // Support public void help () {system. Out. println ("stick to it," + this. Name + "to save you! ");} // Implementation of the method under attack. When the attack is triggered, notifyobserver () will be called to notify the ally public void beattacked (allycontrolcenter ACC) {system. out. println (this. name + "attacked! "); ACC. policyobserver (name) ;}// team control center class: Objective class allycontrolcenter {protected string allyname; // The team name protected arraylist <observer> players = new arraylist <observer> (); // defines a set for storing the public void setallyname (string allyname) {This. allyname = allyname;} Public String getallyname () {return this. allyname;} // registration method public void join (Observer obs) {system. out. println (obs. getname () + "add" + this. al Lyname + "team! "); Players. add (OBS);} // logout method public void quit (Observer obs) {system. out. println (obs. getname () + "quit" + this. allyname + "team! "); Players. remove (OBS);} // declare the abstract notification method public abstract void policyobserver (string name);} // The specific team control center class: target class concreteallycontrolcenter extends allycontrolcenter {public concreteallycontrolcenter (string allyname) {system. out. println (allyname + "Team established successfully! "); System. out. println ("----------------------------"); this. allyname = allyname;} // notification implementation method public void yyobserver (string name) {system. out. println (this. allyname + "Team emergency notice, Ally" + name + "attacked by the enemy! "); // Traverses the observer set and calls the support method for (Object Obs: players) {If (! (Observer) obs). getname (). inclusignorecase (name) {(observer) obs). Help ();}}}}

Write the following client test code:

 
Class client {public static void main (string ARGs []) {// defines the allycontrolcenter ACC; ACC = new concreteallycontrolcenter ("Jin Yong Qun Xia "); // define four observer objects: Observer player1, player2, player3, player4; player1 = new player ("Yang Guo"); ACC. join (player1); player2 = new player ("Ling Hu Chong"); ACC. join (player2); player3 = new player ("Zhang Wuji"); ACC. join (player3); player4 = new player ("Duan Yu"); ACC. join (player4); // a member is under attack player1.beattacked (ACC );}}

Compile and runProgramThe output result is as follows:

the JINYONG group Xia team is successfully established!

----------------------------

Yang once joined the JINYONG group Xia team!

ling yuchong joins the JINYONG clan!

Zhang Wuji joins the JINYONG clan!

Duan Yu joined the JINYONG clan!

Yang Guo was attacked!

An emergency notice was issued by the JINYONG group Xia team. Ally Yang was attacked by the enemy!

stick to it and let the fox rush to save you!

stick to it and Zhang Wuji will save you!

stick to it and Duan Yu will save you!

in this instance, achieves the association between two objects , when a game player player objects, beattacked () method is called, allycontrolcenter yyobserver () , in notifyobserv In the ER () method, other help () method of the Times New Roman "> player object. beattacked () of the player () yyobserver () method and help () method of Times New Roman "> player forms a linkage trigger chain, the execution sequence is as follows:

Player. beattacked () -->Allycontrolcenter. policyobserver () -->Player. Help ().

【Author】 Liu WeiHttp://blog.csdn.net/lovelion]

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.