Skip to content
Browse files

Initial

  • Loading branch information...
1 parent 25b62ac commit 985418cce34bf4e2676b2fddec9eec7adfaaa663 @wscherphof committed Jun 20, 2012
Showing with 65 additions and 1 deletion.
  1. +11 −0 Controller.js
  2. +21 −0 Model.js
  3. +1 −1 README.md
  4. +29 −0 TransferMoneyContext.js
  5. +3 −0 View.js
View
11 Controller.js
@@ -0,0 +1,11 @@
+var Model = require('./Model');
+var TransferMoneyContext = require('./TransferMoneyContext');
+
+exports.transfer = function (amount, source, destination) {
+ var sourceAccount = Model.Account.findByNumber(source);
+ var destinationAccount = Model.Account.findByNumber(destination);
+ var context = new TransferMoneyContext(amount, sourceAccount, destinationAccount);
+ context.enact();
+};
+
+
View
21 Model.js
@@ -0,0 +1,21 @@
+function Account () {
+ this.number = null; // hack
+ this.balance = 1000; // hack
+}
+Account.prototype.increaseBalance = function (amount) {
+ this.balance += amount;
+};
+Account.prototype.decreaseBalance = function (amount) {
+ this.balance -= amount;
+};
+Account.prototype.log = function (message) {
+ console.log({account: this, message: message});
+};
+
+exports.Account = {
+ findByNumber: function (number) {
+ var account = new Account();
+ account.number = number; // hack
+ return account;
+ }
+};
View
2 README.md
@@ -1,4 +1,4 @@
dciBank
=======
-The Bank example of DCI, in JavaScript
+The Bank example of DCI, in JavaScript.
View
29 TransferMoneyContext.js
@@ -0,0 +1,29 @@
+function withTransferMoneySource () {
+ this.transferTo = function (amount, sink) {
+ this.decreaseBalance(amount);
+ sink.transferFrom(amount, this);
+ this.log({out: amount, sink: sink.number});
+ };
+}
+
+function withTransferMoneySink () {
+ this.transferFrom = function (amount, source) {
+ this.increaseBalance(amount);
+ this.log({in: amount, source: source.number});
+ };
+}
+
+function TransferMoneyContext (amount, sourceAccount, destinationAccount) {
+ this.amount = amount;
+ this.source = sourceAccount;
+ this.sink = destinationAccount;
+
+ withTransferMoneySource.call(this.source);
+ withTransferMoneySink.call(this.sink);
+}
+
+TransferMoneyContext.prototype.enact = function () {
+ this.source.transferTo(10, this.sink);
+};
+
+module.exports = TransferMoneyContext;
View
3 View.js
@@ -0,0 +1,3 @@
+var controller = require('./Controller');
+
+controller.transfer(10, '5896495', '8743353');

0 comments on commit 985418c

Please sign in to comment.
Something went wrong with that request. Please try again.