Chain of response yet another popular pattern that can be used in the situation where :-
- Initial configuration takes more time.
- Same type of request comes for many time to process.
Example,
— Printer is perfect real time example for these above scenario.
— Printer needs significant amount of time for warming up for the first request.
— Example code below,
/*Chain of Responsibility
*@Sandeep Kumar Patel
* 1. A reusable object is created 'printer'.
* 2. This 'printer' can handle the subsequent requests.
*/
var Printer = function(textToPrint){/*All configuration stuff related to printer will be done here*/}
Printer.prototype = {
print :function(textToPrint){
alert(textToPrint);
return this;
}
}
var work = function(){
var printer = new Printer();
/*Here is a chain of request/responsibility for printing*/
printer.print('sandeep0').print('sandeep1')
.print('sandeep2').print('sandeep3').print('sandeep4');
}
work();