前面解释了基本对象模板 IObject,TLBaseObjec,下面介绍重要的TLBaseModule。这三个类属于逐步继承的关系。在介绍TLBaseModule前,我们先看下消息类TLMsg,所有的对象通过统一的消息来传递信息。
TLMsg 消息类
源码;
public class TLMsg implements Serializable , Cloneable{
private static final long serialVersionUID = 2631590509760908280L;
protected String source; //消息产生源
protected String previous; //上一个处理消息的对象
protected String nowObject; //当前处理消息的对象
protected String destination; //消息目的地
protected HashMap<String, Object> args= new HashMap<>();
protected String action="";
protected String msgId ; //消息id,对象可根据消息id 查询信息路由表或信息表中获取相关处理信息
protected TLMsg nextMsg=null; //形成msg链,下一个msg
protected Boolean waitFlag =true; //异步put标志,默认非异步
public TLMsg( ){
}
public TLMsg(String action){
this.action = action;
}
public TLMsg(String action, HashMap args){
this.action = action;
this.args=args;
}
public TLMsg(String action, HashMap args, String msgId){
this.action = action;
this.args=args;
this.msgId=msgId;
}
public TLMsg(String action, String param, Object value){
this.action = action;
args.put(param,value);
}
public String getSource()
{
return source ;
}
public TLMsg setSource(String source)
{
this.source=source ;
return this;
}
public String getNowObject()
{
return nowObject ;
}
public TLMsg setNowObjcect(String nowObjcect)
{
this.nowObject=nowObjcect ;
return this;
}
public Boolean getWaitFlag()
{
return waitFlag ;
}
public TLMsg setWaitFlag(Boolean value)
{
this.waitFlag=value ;
return this;
}
public String getPrevious()
{
return previous ;
}
public TLMsg setPrevious(String previous)
{
this.previous=previous;
return this;
}
public String getDestination()
{
return destination ;
}
public TLMsg setDestination(String destination)
{
this.destination=destination ;
return this;
}
public HashMap getArgs()
{
return args ;
}
public TLMsg setArgs(HashMap args)
{
this.args=args ;
return this ;
}
public TLMsg addArgs(HashMap<String, Object>params)
{
if(params==null)
return this ;
for (String key : params.keySet()) {
this.args.put(key,params.get(key));
}
return this ;
}
public String getMsgId()
{
return msgId ;
}
public TLMsg setMsgId(String msgId)
{
this.msgId=msgId ;
return this;
}
public TLMsg getNextMsg()
{
return nextMsg;
}
public TLMsg setNextMsg(TLMsg nextMsg)
{
this.nextMsg=nextMsg ;
return this;
}
public String getAction()
{
return action;
}
public TLMsg setAction(String action)
{
this.action = action;
return this;
}
public Object getParam(String param)
{
return args.get(param) ;
}
public Object removeParam(String param)
{
return args.remove(param) ;
}
public TLMsg setParam(String param ,Object value)
{
args.put(param,value);
return this;
}
public TLMsg clear()
{
this.action ="";
this.args.clear();
this.msgId=null;
this.nextMsg=null;
this.destination=null;
this.waitFlag=true;
return this ;
}
public TLMsg copyFrom(TLMsg msg){
action =msg.getAction();
addArgs(msg.getArgs());
msgId=msg.getMsgId();
nextMsg=msg.getNextMsg();
destination=msg.getDestination();
waitFlag=msg.getWaitFlag();
source=msg.getSource();
previous=msg.getPrevious();
nowObject=msg.getNowObject();
return this;
}
public TLMsg copyTo(TLMsg nmsg){
if(nmsg ==null)
nmsg= new TLMsg();
nmsg.setAction(action);
nmsg.addArgs(args);
nmsg.setDestination(destination);
nmsg.setSource(source) ;
nmsg.setWaitFlag(waitFlag);
nmsg.setNextMsg(nextMsg);
nmsg.setPrevious(previous);
nmsg.setNowObjcect(nowObject);
return nmsg;
}
@Override
public Object clone() {
TLMsg copy = null;
try{
copy = (TLMsg) super.clone(); //浅复制
}catch(CloneNotSupportedException e) {
e.printStackTrace();
}
return copy;
}
消息类TLMsg 主要是对象属性,而方法基本都是对于属性的设置、存取,消息的复制、拷贝。
其中主要属性如下;
action:消息包含的指令,也就是希望对象的行为,名称不一定对应真实方法的名称,后续分析可看到。
args:行为参数,内部方法的参数
msgId:消息ID,每个消息可以设置ID,对象根据消息ID在内部消息表中取出对应的消息。
destination:消息目的。接收到消息的对象首先检查目的是否为自己,为自己则处理,不是自己的则转出到目的对象。
nextMsg:消息中可以链接下一个消息,形成消息链。对象依次执行各个消息
waitFlag:消息是否异步,默认同步消息,在TLBaseObject介绍提到过。
消息类TLMsg 是对象之间统一的消息对象类,统一方便了消息的传递和处理,包括通过网络传递。