0

下面是工作 java 代码,它提供了一个处理平衡侦听器注册的基本引擎类,用于各种游戏使用的许多引擎实现。例如,将有一个演示引擎维护演示游戏的演示余额和同一引擎的现金版本,该引擎从后台获取余额等。这里的关键不是实际的 java,而是如何实现这种JavaScript 中的模式。我已经尝试了大约 30 种不同的方法来做到这一点,包括使用 John Resigs 的“简单 JavaScript 继承”和“JavaScript:权威指南”中定义的 extend() 糖,使用各种模块模式,使用 that=this 等。为这个问题工作。

这是工作的java代码:

文件引擎.java:

package com.test;
public abstract class Engine {
    BalanceListener externalBalanceListener = null;
    double balance = 0;
    public void registerBalanceListener(BalanceListener balanceListener) {
        externalBalanceListener = balanceListener;
        balanceListener.update(balance);  // call once when first register
    }
    public double getBalance() {
        return balance;
    }
    protected void setBalance(double newBal) {
        if (newBal != balance) {
            balance = newBal;
            if (externalBalanceListener != null) {
                externalBalanceListener.update(newBal);
            }
        }       
    }
    public abstract double startGame(double stake, int numLines);
}

文件 BalanceListener.java

package com.test;
public interface BalanceListener {
    void update(double balance);
}

文件 DemoEngine.java

package com.test;
import java.util.Random;

public class DemoEngine extends Engine {
    public DemoEngine() {
        setBalance(10000);
    }
    public double startGame(double stake, int numLines) {
        double wonAmount;
        Random random = new Random();

        setBalance (getBalance() - (stake * numLines));

        // some game logic
        wonAmount = Math.round((random.nextDouble() * 10)) * stake;
        setBalance (getBalance() + wonAmount);          
        return wonAmount;
    }
}

文件 DemoGame.java

package com.test;

public class DemoGame {

    public class MyListener implements BalanceListener {
        public MyListener(){
        }
        public void update(double balance) {
            System.out.println("new balance: " + balance);
        }
    }

    public static void main(String[] args) {
        Engine engine = new DemoEngine();

        DemoGame demoGame = new DemoGame();

        BalanceListener balanceListener = demoGame.new MyListener();

        engine.registerBalanceListener(balanceListener);

        engine.startGame(10, 20);
    }
}

这是一个普通的(失败的)尝试让同样的事情在 JavaScript 中工作(见http://jsfiddle.net/fmX67/

function Engine() {
    this.balance = 0;
    this.externalBalanceListener;

    this.registerBalanceListener = function(l) {
            this.externalBalanceListener= l;
            this.externalBalanceListener(this.balance);
    };

    this.getBalance = function() {
        return this.balance;
    };

    this.setBalance = function (newBal) {
        if (newBal != this.balance) {
            this.balance = newBal;
            if (this.externalBalanceListener != undefined) {
                this.externalBalanceListener(newBal);
            }
        }
    };

};

function DemoEngine() {
    this.startGame = function(stake, numLines) {
        var won;

        setBalance(this.getBalance() - stake*numlines);
        won = Math.round(Math.random() * 10) * Stake;

        this.setBalance(this.getBalance() + won);

        return won;
    };
}

DemoEngine.prototype = Engine;

function DemoGame() {

    function balanceListener(balance) {
        console.log(balance);
    }

    var engine = new DemoEngine();

    engine.registerBalanceListener(balanceListener); // This throws an exception: Uncaught TypeError: Object [object Object] has no method 'registerBalanceListener'

    engine.startGame(10, 25);
}

var game = new DemoGame();

显然我不知道我在做什么(尽管阅读了几本 JS 书籍)。我假设我可以使用组合而不是尝试继承,但这限制了语言的使用以及可以实现的模式。

编辑:这是肖恩·韦斯特回答的工作版本。见 http://jsfiddle.net/fmX67/3/

function Engine() {
    this.balance = 0;
    this.externalBalanceListener;

    this.registerBalanceListener = function(l) {
            this.externalBalanceListener= l;
            this.externalBalanceListener(this.balance);
    };

    this.getBalance = function() {
        return this.balance;
    };

    this.setBalance = function (newBal) {
        if (newBal != this.balance) {
            this.balance = newBal;
            if (this.externalBalanceListener != undefined) {
                this.externalBalanceListener(newBal);
            }
        }
    };

};

function DemoEngine() {
    this.setBalance(1000);
    this.startGame = function(stake, numLines) {
        var won;

        this.setBalance(this.getBalance() - stake*numLines);
        won = Math.round(Math.random() * 10) * stake;

        this.setBalance(this.getBalance() + won);

        return won;
    };
}

DemoEngine.prototype = new Engine();

function DemoGame() {

    function balanceListener(balance) {
        console.log(balance);
    }

    var engine = new DemoEngine();

    engine.registerBalanceListener(balanceListener); // This throws an exception: Uncaught TypeError: Object [object Object] has no method 'registerBalanceListener'

    engine.startGame(10, 25);
}


var game = new DemoGame();
4

1 回答 1

1

对于来自 Java 的人来说,这可能看起来很奇怪,但在您的尝试中,您需要更改它:

DemoEngine.prototype = Engine;

对此:

DemoEngine.prototype = new Engine();

如果您想了解更多信息,这个答案非常好:JavaScript 中的“new”关键字是什么?

于 2013-07-17T15:41:32.143 回答