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?
2
To me it looks like the strategy pattern, only that the selection of the specific algorithm is not hard-coded.
1
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.
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.
1
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.
1
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.
2