what’s the name of this pattern?

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật