Chain of responsibility

12
http://jy aasa.com Copyright 2016. Jyaasa Technologies. Chain of Responsibility

Transcript of Chain of responsibility

http://jyaasa.comCopyright 2016. Jyaasa Technologies.

Chain of Responsibility

Copyright 2016. Jyaasa Technologies.

http://jyaasa.com

Associate Software Engineer at

Jyaasa Technologies

Hello ! I am Sarbada Nanda Jaiswal

Copyright 2016. Jyaasa Technologies.

http://jyaasa.com

What is Chain of Responsibility?

Copyright 2016. Jyaasa Technologies.

http://jyaasa.com

Avoid coupling the sender of a request to its receiver by giving more than one receiver object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

Copyright 2016. Jyaasa Technologies.

http://jyaasa.com

Structure:

Copyright 2016. Jyaasa Technologies.

http://jyaasa.com

The chain of responsibility pattern creates a chain of receiver objects for a request.

In this pattern, normally each receiver contains reference to another receiver. If one receiver can not handle the request then it passes the same request to the next receiver and so on.

One receiver in the chain handles a request or one or more receiver in the chain handles a request.

Copyright 2016. Jyaasa Technologies.

http://jyaasa.com

Copyright 2016. Jyaasa Technologies.

http://jyaasa.com

In Ruby: Chain of Responsibility

module Chainable

def next_in_chain(link) @next = link end

def method_missing(method, *args, &block) if @next == nil puts "This request cannot be handled!" return end @next.__send__(method, *args, &block) endend

class WebManager include Chainable

def initialize(link = nil) next_in_chain(link) end def deliver_application design_interface build_application write_documentation deploy_application puts "#{self.class.to_s}: Application delivered" end

end

class WebDeveloper include Chainable

def initialize(link = nil) next_in_chain(link) end

def build_application puts "#{self.class.to_s}: I'm building the application" end

def deploy_application puts "#{self.class.to_s}: I'm deploying the application" end

end

Copyright 2016. Jyaasa Technologies.

http://jyaasa.com

class WebDesigner include Chainable

def initialize(link = nil) next_in_chain(link) end

def design_interface puts "#{self.class.to_s}: I'm designing the interface" end

end

class TechnicalWriter include Chainable

def initialize(link = nil) next_in_chain(link) end

def write_documentation puts "#{self.class.to_s}: I'm writing the documentation" end

end

provider = WebManager.new(WebDeveloper.new(WebDesigner.new(TechnicalWriter.new)))

provider.deliver_application

provider.make_support

OutPut:

WebDesigner: I'm designing the interface

WebDeveloper: I'm building the application

TechnicalWriter: I'm writing documentation

WebDeveloper: I'm deploying the application

WebManager: Application delivered

This request cannot be handled!

Copyright 2016. Jyaasa Technologies.

http://jyaasa.com

Copyright 2015. Jyaasa Technologies. Copyright 2016. Jyaasa Technologies.

http://jyaasa.com