5. Factory-EXP if (typeofXMLHttpRequest != "undefined") { return new XMLHttpRequest();} else if (typeofwindow.ActiveXObject != "undefined") { return new ActiveXObject("MSXML2.XMLHttp");} function XMLHttpFactory() {}XMLHttpFactory.createXMLHttp = function () { if (typeofXMLHttpRequest != "undefined") { return new XMLHttpRequest(); } else if (typeofwindow.ActiveXObject != "undefined") { return new ActiveXObject("MSXML2.XMLHttp"); }} varxmlhttp=XMLHttpFactory.createXMLHttp();
18. Observer-EXP // The Observer Object – 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-EXP var 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 invoked p.addJob( "italics", printWithItalics); // Notify the observer about a state change p.doPrint("this is a test");
24. Strategy-EXP var 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);