Skip to content

Instantly share code, notes, and snippets.

@VictorHom
Created January 19, 2016 05:24
Show Gist options
  • Save VictorHom/bbd714470eac411343a6 to your computer and use it in GitHub Desktop.
Save VictorHom/bbd714470eac411343a6 to your computer and use it in GitHub Desktop.
examples
function Shipping() {
this.request = function (zipStart, zipEnd, weight) {
return 40; // assume calculations take place
}
};
function ModifiedShipping() {
this.login = function(credentials) {
console.log("setting credentials");
};
this.setStart = function(start) {
console.log("this is the start");
}
this.calculate = function(weight) {
console.log(weight);
return 40; // assume calculations take place
}
}
function ShippingAdapter(credentials) {
var shipping = new ModifiedShipping();
shipping.login(credentials);
return {
request : function(zipStart, zipEnd, weight) {
shipping.setStart(zipStart);
return shipping.calculate(weight);
}
}
}
var shipping = new Shipping(); // old way
var adapter = new ShippingAdapter("potus"); // new way
// can use both in the same way
/**
Provide a way to access elements of an aggregate object sequentially without
exposing how the accessing is done
we see this in our array methods Array.forEach()
client can just use the iterator and not have to be concerned with the underlyings
the iterator object has a reference to the collection and the current position
also has methods such as first, next, hasNext, reset, and each in some implementation
*/
var Iterator = function (items) {
this.index = 0;
this.items = items;
}
Iterator.prototype = {
first : function() {
this.reset();
return this.next();
},
next : function() {
return this.items[this.index++];
},
hasNext : function() {
return this.index <= this.items.length;
},
reset : function() {
this.index = 0;
},
each : function(callback) {
for (var item = this.first(); this.hasNext(); item = this.next()) {
callback(item);
}
}
}
var items = ["ITERATORS" , "ARE" , "AWESOME"];
var iter = new Iterator(items);
// can do this
for (var item = iter.first(); iter.hasNext(); item = iter.next()) {
console.log(item);
}
// and this
iter.each(function(item) {
console.log(item);
})
var Singleton = (function () {
var instance;
function createInstance() {
return this;
}
return {
getInstance: function () {
if (!instance) {
instance = new createInstance();
}
return instance;
}
};
})();
var instance1 = Singleton.getInstance();
var instance2 = Singleton.getInstance();
console.log(instance1 === instance2);
/**
* pro :
* - only need one instance, like a logger
* - instantiate only a single object for shared state
* - it provides a way to close off private variables from the outside
* con:
* - you have this global all powerful object that can do too much
* - the object controls its own creation (you might try instantiating the object and not know that it was already made)
* - testing can be hard. Unit tests are now dependent on previous results when needs to be independent
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment