-2

I was reading COR(Chain of responsibility pattern),I felt it works as simple as a if else or switch case in system level and this pattern can be easily replaced by similar pattern like factory or composite pattern.

Is there any scenario or example to prove only COR can solve this??

Thanks in advance

keerti_h
  • 383
  • 2
  • 13
  • 4
    You could always code as you want, including *not* using COR (or any design pattern) at all. So I don't think there is any scenario to prove that COR *must* be used. COR, however, like any other design pattern, is useful for separation of concerns. And it is working more like collection of `if`s in the system level. – Ian May 18 '16 at 07:01
  • 1
    Completely agree with Ian, for a basic example where it's _convenient_ to use it: imagine you have an image processing application. You deploy multiple _filters_ to open different file formats but you can add more later (or 3rd party can write their own). User asks to open a file, which filter you should use? You iterate your list until you find one filter that say _yes, I can handle this_. – Adriano Repetti May 18 '16 at 07:04
  • but i just wanted to know.. what is that problem or situation which made us to think about this pattern. i agree its always up to the convenient among many solution.. Thanks @lan ,Ardiano – keerti_h May 18 '16 at 07:09

2 Answers2

2

A classic example COR is processing HelpRequested events in WindowsForms. See source.

If we have event handler on the particular control, then process it. Otherwise, a request to process an event is passed up to the parent control.

If else or switch case will not help us in this case, since the child control does not knows in advance who will be it parents and does not knows which of them can process a request to display help.

Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
0

Like Alexander said but on a more general level - every processing pipeline where the processors can be dynamically added, moved or removed based on information only known at runtime.

Patterns usually contain a very precise description of the problem at hand (which most people tend to disregard, instead relying on a gut feeling that the pattern is close enough to their context). Most of the time the solution is directly derivable from the problem formulation itself. This constrains the solution to pretty much one viable shape, although there are multiple possible implementations of a pattern.

guillaume31
  • 13,738
  • 1
  • 32
  • 51