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

时间:2020-12-29 08:22:11

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

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

原文地址:http://www.2cto.com/kf/201310/249266.html