存根类(stub) 是什么意思?有什么作用?

  存根类是一个类,它实现了一个接口,但是实现后的每个方法都是空的。 
   

   它的作用是:如果一个接口有很多方法,如果要实现这个接口,就要实现所有的方法。但是一个类从业务来说,可能只需要其中一两个方法。如果直接去实现这个接口,除了实现所需的方法,还要实现其他所有的无关方法。而如果通过继承存根类就实现接口,就免去了这种麻烦


这个在omco2.6版本中用户登录的session中的接口就有体现。




package com.utstar.omco.jnti.inc;


public interface ITBusStub extends IEngineHandle
{
	ITAclInterface getAclInterface();
	void setName(String name);
	String getUUID();
	int generateSID();
	int getSessionCount();
	ITServerSession getSession(int sid);
	ITServerSession[] getAllSession();
	int delSession(int sid, ITableRegChange ic);
	int _onRecvResult(Msg.MsgInfo msg);
	Msg.MsgInfo _onNeedExec();
	int _onRecvFromSession(ITServerSession s, Msg.MsgInfo msg);
	int _onRegister(Msg.ReguestRegister reg, ITableRegChange ic);
	void _onUpdateRegInfo(String src, ITableRegChange ic);
	int _onAddSession(ITServerSession s);
}


上面的类ITBusStub,就是一个stub类,它的作用主要是用于继承一个接口类,然后它的实现类只需要通过实现它这个接口就可以,

实现需要调用的方法。BusStub是它的实现类。


public class BusStub extends AbsEngineHandle implements ITBusStub,IMonitor
{
	public static interface MsgPriorityStrategy
	{
		public int onRecvResultPriority(Msg.MsgInfo msg);
		public int onRecvFromSessionPriority(ITServerSession s, Msg.MsgInfo msg);
	}
	
	public static class ResultPriorMsgPriorityStrategy implements MsgPriorityStrategy
	{
		public int onRecvResultPriority(Msg.MsgInfo msg)
		{
			return DefaultEngine.PRIO_HIGH;
		}

		public int onRecvFromSessionPriority(ITServerSession s, Msg.MsgInfo msg)
		{
			return DefaultEngine.PRIO_DEFAULT;
		}
	}
	
	AtomicInteger m_curSessionIdx = new AtomicInteger(1);

	IMsgQueue<Msg.MsgInfo> m_cmdQue = new MsgQueue<Msg.MsgInfo>("cmd");
	IMsgQueue<Msg.MsgInfo> m_resultQue = new MsgQueue<Msg.MsgInfo>("result");

	ConcurrentHashMap<Integer, ITServerSession> m_svc = new ConcurrentHashMap<Integer, ITServerSession>();

	NotifyReg m_reg = new NotifyReg();

	ITDispatch m_dispatch;

	ITAclInterface m_acl = ITAclInterface.s_defaultAcl;
	String m_uuid = UUID.randomUUID().toString();
	String m_name;
	
	MsgPriorityStrategy m_msgPriorityStrategy;
	
	LongStatPrp sp_cmdnum = new LongStatPrp("recv cmd",0);
	LongStatPrp sp_resultnum = new LongStatPrp("send result",0);
	LongStatPrp sp_notifynum = new LongStatPrp("send notify",0);
	
	private static final Log logger = LogFactory.getLog("comm");
	
	public BusStub(String name)
	{
		this(name, null);
	}

	public BusStub(String name, MsgPriorityStrategy msgPriorityStrategy)
	{
		m_name = name;
		m_msgPriorityStrategy = msgPriorityStrategy;
	}
	
	
	public String getName()
	{
		return m_name;
	}
	public void setName(String name)
	{
		m_name = name;
	}

	public String getUUID()
	{
		return m_uuid;
	}


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值