diff --git a/chaining/index.html b/chaining/index.html index d5d208f..975b028 100644 --- a/chaining/index.html +++ b/chaining/index.html @@ -1,49 +1,50 @@ - - Project Template - - - - - - - - - - - - - - - - - - - - - -
-

{{vm.title}}

-

- The chaining pattern is an extremely useful way to perform continuous method calls on an object in JavaScript. -

- -

- This pattern is widely used in many different libraries and frameworks for JavaScript. -

- - -
-
-

Starting with a value of 5, Chained methods: add(2), multiply(5), subtract(7) -> Result should be 28

- -
- -

Result

-

{{vm.result}}

-
- + + Project Template + + + + + + + + + + + + + + + + + + + + + +
+

{{vm.title}}

+

+ The chaining pattern is an extremely useful way to perform continuous method calls on an object in JavaScript. +

+ +

+ This pattern is widely used in many different libraries and frameworks for JavaScript. +

+ + + +
+
+

Starting with a value of 5, Chained methods: add(2), multiply(5), subtract(7) -> Result should be 28

+ +
+ +

Result

+

{{vm.result}}

+
+ \ No newline at end of file diff --git a/chaining/js/controllers.js b/chaining/js/controllers.js index ed7bd0d..58a7720 100644 --- a/chaining/js/controllers.js +++ b/chaining/js/controllers.js @@ -2,16 +2,32 @@ 'use strict'; function Calculator(start){ - var total = start || 0; + if (this instanceof Calculator) { + var _total = start || 0; + this.add = function(x) { _total += x; return this;} + this.sub = function(x) { _total -= x; return this;} + this.mul = function(x) { _total *= x; return this;} + this.div = function(x) { _total /= x; return this;} + + Object.defineProperty(this, "Total", {get: function () { + return _total; + }, + set: function (newValue) { + _total = newValue; + + }, + enumerable: false, + configurable: true}); - this.add = function(x) { total += x; return this;} - this.sub = function(x) { total -= x; return this;} - this.mul = function(x) { total *= x; return this;} - this.div = function(x) { total /= x; return this;} + } + else { + return new Calculator(start); + } - this.get = function() { return total; } } + + var controllerId = "mainCtrl"; angular.module('patterns.controllers').controller(controllerId, ['$scope', mainCtrl]); @@ -31,7 +47,7 @@ vm.process = function(){ var calculator = new Calculator(5).add(2).mul(5).sub(7); - vm.result = calculator.get(); + vm.result = calculator.Total; } } }()); \ No newline at end of file diff --git a/visitor/index.html b/visitor/index.html index 3b564f6..a9d2daf 100644 --- a/visitor/index.html +++ b/visitor/index.html @@ -36,7 +36,10 @@

{{vm.title}}

With the modularity of the pattern, we're able to run N visitors against our data, giving us a lot of future extensibility for our system.

- + Visitor Uml +

+ The UML Diagram is above +

diff --git a/visitor/js/controllers.js b/visitor/js/controllers.js index 5950411..1992a85 100644 --- a/visitor/js/controllers.js +++ b/visitor/js/controllers.js @@ -2,51 +2,63 @@ 'use strict'; var Product = function(name, price, discount, sale_price){ - var self = this; + if (this instanceof Product) { + this.accept = function(visitor){ + visitor.visit(this); + }; - this.accept = function(visitor){ - visitor.visit(self); - }; + this.setDiscount = function(dis){ + discount = dis; + }; - this.getName = function(){ - return name; - }; - this.getPrice = function(){ - return price; - } - - this.setDiscount = function(dis){ - discount = dis; - }; + this.getDiscount = function(){ + return discount; + }; - this.getDiscount = function(){ - return discount; - }; + this.getDiscountPercentStr = function(){ + if(discount){ + return (discount * 100) + "%"; + } + } - this.getDiscountPercentStr = function(){ - if(discount){ - return (discount * 100) + "%"; + this.getDiscountAmount = function(){ + if(discount){ + return (price * this.getDiscount()).toFixed(2); + } } - } - this.getDiscountAmount = function(){ - if(discount){ - return (price * self.getDiscount()).toFixed(2); + this.getSalePrice = function(){ + if(discount){ + return (price * (1 - this.getDiscount())).toFixed(2); + } } - } + this.getName = function(){ + return name; + }; - this.getSalePrice = function(){ - if(discount){ - return (price * (1 - self.getDiscount())).toFixed(2); + this.getPrice = function(){ + return price; } + } + else { + return new Product(name,price,discount, sale_price); + } + + } var Discount = function(){ - this.visit = function(prod){ - prod.setDiscount(0.10); + if(this instanceof Discount) { + this.visit = function (prod) { + prod.setDiscount(0.10); + } + } + else { + return new Discount(); } + } var controllerId = "mainCtrl"; diff --git a/visitor/umldiagram.gif b/visitor/umldiagram.gif new file mode 100644 index 0000000..00f369c Binary files /dev/null and b/visitor/umldiagram.gif differ