Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I see this a lot in frameworks.

You have a master class which other classes register with.

The master class then decides which of the registered classes to delegate the request to. An example based passed in class may be something this.

public interface Processor {
  public boolean canHandle(Object objectToHandle);
  public void handle(Object objectToHandle);
}

public class EvenNumberProcessor extends Processor {
  public boolean canHandle(Object objectToHandle) {
     if (!isNumeric(objectToHandle)){
      return false
     }
     return isEven(objectToHandle);
   }

   public void handle(objectToHandle) {
     //Optionally call canHandleAgain to ensure the calling class is fufilling its contract
     doSomething();
   }
 }

public class OddNumberProcessor extends Processor {
  public boolean canHandle(Object objectToHandle) {
     if (!isNumeric(objectToHandle)){
      return false
     }

    return isOdd(objectToHandle);
  }

   public void handle(objectToHandle) {
     //Optionally call canHandleAgain to ensure the calling class is fufilling its contract
     doSomething();
   }
 }

//Can optionally implement processor interface
public class processorDelegator {

  private List processors;
  public void addProcessor(Processor processor) {
    processors.add(processor);
  }

  public void process(Object objectToProcess) {
     //Lookup relevant processor either by keeping a list of what they can process
     //Or query each one to see if it can process the object.

     chosenProcessor=chooseProcessor(objectToProcess);
     chosenProcessor.handle(objectToProcess); 
  }

}

Note there are a few variations I see on this. In one variation the sub classes provide a list of things they can process which the ProcessorDelegator understands. The other variation which is listed above in fake code is where each is queried in turn.

This is similar to chain of command but I don't think its the same as chain of command means that the processor needs to pass to other processors.

The other variation is where the ProcessorDelegator itself implements the interface which means you can get trees of ProcessorDelegator which specialise further. In the above example you could have a numeric processor delegator which delegates to an even/odd processor and a string processordelegator which delegates to different strings.

My question is does this pattern have a name?

share|improve this question
1  
What makes you think it's any pattern at all? –  DeadMG Sep 5 '12 at 18:10
    
I thought a good name would be registry but that means something else. I've seen enough variations on this that a specific name would be useful for discussions. If one doesn't exist then thats a pity. –  Wes Sep 6 '12 at 9:19

5 Answers 5

To me it looks like the strategy pattern, only that the selection of the specific algorithm is not hard-coded.

share|improve this answer
1  
+1 I'd have gone with Strategy Pattern as well. –  Shivan Dragon Sep 5 '12 at 10:10

The over-all pattern of the code presented here is Strategy because it lets the underlying algorithm vary independently of the clients that use it. A Chain of Responsibility in the chooseProcessor() method determines which underlying algorithm is used by giving more than one client a chance to handle a request.

If your example focused on the implementation of the chooseProcessor() method, I could see Chain of Responsibility being the dominant pattern, but since only two comments are given and no implementation, your example clearly focuses on the Strategy method.

share|improve this answer

It probably best fits into the Chain of Responsibility, but seems specific enough that it should be considered different. I don't think there's a name though for this specific pattern.

share|improve this answer
    
It's closest to Chain of Responsibility to me as well. But one could also argue for Strategy –  Mike Brown Sep 5 '12 at 6:08

I would disagree with everyone else here and go with a tierable implementation of the observer pattern. The word 'handle' gives it away: something occurs or is to occur and a list of handlers are given a piece of data based on the occurrence, and often times in observers you're first act is to test the data to see if you can handle it, and if not ignore it. The dictation back upwards that you can handle it is no different than in javascript when you call e.Cancel(); or some other such to stop the rest of the event chain.

share|improve this answer

What makes you think it's any kind of pattern at all? Maybe it's just (gosh) some code. So I'mma go with "None".

I mean, fundamentally, what makes a pattern a pattern is that you label it that way and then the other developers need much less additional documentation to understand how it works. But since you haven't given it any name, and I'm not at all sure how on earth it works or what the point is, even after reading the full implementation (for example, the chooseProcessor method, a vital component, appears to be missing). So it appears to me that, in all the ways that count, it's not a pattern.

I mean, if only you could do simply have some kind of type system to distinguish what items each Processor can accept and then you could register those Processors with uh, uh, uh, a compiler, and then it would implement all the logic for you. A man can dream.

share|improve this answer
    
I just hoped there was a name as it makes conventions / discusions arround a structure I've seen serveral times easier. –  Wes Sep 6 '12 at 9:11
    
Object may have been too loose for the example. –  Wes Sep 6 '12 at 9:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.