看看一段工厂模式的代码是否正确?

02-10-10 banq
    

只有一个叫GameFactroy的类:

public class GameFactroy {
	
    public static Logger logger = Logger.getLogger(GameFactroy.class);

    public GameFactroy() { }
    /**
     * Construct a game instance
     * @param _gameClassPath The game class path is stored in game.xml
     * @see GameKind
     * @return game instanc
     * @throws GameFactroyException
     */
    public GameInterface getGameInstance(String gameClassPath) throws GameFactroyException{
        try{
            Class c = Class.forName(gameClassPath);
            GameInterface game = (GameInterface)c.newInstance();
            logger.info("Susccess contruct a game instance,gameClassPath=" + gameClassPath);
            return game;
        }catch(Exception ex){
            logger.error("Can not construct a game instance,gameClassPath=" + gameClassPath + ",Error=" + ex);
            throw new GameFactroyException("Can not construct a game instance");
        }//end try-catch
    }
}
<p>

没有concrete class

看看客户端调用:

GameInterface game=gameFactroy.getGameInstance(gameClassPath);

gameClassPath是game的一个实例concrete class的类的名称。

    

cc
2002-10-14 09:31

有什么不对的地方么?我并没有看出来啊?

public static Logger logger = Logger.getLogger(GameFactroy.class);

这是干什么用的?

曙光
2002-10-14 10:48

加一个Hashtable来存实例

private java.util.Hashtable hashtable = new Hashtable();

GameInterface game = (GameInterface) hashtable.get(gameClassPath);
if(game == null) {
  try{
            Class c = Class.forName(gameClassPath);
            game = (GameInterface)c.newInstance();
            logger.info("Susccess contruct a game instance,gameClassPath=" + gameClassPath);
        }catch(Exception ex){
            logger.error("Can not construct a game instance,gameClassPath=" + gameClassPath + ",Error=" + ex);
            throw new GameFactroyException("Can not construct a game instance");
        }//end try-catch
}
            return game;
<p>

浆糊
2002-10-14 11:48

日志处理

banq
2002-10-14 13:17

这是一个工厂方法+动态载入。

4Go 1 2 3 4 下一页