Observer-EXP// The ObserverObject – One who super sees all the print operations function printManager( ) { var queue = [ ]; // The attach method this.addJob = function(name, job) { queue.push( { ”name” : name, "job” : job } ); } // The detach method this.removeJob = function(job) { var _queue = [ ]; for(var i in queue) { if(queue[ i ].job == job) continue; else _queue.push( queue[ i ] ); } queue = _queue; } // The notify method this.doPrint = function( item ) { for ( var i in queue ) { queue[ i ].job.call( this, item );}} }
19.
Observer-EXPvar p =new printManager(); // Publishers are in charge of "publishing” function printWithItalics( str ) { // The callback function – the print job alert( “<i>” + str + “</i>” ); } //Once subscribers are notified their callback functions are invokedp.addJob( "italics", printWithItalics); // Notify the observer about a state change p.doPrint("this is a test");
Strategy-EXPvar sum =function(n) { var sum = 0; for ( var a in n ) { sum = sum + n[a]; } return sum;}; var a = new Button(sum, "Add numbers");var b = new Button(function(numbers) { return numbers.join(','); }, "test2");a.on_submit(numbers);b.on_submit(numbers);