@Override voidhandleRequest(Request request){ if (request.getRequestType().equals(RequestType.ConcreteHandler1)) System.out.println("ConcreteHandler1 instance has been created"); else super.handleRequest(request); } }
@Override voidhandleRequest(Request request){ if (request.getRequestType().equals(RequestType.ConcreteHandler2)) System.out.println("ConcreteHandler2 instance has been created"); else super.handleRequest(request); } }
classConcreteHandler1(Handler): defhandle_request(self, request): if request._type == 'ConcreteHandler1': print('ConcreteHandler1 instance has been created') else: self._next.handle_request(request)
classConcreteHandler2(Handler): defhandle_request(self, request): if request._type == 'ConcreteHandler2': print('ConcreteHandler2 instance has been created') else: self._next.handle_request(request)
classDefaultHandler(Handler): def__init__(self): pass defhandle_request(self, request): print('Without this request type')
if __name__ == '__main__': handler = ConcreteHandler1(ConcreteHandler2(DefaultHandler())) handler.handle_request(Request('ConcreteHandler1')) handler.handle_request(Request('ConcreteHandler2')) handler.handle_request(Request('ConcreteHandler3'))