How do I implement this java pattern in JavaScript (using inheritance)? -
here working java code provide base engine class handles balance listener registration, number of engine implementations used various games. e.g. there demo engine maintains demo balance demo game , cash version of same engine gets balance office etc. crux here not actual java, how implement kind of pattern in javascript. have tried 30 different ways it, including using john resigs "simple javascript inheritance" , extend() sugar defined in "javascript: definitive guide", using various module patterns, using that=this etc. none of worked problem.
here working java code:
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); }
file balancelistener.java
package com.test; public interface balancelistener { void update(double balance); }
file 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)); // game logic wonamount = math.round((random.nextdouble() * 10)) * stake; setbalance (getbalance() + wonamount); return wonamount; } }
file 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); } }
here plain (failed) attempt same thing working in javascript (see 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); // throws exception: uncaught typeerror: object [object object] has no method 'registerbalancelistener' engine.startgame(10, 25); } var game = new demogame();
obviously have no idea doing (despite reading several js books). assume use composition instead of trying inherit, limits use of language , patterns can implemented.
edit: here working version shaun west's answer. see 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); // throws exception: uncaught typeerror: object [object object] has no method 'registerbalancelistener' engine.startgame(10, 25); } var game = new demogame();
as odd may seem coming java, in attempt you'll want change this:
demoengine.prototype = engine;
to this:
demoengine.prototype = new engine();
this answer pretty if want more information: what 'new' keyword in javascript?
Comments
Post a Comment