看看一段工廠模式的程式碼是否正確?

banq發表於2002-10-10
只有一個叫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 class="indent">

沒有concrete class

看看客戶端呼叫:

GameInterface game=gameFactroy.getGameInstance(gameClassPath);
gameClassPath是game的一個例項concrete class的類的名稱。



相關文章