Skip to content

Instantly share code, notes, and snippets.

@sivagao
Last active December 19, 2015 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sivagao/6036975 to your computer and use it in GitHub Desktop.
Save sivagao/6036975 to your computer and use it in GitHub Desktop.
javascript & jQuery patterns text from book jQuery patterns<essential javascript design patterns for beginners> <javascript patterns><javascript design patterns><Learning.JavaScript.Design.Patterns>
/*
applications are made up of separate objects, which need a way to communicate among themselves.
when objects know too much about each other, and communicate directly.. means, too tight coupling.
in this pattern
independent objects(colleagues) dont communicate directly. but through a mediator object
*/
function Player(name){
this.points = 0;
this.name = name;
}
Play.prototype.play = function(){
this.points += 1;
mediator.played();
}
var scoreboard = {
element: document.getElementById('results'),
update: function(scope){
var i, msg = '';
for (i in score) {
if(score.hasOwnProperty(i)){
msg += '<p><strong>' + i + '<\/strong>: ';
msg += score[i];
msg += '<\/p>';
}
}
this.element.innerHTML = msg;
}
};
var mediator = {
// all the players
players: {},
// initialization
setup: function () {
var players = this.players;
players.home = new Player('Home');
players.guest = new Player('Guest');
},
// someone plays, update the score
played: function () {
var players = this.players,
score = {
Home: players.home.points,
Guest: players.guest.points
};
scoreboard.update(score);
},
// handle user interactions
keypress: function (e) {
e = e || window.event; // IE
if (e.which === 49) { // key "1"
mediator.players.home.play();
return;
}
if (e.which === 48) { // key "0" mediator.players.guest.play();
return;
}
}
};
mediator.setup();
window.onkeypress = mediator.keypress;
setTimeout(function(){
window.onkeypress = null;
alert('come over!');
}, 3000)
/*
proxy design pattern, one object acts as an interface to another object.
different from facade pattern(where all you have is convenience methods that combine several other method calls)
the proxy sit between them and protects the access to the object.
*/
/*
the strategy pattern enables you to select algorithms at runtime
the client of your code work with the same interface but pick from a number fo available algorithms
to handle their specific task depending on the context of what they are trying to do.
*/
validator.types.isNonEmpty = function(){
validate: function(value){
return value !== '';
},
instrcutions: 'this value cannot be empty'
};
var validator = {
// all available checks
types: {},
// error messages in the current
// validation session
messages: [],
// current validation config
// name: validation type
config: {},
// the interface method
// `data` is key => value pairs
validate: function (data) {
var i, msg, type, checker, result_ok;
// reset all messages
this.messages = [];
for (i in data) {
if (data.hasOwnProperty(i)) {
type = this.config[i];
checker = this.types[type];
if (!type) {
continue; // no need to validate }
if (!checker) { // uh-oh throw {
name: "ValidationError",
message: "No handler to validate type " + type
};
}
result_ok = checker.validate(data[i]);
if (!result_ok) {
msg = "Invalid value for *" + i + "*, " + checker.instructions;
this.messages.push(msg);
}
}
}
return this.hasErrors();
},
// helper
hasErrors: function () {
return this.messages.length !== 0;
}
};
var data = {
first_name: 'Super',
last_name: 'Man',
age: 'unknown',
username: 'o_0'
};
validator.config = {
first_name: 'isNonEmpty',
age: 'isNumber',
userName: 'isAlphaNum'
};
validator.validate(data);
if(validator.hasErrors()){
console.log(validator.messages.join('\n'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment