One template that comes to mind is the Chain of Responsibility (perhaps not a creation template)
First, you need RequestHandlers
public interface IRequestHandler { bool CanHandle(Request req); void Handle(Request req); } public class LargeRequestHandler : IRequestHandler { public bool CanHandle(Request req) { return (req.Type == RequestType.SomeVal && req.id > 1000); } public void Handle(Request req) { processors.Add(new ProcessLargeRequest(request)); } } public class SmallRequestHandler : IRequestHandler { public bool CanHandle(Request req) { return (req.Type == RequestType.SomeVal && req.id < 1000); } public void Handle(Request req) { processors.Add(new SmallLargeRequest(request)); } }
... similarly, continue to add classes for more handlers as needed.
Then chain these handlers, for example
public class RequestChain { IRequestHandler[] handlers; public RequestChain() { handlers = new[] { new LargeRequestHandler(), new SmallRequestHandler() }; } public void ProcessRequest(Request req) { foreach (var handler in handlers) { if (handler.CanHandle(req)) { handler.Handle(req); } } } }
Hope this helps. Greetings.
source share