[Openfire plug-in Development] intermediary mode in group chat

Source: Internet
Author: User

Original link: http://www.hechunchen.info /? P = 54

 

Intermediaries in life are very common, such as United Nations organizations that maintain world peace, housing agencies, and overseas agencies. So what is the intermediary model in the design model? Group chat in openfire is a typical example. We useSimplified group chat ModelTo describe the mediator mode.

Our consideration is that we add an intermediary-chat room between users. In this way, the coupling between users will be reduced without mutual reference. They only need to interact with the intermediary independently. The structure is as follows:

The following is the key code. Interface ichatroom. Java:

package mediator;public interface IChatroom{void forward(User from, User to, String message);}

 

Chatroom. Java:

package mediator;public class Chatroom implements IChatroom{@Overridepublic void forward(User from, User to, String message){// TODO Auto-generated method stubif(to!=null){to.receive(from, message);}}}

 

Abstract class user. Java:

package mediator;public abstract class User{IChatroom room;public User(IChatroom room){this.room = room;}public abstract void send(User to, String message);public abstract void receive(User from, String message);}

 

Maleuser. Java:

Package mediator; public class maleuser extends user {public maleuser (ichatroom room) {super (room); // todo auto-generated constructor stub} @ overridepublic void receive (user from, string message) {// todo auto-generated method stubsystem. out. println ("handsome guy I received:" + message) ;}@ overridepublic void send (user to, string message) {// todo auto-generated method stubroom. forward (this, to, message );}}

 

Femaleuser. Java:

Package mediator; public class femaleuser extends user {public femaleuser (ichatroom room) {super (room); // todo auto-generated constructor stub} @ overridepublic void receive (user from, string message) {// todo auto-generated method stubsystem. out. println (" I received:" + message) ;}@ overridepublic void send (user to, string message) {// todo auto-generated method stubroom. forward (this, to, message );}}

 

Test class testclient. Java:

Package mediator; public class testclient {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubichatroom room = new chatroom (); femaleuser HCC = new femaleuser (room); maleuser FQ = new maleuser (room ); maleuser GQ = new maleuser (room); maleuser Lp = new maleuser (room); // send a message to the male GQ. send (FQ, "have you eaten? "); // Send a message to a female LP. Send (HCC," I didn't eat ");}}

 

To sum up, mediator changesCoupling Mode.

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.