3.4. The Chain of Responsibility Pattern (ok)

C:\xampp\htdocs\php\js\main.js

require(['cor/init'],function(init) {
	var examples = {
		cor: init
	}
	for (var example in examples) {
		examples[example].init();
	}
});

C:\xampp\htdocs\php\js\cor\init.js

define(function(require) {
  'use strict';
  return {
    init: function() {
      var call, sms, email, handler, telepathy, Handler = require('cor/handler');
      var callHandler = require('cor/handlers/call');
      call = {
        type: 'call',
        number: '07123456789',
        ownNumber: '070031101003'
      };
      sms = {
        type: 'sms',
        number: '07123456789',
        message: 'Hey Dan'
      };
      email = {
        type: 'email',
        recipient: 'dan@danwellman.co.uk',
        message: 'Hi Dan'
      };
      telepathy = {
        type: 'esp',
        target: 'someone else',
        message: 'spooky'
      };
      handler = new Handler(null, null, callHandler);
      handler.handleCommunication(email);
      handler.handleCommunication(sms);
      handler.handleCommunication(call);
      handler.handleCommunication(telepathy);
    }
  };
});

C:\xampp\htdocs\php\js\cor\handler.js

define(function() {
  'use strict';
  var CommunicationHandler = function(communicationType, handler, nextHandler) {
    this.communicationType = communicationType;
    this.handler = handler;
    this.nextHandler = nextHandler;
  };
  CommunicationHandler.prototype.handleCommunication = function(communication) {
    if (communication.type !== this.communicationType) {
      (this.nextHandler) ? this.nextHandler.handleCommunication(communication): console.log('Communication type', communication.type, 'could not be handled');
      return;
    }
    this.handler(communication);
  };
  return CommunicationHandler;
});

C:\xampp\htdocs\php\js\cor\handlers\call.js

define(function(require) {
  'use strict';
  var Handler = require('cor/handler');
  var smsHandler = require('cor/handlers/sms');
  var callHandler;
  callHandler = new Handler('call', handleCall, smsHandler);
  function handleCall(call) {
    console.log('Call placed to number', call.number, 'from number', call.ownNumber);
  }
  return callHandler;
});

C:\xampp\htdocs\php\js\cor\handlers\sms.js

define(function(require) {
  'use strict';
  var Handler = require('cor/handler');
  var emailHandler = require('cor/handlers/email');
  var smsHandler;
  smsHandler = new Handler('sms', handleSms, emailHandler);
  function handleSms(sms) {
    console.log('SMS sent to number', sms.number, 'message: ', sms.message);
  }
  return smsHandler;
});

C:\xampp\htdocs\php\js\cor\handlers\email.js

define(function(require) {
  'use strict';
  var Handler = require('cor/handler');
  var emailHandler;
  emailHandler = new Handler('email', handleEmail, null);
  function handleEmail(email) {
    console.log('Email sent to', email.recipient, 'message: ', email.message);
  }
  return emailHandler;
});

Last updated

Navigation

Lionel

@Copyright 2023