这是一个工作的java代码,用于提供一个基本引擎类,它处理各种游戏使用的大量引擎实现的余额监听器注册。例如会有一个演示引擎,它维护演示游戏的演示平衡和相同引擎的现金版本,从后台获取余额等。这里的关键不是实际的java,而是如何实现这种模式在JavaScript中。我尝试了大约30种不同的方法,包括使用John Resigs“简单的JavaScript继承”和“JavaScript:权威指南”中定义的extend()糖,使用各种模块模式,使用那个=这个等等。为这个问题而努力。
这是工作的java代码:
File Engine.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书籍)。我假设我可以使用合成而不是尝试继承,但这限制了语言的使用以及可以实现的模式。
编辑:这是Shaun West的答案的工作版本。见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();
答案 0 :(得分:1)
对于来自Java的人来说似乎很奇怪,在你的尝试中你会想要改变这个:
DemoEngine.prototype = Engine;
对此:
DemoEngine.prototype = new Engine();
如果您想了解更多信息,这个答案非常好:What is the 'new' keyword in JavaScript?