你好BANQ,帮我看看访问都模式

我使用了Visitor模式,下面是一个得到Visitable的实现类的一个工厂,要根据不同的命令new 一个对象(这些对象就是Visitable的实现类),请问如果有100个命令,那我是不是要写100实现类,这里也会有很多if else,请教如果解决?谢谢!
public class VisitableFactory {
public static Visitable getVisitable(int commandId, StatSeqInfo record) {
Visitable v = null;
if (commandId == 1) {
v = new NewUserStat(record);
} else if (commandId == 81) {
v = new ChatRoomMoStat(record);
} else if (commandId == 82) {
v = new TroopMoStat(record);
} else if (commandId == 83) {
v = new PrivateMoStat(record);
}
return v;
}
}

这里使用command模式

你的意思是说仅这段代码使用command,还是把Visitor改成command

因为你需要根据commandId来进行不到对象的调用,这个场景就需要Command模式,你将commandId和对象class进行XML映射,当然这些class都会实现统一的command接口。

至于访问者模式,一般不轻易使用,能不用则不用。在这个应用中,目前没有看到需要使用的业务前提。

是这样的,commandId是别人传给我的,别人的代码不能改了,是他调用我的代码
public interface Analyser {
public void dispose(StatSeqInfo record);
}
这是他给我的接口,commandId是从record.getCommandId();得到
我在此实现在要根据commandId,创建一个Command的实现类,怎么利用XML文件,我想了一下,好像不行,还得用if else

而且这样问题还是没有解决,如果有100或多个commandId,则需要100个Command的实现类,还不如用Vistior:
public interface MoStatVisitor {
void visitRegister(NewUserStat regStat);
void visitChatroom(ChatRoomMoStat crmoStat);
void visitTroop(TroopMoStat tmoStat);
void visitPrivateSms(PrivateMoStat pStat);
void visitStat(Visitable visitable);
}
现在只有上面4个,如果再加一个commandid,则在这里加一个方法,如果用command,则得加一个Command的实现类,严重影响了性能

你的想法混乱,这个明显必须使用Command模式,Command模式性能很好,而且以后你有新的commandId,拓展非常方便。

command模式典型应用就是我们的web应用,html表单中form action=xxxx,action的值就是类似你的commandId字符串,发送到服务器段后,struts之类框架通过action值在xml配置中查找对应的Action,加载Action,执行缺省命令execute.