java是所有静态方法还是单个实例?
有时,由于集合的存在,我会想到一个类的一个实例就足够了。我的Decoder类就是一个例子:
public class Decoder
{
private static final Decoder instance = new Decoder();
private final HashMap<Client, State> states = new HashMap<Client, State>();
private Decoder()
{
}
public Packet decode(Client client, ByteBuffer data)
{
return null;
}
}
但我在想,为什么不让它看起来像:
public class Decoder
{
private static final HashMap<Client, State> states = new HashMap<Client, State>();
public static Packet decode(Client client, ByteBuffer data)
{
return null;
}
}
两种设计有效地完成了相同的任务。这两者有实际区别吗?什么时候我会用一个而不是另一个?谢谢
共 (0) 个答案