I wrote an article about the powerful enumeration feature a few days ago. A few of my friends told me that it is not intuitive to simply describe the case. Indeed, I am writing a case Article here to explain the previous article.
In this case, the message identifiers of a game server are simplified. As we all know in game development, the interaction between the client and the server requires many message types to be defined, and this message type needs to be continuously expanded. The message identifier encapsulates the message entity object based on the message encoding sent from the client for processing at the business logic layer. Here we use the simplified message identifier to demonstrate an enumeration application. (This is mainly used as an example of enumeration. Due to simplified code processing, some improper design is inevitable. Please forgive me)
An interface of the message entity class:
public interface IMsg {public void setMsgCode(int code);public void execute();public void readData(IoBuffer bufer);public void writerData(IoBuffer bufer);}
An abstract class of the message entity class:
public abstract class AbstractMsg implements IMsg{protected int msgCode;public AbstractMsg(int msgCode){this.msgCode = msgCode;}}
An Implementation class of the message entity:
Public class testmsg extends actmsg {public testmsg (INT msgcode) {super (msgcode);} private int data1; private int data2; @ overridepublic void setmsgcode (INT code) {This. msgcode = Code;} @ overridepublic void execute () {// Message Processing Method} @ overridepublic void readdata (iobuffer bufer) {This. data1 = bufer. getint (); this. data2 = bufer. getint () ;}@ overridepublic void writerdata (iobuffer bufer ){}}
For modular management, an interface is provided for enumeration types, and messages from one module can be registered to an enumeration class, which all implement this interface.
public interface IMsgCodeClass {public int getMsgCode();public Class<? extends AbstractMsg> getMsgClass();}
This is an enumeration class. One module can be an enumeration class. Messages under this module are registered under this enumeration class.
Public Enum msgcodeclassconstants implements imsgcodeclass {test (1001, testmsg. Class) // not much to list here; private int msgcode; private class <? Extends extends actmsg> msgclass; msgcodeclassconstants (INT msgcode, class <? Extends extends actmsg> msgclass) {This. msgcode = msgcode; this. msgclass = msgclass ;}@ overridepublic int getmsgcode () {return msgcode ;}@ overridepublic class <? Extends extends actmsg> getmsgclass () {return msgclass ;}}
Finally, the message identifier class:
public class MsgRecogniser {Map<Integer, Class<? extends AbstractMsg>> msges = new ConcurrentHashMap<Integer, Class<? extends AbstractMsg>>();public void init(){MsgCodeClassConstants[] contants = MsgCodeClassConstants.values();for(int i=0;i<contants.length;i++){msges.put(contants[i].getMsgCode(), contants[i].getMsgClass());}}public IMsg getMsg(int msgCode){IMsg msg = null;Class clazz = msges.get(msgCode);try {Constructor constructor = clazz.getConstructor(int.class);msg= (IMsg) constructor.newInstance(msgCode);} catch (Exception e) {e.printStackTrace();}return msg;}}
Message identifiers are often used in decoder. After the decoder receives the data, it first reads the message encoding, and then calls the identifier to obtain the message entity object according to the encoding, call the readdata method of the message object to uniquely store the client data in the message object.
To expand a message in the future, you only need to write the message entity class and register it in the enumeration class. :)