0

can Chain of Responsibility have a predessesor or there should be only successor in chain. I mean that if we need to call previous objects in chain. Is it possible. As much as I have seen online there are only successor. Will setting up a predessesor break the chain. For example,

    Handler object1 = new Start(); // return index of "c" in target String
    Handler object2 = new Normal();// matches "a"
    Handler object3 = new Dot();   // matches any one character
    Handler object4 = new End();   // matches "t",i.e., end of chain
   object1.setSuccessor(object2);
   object2.setSuccessor(object3);
   object3.setSuccessor(object4);


   Match  match = new Match( "car.ot" );
   object1.find( match );

Actual problem is how to match "r" and "o" using given chain of objects.

Here I need to match given String. object1 will return the position of "c". object2 will match "a". If its true , it should pass to match "r" to the next object in chain. How to do it without using a predessesor. Please help.

ela
  • 67
  • 1
  • 1
  • 6
  • What you seem to be using there is not a chain of responsibility, but a state machine. See https://en.wikipedia.org/wiki/State_pattern. But your question is quite unclear. You didn't tell what you wanted to achieve. – JB Nizet May 01 '16 at 09:00
  • Can't you just use another ```Normal```? – Jorn Vernee May 01 '16 at 09:03
  • There is nothing wrong with your implementation. But I'd rather say that this is neither chain of responsibility nor state pattern. When you need to make matcher a bit more complex (for example, by supporting alternatives like "xyz(a|b|c)def"), you will have to modify your "chain" into a tree. – gudok May 01 '16 at 09:14
  • @JB Nizet . Its actually a string pattern matcher using chain of responsibility. Here the there should be one object in the chain for each character in the pattern. It means that chain should be infinite as we do not know in advance that how many characters will be in string being matched I am trying to implement that. I am trying to implement that – ela May 01 '16 at 17:50
  • @gudok why is it not chain of responsibility. What changes I need to do to do the same – ela May 01 '16 at 17:59

0 Answers0